转载自:https://mp.weixin.qq.com/s/5I4tC82Vym4odRLcUAlLyw
当每次输入listing命令时都会有经过系统内部排过序的谓词列出,所以我们在后续的操作中都采用有具体谓词名的命令来列出要查看的谓词。
?- assert(happy(mia)).
true.
?- listing(happy).
:- dynamic happy/1.
happy(mia).
true.
?- assert(happy(vincent)).
true.
?- assert(happy(marcellus)).
true.
?- assert(happy(butch)).
true.
?- assert(happy(vincent)).
true.
?- listing(happy).
:- dynamic happy/1.
happy(mia).
happy(vincent).
happy(marcellus).
happy(butch).
happy(vincent).
true.
?- assert((naive(X):- happy(X))).
true.
?- listing(naive).
:- dynamic naive/1.
naive(A) :-
happy(A).
true.
?- retract(happy(marcellus)).
true.
?- listing(happy).
:- dynamic happy/1.
happy(mia).
happy(vincent).
happy(butch).
happy(vincent).
true.
//少了事实happy(marcellus)
?- retract(happy(vincent)).
true.
?- listing(happy).
:- dynamic happy/1.
happy(mia).
happy(butch).
happy(vincent).
true.
?- retract(happy(X)).
X = mia ;
X = butch ;
X = vincent.
?- listing(happy).
:- dynamic happy/1.
true.
?- listing(naive).
:- dynamic naive/1.
naive(A) :-
happy(A).
true.
?- assert( p(b) ), assertz( p(c) ), asserta( p(a) ).
true.
?- listing(p).
:- dynamic p/1.
p(a).
p(b).
p(c).
true.
?- [user].
|: :- dynamic lookup/3.
|: add_and_square(A, B, C) :-
|: lookup(A, B, C),
|: !.
|: add_and_square(B, C, A) :-
|: A is (B+C)*(B+C),
|: assert(lookup(B, C, A)).
|: ^D% user://1 compiled 0.00 sec, 2 clauses
true.
?- listing(lookup).
:- dynamic lookup/3.
lookup(3, 7, 100).
lookup(3, 4, 49).
true.
?- retract(lookup(X,Y,Z)).
X = 3,
Y = 7,
Z = 100 ;
X = 3,
Y = 4,
Z = 49.
?- listing(lookup).
:- dynamic lookup/3.
true.
?- add_and_square(3,7,X).
X = 100.
?- add_and_square(3,4,X).
X = 49.
?- listing(lookup).
:- dynamic lookup/3.
lookup(3, 7, 100).
lookup(3, 4, 49).
true.
?- retract(lookup(_,_,_)).
true ;
true.
?- [user].
|: child(martha,charlotte).
|: child(charlotte,caroline).
|: child(caroline,laura).
|: child(laura,rose).
|: descend(X,Y) :- child(X,Y).
|: descend(X,Y) :- child(X,Z),
|: descend(Z,Y).
|: ^D% user://2 compiled 0.00 sec, 6 clauses
true.
?- descend(martha,Y).
Y = charlotte ;
Y = caroline ;
Y = laura ;
Y = rose ;
false.
?- findall(X,descend(martha,X),Z).
Z = [charlotte, caroline, laura, rose].
?- findall(fromMartha(X),descend(martha,X),Z).
Z = [fromMartha(charlotte), fromMartha(caroline), fromMartha(laura), fromMartha(rose)].
?- findall(X,descend(mary,X),Z).
Z = [].
?- findall(Child,descend(Mother,Child),List).
List = [charlotte, caroline, laura, rose, caroline, laura, rose, laura, rose|…].
?- bagof(Child,descend(Mother,Child),List).
Mother = caroline,
List = [laura, rose] ;
Mother = charlotte,
List = [caroline, laura, rose] ;
Mother = laura,
List = [rose] ;
Mother = martha,
List = [charlotte, caroline, laura, rose].
?- bagof(Child,Mother^descend(Mother,Child),List).
List = [charlotte, caroline, laura, rose, caroline, laura, rose, laura, rose|…].
?- findall(List, bagof(Child,descend(Mother,Child),List),Z).
Z = [[laura, rose], [caroline, laura, rose], [rose], [charlotte, caroline, laura, rose]].
?- bagof(List,Child^Mother^bagof(Child,descend(Mother,Child),List),Z).
Z = [[laura, rose], [caroline, laura, rose], [rose], [charlotte, caroline, laura, rose]].
?- [user].
|: age(harry,13).
|: age(draco,14).
|: age(ron,13).
|: age(hermione,13).
|: age(dumbledore,60).
|: age(hagrid,30).
|: ^D% user://3 compiled 0.00 sec, 6 clauses
true.
?- findall(X,age(X,Y),Out).
Out = [harry, draco, ron, hermione, dumbledore, hagrid].
?- setof(X,Y^age(X,Y),Out).
Out = [draco, dumbledore, hagrid, harry, hermione, ron].
?- setof(X,age(X,Y),Out).
Y = 13,
Out = [harry, hermione, ron] ;
Y = 14,
Out = [draco] ;
Y = 30,
Out = [hagrid] ;
Y = 60,
Out = [dumbledore].
?- findall(Y,age(X,Y),Out).
Out = [13, 14, 13, 13, 60, 30].
?- setof(Y,X^age(X,Y),Out).
Out = [13, 14, 30, 60].
练习
//11-1
?- assert(q(a,b)), assertz(q(1,2)), asserta(q(foo,blug)).
true.
?- listing(q).
:- dynamic q/2.
q(foo, blug).
q(a, b).
q(1, 2).
true.
?- retract(q(1,2)), assertz( (p(X) :- h(X))).
true.
?- listing(q).
:- dynamic q/2.
q(foo, blug).
q(a, b).
true.
?- listing(p).
:- dynamic p/1.
p(A) :-
h(A).
true.
//11-2
?- [user].
|: q(blob,blug).
Warning: user://4:211:
Warning: Redefined static procedure q/2
|: q(blob,blag).
|: q(blob,blig).
|: q(blaf,blag).
|: q(dang,dong).
|: q(dang,blug).
|: q(flab,blob).
|: ^D% user://4 compiled 0.00 sec, 7 clauses
true. //警告是因为接着练习1做的
?- findall(X,q(blob,X),List).
List = [blug, blag, blig].
?- findall(X,q(X,blug),List).
List = [blob, dang].
?- findall(X,q(X,Y),List).
List = [blob, blob, blob, blaf, dang, dang, flab].
?- bagof(X,q(X,Y),List).
Y = blag,
List = [blob, blaf] ;
Y = blig,
List = [blob] ;
Y = blob,
List = [flab] ;
Y = blug,
List = [blob, dang] ;
Y = dong,
List = [dang].
?- setof(X,Y^q(X,Y),List).
List = [blaf, blob, dang, flab].
//11-3
?- [user].
|: :- dynamic sigmares/2.
|: sigmares(0,0).
|: sigma(Number,Sum):-
|: sigmares(Number,Sum).
|: sigma(Number,Total):-
|: Number > 0,
|: \+ sigmares(Number,Total),
|: NewNumber is Number - 1,
|: sigma(NewNumber,SubTotal),
|: Total is SubTotal + Number,
|: assert(sigmares(Number,Total)).
|: ^D% user://1 compiled 0.00 sec, 3 clauses
true.
?- listing(sigmares).
:- dynamic sigmares/2.
sigmares(0, 0).
true.
?- listing(sigma).
sigma(A, B) :-
sigmares(A, B).
sigma(A, B) :-
A>0,
\+ sigmares(A, B),
C is A+ -1,
sigma(C, D),
B is D+A,
assert(sigmares(A, B)).
true.
?- sigma(3,X).
X = 6 .
?- sigma(5,X).
X = 15 .
?- sigma(2,X).
X = 3 .
?- listing(sigmares/2).
:- dynamic sigmares/2.
sigmares(0, 0).
sigmares(1, 1).
sigmares(2, 3).
sigmares(3, 6).
sigmares(4, 10).
sigmares(5, 15).
true.
?- sigma(3,X).
X = 6 .
?- listing(sigmares/2).
:- dynamic sigmares/2.
sigmares(0, 0).
sigmares(1, 1).
sigmares(2, 3).
sigmares(3, 6).
sigmares(4, 10).
sigmares(5, 15).
true.
按实践环节要求去完成。