转载自:https://mp.weixin.qq.com/s/ETG_9fiElwWtQYb-JUf6yA
?- [user].
|: p(X):- a(X).
|: p(X):- b(X), c(X), d(X), e(X).
|: p(X):- f(X).
|: a(1).
|: b(1).
|: b(2).
|: c(1).
|: c(2).
|: d(2).
|: e(2).
|: f(3).
|: ^D% user://1 compiled 0.00 sec, 11 clauses
true.
?- p(X).
X = 1 ;
X = 2 ;
X = 3.
[trace] ?- p(X).
Call: (10) p(_8342) ? creep
Call: (11) a(_8342) ? creep
Exit: (11) a(1) ? creep
Exit: (10) p(1) ? creep
X = 1 ;
Redo: (10) p(_8342) ? creep
Call: (11) b(_8342) ? creep
Exit: (11) b(1) ? creep
Call: (11) c(1) ? creep
Exit: (11) c(1) ? creep
Call: (11) d(1) ? creep
Fail: (11) d(1) ? creep
Redo: (11) b(_8342) ? creep
Exit: (11) b(2) ? creep
Call: (11) c(2) ? creep
Exit: (11) c(2) ? creep
Call: (11) d(2) ? creep
Exit: (11) d(2) ? creep
Call: (11) e(2) ? creep
Exit: (11) e(2) ? creep
Exit: (10) p(2) ? creep
X = 2 ;
Redo: (10) p(_8342) ? creep
Call: (11) f(_8342) ? creep
Exit: (11) f(3) ? creep
Exit: (10) p(3) ? creep
X = 3.
?- [user].
|: p(X):- a(X).
Warning: user://2:98:
Warning: Redefined static procedure p/1
Warning: Previously defined at user://1:9
|: p(X):- b(X), c(X), !, d(X), e(X).
|: p(X):- f(X).
|: ^D% user://2 compiled 0.00 sec, 3 clauses
true.
?- listing(p).
p(A) :-
a(A).
p(A) :-
b(A),
c(A),
!,
d(A),
e(A).
p(A) :-
f(A).
true.
?- trace.
true.
[trace] ?- p(X).
Call: (10) p(_10668) ? creep
Call: (11) a(_10668) ? creep
Exit: (11) a(1) ? creep
Exit: (10) p(1) ? creep
X = 1 ;
Redo: (10) p(_10668) ? creep
Call: (11) b(_10668) ? creep
Exit: (11) b(1) ? creep
Call: (11) c(1) ? creep
Exit: (11) c(1) ? creep
Call: (11) d(1) ? creep
Fail: (11) d(1) ? creep
Fail: (10) p(_10668) ? creep
false.
?- [user].
|: s(X,Y):- q(X,Y).
|: s(0,0).
|: q(X,Y):- i(X), j(Y).
|: i(1).
|: i(2).
|: j(1).
|: j(2).
|: j(3).
|: ^D% user://3 compiled 0.00 sec, 8 clauses
true.
?- s(X,Y).
X = Y, Y = 1 ;
X = 1,
Y = 2 ;
X = 1,
Y = 3 ;
X = 2,
Y = 1 ;
X = Y, Y = 2 ;
X = 2,
Y = 3 ;
X = Y, Y = 0.
?- trace.
true.
[trace] ?- s(X,Y).
Call: (10) s(_14996, _14998) ? creep
Call: (11) q(_14996, _14998) ? creep
Call: (12) i(_14996) ? creep
Exit: (12) i(1) ? creep
Call: (12) j(_14998) ? creep
Exit: (12) j(1) ? creep
Exit: (11) q(1, 1) ? creep
Exit: (10) s(1, 1) ? creep
X = Y, Y = 1 ;
Redo: (12) j(_14998) ? creep
Exit: (12) j(2) ? creep
Exit: (11) q(1, 2) ? creep
Exit: (10) s(1, 2) ? creep
X = 1,
Y = 2 ;
Redo: (12) j(_14998) ? creep
Exit: (12) j(3) ? creep
Exit: (11) q(1, 3) ? creep
Exit: (10) s(1, 3) ? creep
X = 1,
Y = 3 ;
Redo: (12) i(_14996) ? creep
Exit: (12) i(2) ? creep
Call: (12) j(_14998) ? creep
Exit: (12) j(1) ? creep
Exit: (11) q(2, 1) ? creep
Exit: (10) s(2, 1) ? creep
X = 2,
Y = 1 ;
Redo: (12) j(_14998) ? creep
Exit: (12) j(2) ? creep
Exit: (11) q(2, 2) ? creep
Exit: (10) s(2, 2) ? creep
X = Y, Y = 2 ;
Redo: (12) j(_14998) ? creep
Exit: (12) j(3) ? creep
Exit: (11) q(2, 3) ? creep
Exit: (10) s(2, 3) ? creep
X = 2,
Y = 3 ;
Redo: (10) s(_14996, _14998) ? creep
Exit: (10) s(0, 0) ? creep
X = Y, Y = 0.
[trace] ?- notrace.
true.
[debug] ?- nodebug.
true.
?- [user].
|: q(X,Y):- i(X), !, j(Y).
Warning: user://4:252:
Warning: Redefined static procedure q/2
Warning: Previously defined at user://3:188
|: ^D% user://4 compiled 0.00 sec, 1 clauses
true.
?- listing(q).
q(A, B) :-
i(A),
!,
j(B).
true.
?- trace.
true.
[trace] ?- s(X,Y).
Call: (10) s(_18488, _18490) ? creep
Call: (11) q(_18488, _18490) ? creep
Call: (12) i(_18488) ? creep
Exit: (12) i(1) ? creep
Call: (12) j(_18490) ? creep
Exit: (12) j(1) ? creep
Exit: (11) q(1, 1) ? creep
Exit: (10) s(1, 1) ? creep
X = Y, Y = 1 ;
Redo: (12) j(_18490) ? creep
Exit: (12) j(2) ? creep
Exit: (11) q(1, 2) ? creep
Exit: (10) s(1, 2) ? creep
X = 1,
Y = 2 ;
Redo: (12) j(_18490) ? creep
Exit: (12) j(3) ? creep
Exit: (11) q(1, 3) ? creep
Exit: (10) s(1, 3) ? creep
X = 1,
Y = 3 ;
Redo: (10) s(_18488, _18490) ? creep
Exit: (10) s(0, 0) ? creep
X = Y, Y = 0.
//体会下面两段代码
?- [user].
|: max(X,Y,Y):-X=<Y.
|: max(X,Y,X):-X>Y.
|: ^D% user://6 compiled 0.00 sec, 2 clauses
true.
?- max(2,3,3).
true ;
false.
?- [user].
|: max(X,Y,Y):-X=<Y,!.
Warning: user://7:372:
Warning: Redefined static procedure max/3
Warning: Previously defined at user://6:362
|: max(X,Y,X):-X>Y.
|: ^D% user://7 compiled 0.00 sec, 2 clauses
true.
?- listing(max).
max(B, A, A) :-
B=<A,
!.
max(A, B, A) :-
A>B.
true.
?- max(2,3,3).
true.
// end!
?- [user].
|: max(B, A, A) :-
|: B=<A,
|: !.
Warning: user://8:394:
Warning: Redefined static procedure max/3
Warning: Previously defined at user://6:362
|: max(A, B, A).
Warning: user://8:401:
Warning: Singleton variables: [B]
|: ^D% user://8 compiled 0.00 sec, 2 clauses
true.
?- listing(max).
max(B, A, A) :-
B=<A,
!.
max(A, _, A).
true.
?- max(100,101,X).
X = 101.
?- max(3,2,X).
X = 3.
?- max(2,3,2).
true.
?- [user].
|: max(X,Y,Z):- X =< Y, !, Y = Z.
Warning: user://9:440:
Warning: Redefined static procedure max/3
Warning: Previously defined at user://8:394
|: max(X,Y,X).
Warning: user://9:445:
Warning: Singleton variables: [Y]
|: ^D% user://9 compiled 0.00 sec, 2 clauses
true.
?- listing(max).
max(A, B, C) :-
A=<B,
!,
B=C.
max(A, _, A).
true.
?- max(2,3,2).
false.
?- trace
| .
true.
[trace] ?- max(2,3,2).
Call: (10) max(2, 3, 2) ? creep
Call: (11) 2=<3 ? creep
Exit: (11) 2=<3 ? creep
Call: (11) 3=2 ? creep
Fail: (11) 3=2 ? creep
Fail: (10) max(2, 3, 2) ? creep
false.
[trace] ?- notrace.
true.
[debug] ?- nodebug.
true.
?- [user].
|: enjoys(vincent,X) :- big_kahuna_burger(X),!,fail.
|: enjoys(vincent,X) :- burger(X).
|: burger(X) :- big_mac(X).
|: burger(X) :- big_kahuna_burger(X).
|: burger(X) :- whopper(X).
|: big_mac(a).
|: big_mac(c).
|: big_kahuna_burger(b).
|: whopper(d).
|: ^D% user://1 compiled 0.00 sec, 9 clauses
true.
?- trace.
true.
[trace] ?- enjoys(vincent,a).
Call: (10) enjoys(vincent, a) ? creep
Call: (11) big_kahuna_burger(a) ? creep
Fail: (11) big_kahuna_burger(a) ? creep
Redo: (10) enjoys(vincent, a) ? creep
Call: (11) burger(a) ? creep
Call: (12) big_mac(a) ? creep
Exit: (12) big_mac(a) ? creep
Exit: (11) burger(a) ? creep
Exit: (10) enjoys(vincent, a) ? creep
true .
[trace] ?- enjoys(vincent,b).
Call: (10) enjoys(vincent, b) ? creep
Call: (11) big_kahuna_burger(b) ? creep
Exit: (11) big_kahuna_burger(b) ? creep
Call: (11) fail ? creep
Fail: (11) fail ? creep
Fail: (10) enjoys(vincent, b) ? creep
false.
[trace] ?- enjoys(vincent,c).
Call: (10) enjoys(vincent, c) ? creep
Call: (11) big_kahuna_burger(c) ? creep
Fail: (11) big_kahuna_burger(c) ? creep
Redo: (10) enjoys(vincent, c) ? creep
Call: (11) burger(c) ? creep
Call: (12) big_mac(c) ? creep
Exit: (12) big_mac(c) ? creep
Exit: (11) burger(c) ? creep
Exit: (10) enjoys(vincent, c) ? creep
true .
[trace] ?- enjoys(vincent,d).
Call: (10) enjoys(vincent, d) ? creep
Call: (11) big_kahuna_burger(d) ? creep
Fail: (11) big_kahuna_burger(d) ? creep
Redo: (10) enjoys(vincent, d) ? creep
Call: (11) burger(d) ? creep
Call: (12) big_mac(d) ? creep
Fail: (12) big_mac(d) ? creep
Redo: (11) burger(d) ? creep
Call: (12) big_kahuna_burger(d) ? creep
Fail: (12) big_kahuna_burger(d) ? creep
Redo: (11) burger(d) ? creep
Call: (12) whopper(d) ? creep
Exit: (12) whopper(d) ? creep
Exit: (11) burger(d) ? creep
Exit: (10) enjoys(vincent, d) ? creep
true.
?- [user].
|: enjoys(vincent, A) :-
|: burger(A).
Warning: user://4:164:
Warning: Redefined static procedure enjoys/2
Warning: Previously defined at user://3:115
|: enjoys(vincent, A) :-
|: big_kahuna_burger(A),
|: !,fail.
|: ^D% user://4 compiled 0.00 sec, 2 clauses
true.
?- listing(enjoys).
enjoys(vincent, A) :-
burger(A).
enjoys(vincent, A) :-
big_kahuna_burger(A),
!,
fail.
true.
?- trace.
true.
[trace] ?- enjoys(vincent,a).
Call: (10) enjoys(vincent, a) ? creep
Call: (11) burger(a) ? creep
Call: (12) big_mac(a) ? creep
Exit: (12) big_mac(a) ? creep
Exit: (11) burger(a) ? creep
Exit: (10) enjoys(vincent, a) ? creep
true .
[trace] ?- enjoys(vincent,b).
Call: (10) enjoys(vincent, b) ? creep
Call: (11) burger(b) ? creep
Call: (12) big_mac(b) ? creep
Fail: (12) big_mac(b) ? creep
Redo: (11) burger(b) ? creep
Call: (12) big_kahuna_burger(b) ? creep
Exit: (12) big_kahuna_burger(b) ? creep
Exit: (11) burger(b) ? creep
Exit: (10) enjoys(vincent, b) ? creep
true .
?- [user].
|: enjoys(vincent,X) :- burger(X),
|: \+ big_kahuna_burger(X).
|: burger(X) :- big_mac(X).
|: burger(X) :- big_kahuna_burger(X).
|: burger(X) :- whopper(X).
|: big_mac(a).
|: big_mac(c).
|: big_kahuna_burger(b).
|: whopper(d).
|: ^D% user://1 compiled 0.00 sec, 8 clauses
true.
?- enjoys(vincent,X).
X = a ;
X = c ;
X = d.
?- [user].
|: enjoys(vincent,X) :- \+ big_kahuna_burger(X), burger(X).
Warning: user://3:71:
Warning: Redefined static procedure enjoys/2
Warning: Previously defined at user://1:9
|: ^D% user://3 compiled 0.00 sec, 1 clauses
true.
?- listing(enjoys).
enjoys(vincent, A) :-
\+ big_kahuna_burger(A),
burger(A).
true.
?- enjoys(vincent,X).
false.
练习
// 10-1
?- [user].
|: p(1).
|: p(2) :- !.
|: p(3).
|: ^D% user://4 compiled 0.00 sec, 3 clauses
true.
?- p(X).
X = 1 ;
X = 2.
?- p(X),p(Y).
X = Y, Y = 1 ;
X = 1,
Y = 2 ;
X = 2,
Y = 1 ;
X = Y, Y = 2.
?- p(X),!,p(Y).
X = Y, Y = 1 ;
X = 1,
Y = 2.
//10-2
原始程序告诉数字是正数,零还有负数。它使用三个子句来做到这一点。但是,如果这三个子句之一成功解决了一个目标,则其他子句将不适用。因此,我们可以添加绿色切口:
class(Number,positive):- Number > 0, !.
class(0,zero):- !.
class(Number,negative):- Number < 0, !.
//10-3
//不使用截断
?- [user].
|: split([],[],[]).
|: split([Number|L],[Number|Pos],Neg):-
|: Number >= 0,
|: split(L,Pos,Neg).
|: split([Number|L],Pos,[Number|Neg]):-
|: Number < 0,
|: split(L,Pos,Neg).
|: ^D% user://1 compiled 0.01 sec, 3 clauses
true.
?- listing(split).
split([], [], []).
split([A|B], [A|C], D) :-
A>=0,
split(B, C, D).
split([A|B], C, [A|D]) :-
A<0,
split(B, C, D).
true.
?- split([3,4,-5,-1,0,4,-9],P,N).
P = [3, 4, 0, 4],
N = [-5, -1, -9] .
//使用截断
?- [user].
|: split([],[],[]):- !.
|: split([Number|L],[Number|Pos],Neg):-
|: Number > 0, !,
|: split(L,Pos,Neg).
|: split([Number|L],[Number|Pos],Neg):-
|: Number = 0, !,
|: split(L,Pos,Neg).
|: split([Number|L],Pos,[Number|Neg]):-
|: Number < 0, !,
|: split(L,Pos,Neg).
|: ^D% user://2 compiled 0.01 sec, 4 clauses
true.
?- listing(split).
split([], [], []) :-
!.
split([A|B], [A|C], D) :-
A>0,
!,
split(B, C, D).
split([A|B], [A|C], D) :-
A=0,
!,
split(B, C, D).
split([A|B], C, [A|D]) :-
A<0,
!,
split(B, C, D).
true.
?- split([3,4,-5,-1,0,4,-9],P,N).
P = [3, 4, 0, 4],
N = [-5, -1, -9].
//10-4
?- [user].
|: directTrain(saarbruecken,dudweiler).
|: directTrain(forbach,saarbruecken).
|: directTrain(freyming,forbach).
|: directTrain(stAvold,freyming).
|: directTrain(fahlquemont,stAvold).
|: directTrain(metz,fahlquemont).
|: directTrain(nancy,metz).
|: trainConnection(A,B):- directTrain(A,B).
|: trainConnection(A,B):- directTrain(B,A).
|: route(A,B,Route):-
|: route(B,A,[B],Route).
|: route(A,B,Route,[B|Route]):-
|: trainConnection(A,B),
|: \+ member(B,Route).
|: route(A,C,SoFar,Route):-
|: trainConnection(A,B),
|: \+ member(B,SoFar),
|: route(B,C,[B|SoFar],Route).
|: ^D% user://3 compiled 0.00 sec, 12 clauses
true.
?- route(forbach,metz,Route).
Route = [forbach, freyming, stAvold, fahlquemont, metz] .
//10-5
jealous(X,Y):- loves(X,Z), loves(Y,Z).
按实践环节的要求完成操作!