solve(A, B, C, D, E) :- % Correct range: Each worker can have 0 to 600 loaves of bread A in 0..600, B in 0..600, C in 0..600, D in 0..600, E in 0..600, % Rules (unchanged from the previous version) X1 #> 0, B #= A + X1, C #= B + X1, D #= C + X1, E #= D + X1, A * 7 #= C, B * 7 #= D, A + B + C + D + E #= 600, % Labeling and printing (unchanged from the previous version) labeling([], [A, B, C, D, E]), tell('Worker A', A), tell('Worker B', B), tell('Worker C', C), tell('Worker D', D), tell('Worker E', E). tell(Worker, Loaves) :- write(Worker), write(' gets '), write(Loaves), write(' loaves of bread.'), nl.
Write, Run & Share Prolog code online using OneCompiler’s Prolog online compiler for free. It’s a simple and intuitive platform to experiment with logic programming in Prolog. OneCompiler supports standard Prolog syntax, great for learning, prototyping, and practicing logic-based problems.
Prolog (Programming in Logic) is a logic programming language associated with artificial intelligence and computational linguistics. It works through facts, rules, and queries, using a form of symbolic reasoning known as backward chaining. Prolog is declarative, meaning you describe what you want instead of how to compute it.
The following is a simple Prolog program that prints a greeting:
:- initialization(main).
main :-
write('Hello, World!').
Facts represent basic assertions about the world.
likes(alice, pizza).
likes(bob, pasta).
Rules define logical relationships using facts.
friends(X, Y) :- likes(X, Z), likes(Y, Z).
Queries are used to find information based on facts and rules.
?- likes(alice, What).
Operator | Description |
---|---|
:- | Rule definition |
, | Logical AND |
; | Logical OR |
= | Unification |
member(X, [X|_]).
member(X, [_|T]) :- member(X, T).
Prolog heavily relies on recursion.
factorial(0, 1).
factorial(N, F) :-
N > 0,
N1 is N - 1,
factorial(N1, F1),
F is N * F1.
This guide provides a quick reference to Prolog programming syntax and features. Start writing Prolog code using OneCompiler’s Prolog online compiler today!