fork(1) download
  1. :- initialization(run).
  2.  
  3. /* A + B = C */
  4. abc(A,B,C) :- nonvar(A), nonvar(B), write('A+B '), !, C is A+B.
  5. abc(A,B,C) :- nonvar(A), nonvar(C), write('C-A '), !, B is C-A.
  6. abc(A,B,C) :- nonvar(B), nonvar(C), write('C-B '), !, A is C-B.
  7. /* no idea how to solve */
  8. abc(A,B,C) :- write('figvam '), fail.
  9.  
  10. writes([]) :- write('\n').
  11. writes([X|Xs]) :- write(X), writes(Xs).
  12.  
  13. test(A,B,C) :-
  14. writes(['start...']),
  15. abc(A,B,C),
  16. writes(['A=',A, ' B=',B, ' C=',C]);
  17. writes(['failed.']).
  18.  
  19. run :-
  20. test(1,4,_),
  21. test(1,_,5),
  22. test(_,4,5),
  23. test(1,4,5),
  24. test(1,2,10),
  25. test(_,_,10),
  26. test(_,10,_),
  27. test(10,_,_).
Success #stdin #stdout 0.02s 68352KB
stdin
Standard input is empty
stdout
start...
A+B A=1 B=4 C=5
start...
C-A A=1 B=4 C=5
start...
C-B A=1 B=4 C=5
start...
A+B A=1 B=4 C=5
start...
A+B failed.
start...
figvam failed.
start...
figvam failed.
start...
figvam failed.
GNU Prolog 1.3.1
By Daniel Diaz
Copyright (C) 1999-2009 Daniel Diaz