:- table(fib/2).
fib(0, 1) :- !.
fib(1, 1) :- !.
fib(N, F) :-
N > 1,
N1 is N-1,
N2 is N-2,
fib(N1, F1),
fib(N2, F2),
F is F1+F2.
?- fib(91, X).
X = 7540113804746346429
yes
:- table(connection/2).
connection(X, Y) :- write(X), write(' '), write(Y), nl, fail.
connection(X, Y) :-
connection(X, Z),
connection(Z, Y).
connection(X, Y) :-
connection(Y, X).
connection(a, b).
connection(a, c).
connection(b, d).
connection(c, d).
?- connection(a, X).
a Y
X a
X Y
b Y
X b
b b
d b
d Y
X d
b d
d d
c d
c Y
X c
b c
d c
c b
c c
a c
c a
d a
a d
a b
b a
a a
X = b
yes;
X = c
yes;
X = a
yes;
X = d
yes
?- connection(a, X).
X = b
yes;
X = c
yes;
X = a
yes;
X = d
yes
:- table(path/2).
edge(a, b).
edge(b, c).
edge(c, a).
edge(c, d).
path(X, Y) :-
edge(X, Y).
path(X, Y) :-
edge(X, Z),
path(Z, Y).
?- path(a,X).
X = b
yes;
X = c
yes;
X = a
yes;
X = d
yes
?- path(a,a).
yes
?- path(a,b).
yes
?- path(a,c).
yes
?- path(a,d).
yes
?- path(a,z).
no
:- table(test_recursion/2).
test_recursion(1, finished) :- !.
test_recursion(N, X) :-
write('test '), write(N), nl,
N > 1,
N1 is N - 1,
test_recursion(N1, X).
?- test_recursion(5, X).
test 5
test 4
test 3
test 2
X = finished
yes
?- test_recursion(5, X).
X = finished
yes
?- test_recursion(9, X).
test 9
test 8
test 7
test 6
X = finished
yes
:- table(test_error/1).
test_error(1) :- X is Y, !.
test_error(N) :-
write('test_error '), write(N), nl,
N > 1,
N1 is N - 1,
test_error(N1).
?- test_error(5).
Cannot get Numeric for term: Y of type: VARIABLE
?- test_error(5).
Cannot get Numeric for term: Y of type: VARIABLE
?- test_error(9).
Cannot get Numeric for term: Y of type: VARIABLE
:- table(test_remove_duplicates/2).
test_remove_duplicates(X,Y) :- (true;true), X=1, Y=4, writeln(a).
test_remove_duplicates(X,Y) :- X=8, Y=9, writeln(b).
test_remove_duplicates(X,Y) :- X=8, Y=9, writeln(c).
test_remove_duplicates(X,Y) :- (X=1;X=3), Y=2, writeln(d).
?- test_remove_duplicates(X, Y).
a
a
b
c
d
d
X = 1
Y = 4
yes;
X = 8
Y = 9
yes;
X = 1
Y = 2
yes;
X = 3
Y = 2
yes
?- test_remove_duplicates(X, Y).
X = 1
Y = 4
yes;
X = 8
Y = 9
yes;
X = 1
Y = 2
yes;
X = 3
Y = 2
yes
?- test_remove_duplicates(1, Y).
a
a
d
Y = 4
yes;
Y = 2
yes
:- table(test_variables/4).
test_variables(p(X,Y,Z), a, b, c) :- write(1), nl.
test_variables(p(a,b,c), X, Y, Z) :- write(2), nl.
?- test_variables(p(A,B,C), A, B, C).
1
2
A = a
B = b
C = c
yes
?- test_variables(p(B,C,A), B, C, A).
A = c
B = a
C = b
yes
?- test_variables(p(A,B,C), C, A, B).
1
2
A = b
B = c
C = a
yes;
A = a
B = b
C = c
yes
?- test_variables(A, B, C, D), A=p(a,_,_).
1
2
A = p(a, _, _)
B = a
C = b
D = c
yes;
A = p(a, b, c)
B = UNINSTANTIATED VARIABLE
C = UNINSTANTIATED VARIABLE
D = UNINSTANTIATED VARIABLE
yes
?- test_variables(A, B, C, D).
A = p(X, Y, Z)
B = a
C = b
D = c
yes;
A = p(a, b, c)
B = UNINSTANTIATED VARIABLE
C = UNINSTANTIATED VARIABLE
D = UNINSTANTIATED VARIABLE
yes