/* pete1a.pl * * CS 313 * * easy version of pete's puzzle solver * * extended to try to solve all puzzles with nums 1 and 2. */ num(1). num(2). num(3). /* can add more numbers here */ solveall :- writeln('pete1a.pl - all solutions for puzzles with 1 and 2 only'), num(A), num(B), num(C), num(D), write(A), write(B), write(C), write(D), write(': '), trysolve(A, B, C, D), fail. trysolve(A, B, C, D) :- solve(A, B, C, D), !. /* the CUT prevents backtracking for more solns */ trysolve(_, _, _, _) :- writeln('---------- no solution'). /* rest as in pete1.pl, except using =:= (equality after evaluating): */ solve(A, B, C, D) :- exp2(A, B, E1), exp2(C, D, E2), E1 =:= E2, /* replaces 'checkeq' predicate in pete1.pl */ writeln(E1 = E2). exp2(A, B, A+B). exp2(A, B, A-B). exp2(A, B, A*B). exp2(A, B, A/B) :- B \= 0. /* Sample output: ?- solveall. pete1a.pl - all solutions for puzzles with 1 and 2 only 1111: 1+1=1+1 1112: 1+1=1*2 1121: 1+1=2*1 1122: 1-1=2-2 1211: 1*2=1+1 1212: 1+2=1+2 1221: 1+2=2+1 1222: ---------- no solution 2111: 2-1=1*1 2112: 2+1=1+2 2121: 2+1=2+1 2122: 2-1=2/2 2211: 2-2=1-1 2212: ---------- no solution 2221: 2/2=2-1 2222: 2+2=2+2 false. */