I'm brand new to Prolog, and I'm interested in converting the following word problem into (SWI) Prolog:
There are 4 children: Abe, Dan, Mary, and Sue. Their ages, in no particular order, are 3, 5, 6, and 8. Abe is older than Dan. Sue is younger than Mary. Sue's age is Dan's age plus 3 years. Mary is older than Abe.
So far I've come up with
child(X) :-
member(X, [3,5,6,8]).
solution(Abe, Dan, Mary, Sue) :-
child(Abe),
child(Dan),
child(Mary),
child(Sue),
Abe > Dan,
Sue < Mary,
Sue == Dan+3,
Mary > Abe,
Abe \== Dan,
Abe \== Mary,
Abe \== Sue,
Dan \== Mary,
Dan \== Sue,
Mary \== Sue.
But running the query
?- solution(Abe, Dan, Mary, Sue)
I just get false
. As a side question, will Prolog perform brute force search for solutions, or is there some machinery which can solve this (sort of) problem in better than O(n!) ?
The result I want is Abe = 5, Dan = 3, Mary = 9, Sue = 6
.
Sue == Dan+3
will not perform the arithmetic operation. You needSue =:= Dan+3
for that. – lurker