صفحه 1:
Recursion and Lists member(X,[Y|_] ) :- X = ۰ member(X, [ _ | Y]) :- member(X, Y). It would be easier to write this as: member(X,[X| _ ]). member(X, [_ | Y]) :- member(X, Y). ?- member(1, [3,4,5,8,1,9]). Yes ?- member(X, [prolog, c, ada, haskell]). X= prolog; وه

صفحه 2:
Other Examples ?- alter([you,are,a,computer],R). R = [i, [am, not], a, computer) Yes ?- alter([you,are,french],R). R = [i, [am, not], australian] Yes ange(you, i). ange(are, [am, not]). ange(french, australian). /* catchall */

صفحه 3:
asserta/1. assertz/1 assert/1 retract/1 retractall/1 Example: assertz(fib(N,F)).

صفحه 4:
:-dynamic fib fib(1,1). fib(2,1). fib(N,F) :- N= 2; N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), Fis Fl + F2, asserta(fib(N,F)). ?- fib(8, F). F=21 ?- fib(6,F). F=8

صفحه 5:
ae £(5) + 14 £(3) £(3) + + 0 0 13 ae ‏ص‎ iy fe uo (2) 0 1 1 1 1 2 be 1 1

صفحه 6:
asserta/1. assertz/1 assert/1 retract/1 retractall/1 Example: assertz(fib(N,F)).

صفحه 7:
?- animal(dog). Yes ?- listing(animal). :- dynamic animal/1. animal(dog). animal(tiger). animal(lion). animal(monkey). animal(A) :- mamal(A), :dynamic animal/1. %A directive. animal(tiger). animal(lion). animal(monkey). animal(X):-mamal(X), asserta(animal(X)). mamal(cat). mamal(dog).

صفحه 8:
Finding the length of a list

صفحه 9:
~ Exit: (10) 2 is 1+1 ? creep ۱ Exit: (9) leng([4, 5], 2)? creep } ~ Call: (9) _G485 is 2+1 ? creep ‘ ~ Exit: (9) 3 is 2+1 ? creep Exit: (8) leng([3, 4, 5], 3) ? creep ۱ >< Call: (8) 06488 is 3+1 ? creep ۱ ~ Exit: (8) 4 is 3+1 ? creep Exit: (7) leng([2, 3, 4, 5], 4) ? creep >< Call: (7) G405 is 4+1 ? creep ~ Exit: (7) 5 is 4+1 ? creep Exit: (6) leng([1, 2, 3, 4, 5], 5) ? creep [trace] ?- leng([1,2,3,4,5],Ln). Call: (6) leng([1, 2, 3, 4, 51, _G405)?creep Call: (7) leng([2, 3, 4, 5], _G474) ? creep Call: (8) leng([3, 4, 5], G474) ? creep Call: (9) leng([4, 5], G474) ? creep Call: (10) leng([5], G474) ? creep Call: (11) leng({], _G474) ? creep Exit: (11) leng([], 0) ? creep >< Call: (11) 06479 is 0+1 ? creep

صفحه 10:
.emoving an element from a list remove(X, [ ], [ ]). remove(X, [X|T], C):- remove(X,T,C). remove(X, [H|T], [H|T1]):- X \= H, remove(X,T,T1). ?- remove(1,[4,9,8,7,1,9,7,5,11,R). R= (4, 9, 8, 7, 9, 7, 5]

صفحه 11:
Insertion Sort isort([], []). isort([H|T],R):- isort(T,Q), ins(H,Q,R). ins(X, [], [x)). ins(X, [H|T], [H|R]):- X>H, ins(X,T,R). ins(X, [H|T], [X,H|T])):- X=<H. ?- isort([4,1,9,5,8,3,2],S). S =[1, 2, 3, 4, 5, 8, 9] ?- isort([a,b,c,d],S). ERROR: Arithmetic: *c/0' is not a function

صفحه 12:
1/0

صفحه 13:
SBA SP, A full-stop must follow a term to be read ?- read(X). |: massey. X = massey es ?- read(X). px X=_G185 es ?- read(X). |: likes(Giohn,mary). X = likes(john, mary) es ?- read(X), name(X, List). |: massey. X = massey List = [109, 97, 115, 115, 101, 121]

صفحه 14:
2- write(‘hello world’). hello world Yes ?- write(‘hello world'),nl,nl,tab(9),write(‘I am here :)'). hello world IT am here :) Yes 2- write("abc"). [97, 98, 99] Yes 2- write(‘abc'). abc Yes

صفحه 15:
?- read(X),nl,write(‘Here is what was entered: ‘),write(X). |: massey. Here is what was entered: massey X = massev predicate for printing a list printlst([ ]) :- put('.'), nl. printlst([H|T]) :- - write(H), tab(1), ?- write([‘This’,is,a,list]). [This, is, a, list] ?- printlst([‘This’,is, a,list]). This is a list .

صفحه 16:
male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). is brother _of(X,Y):-male(X),parents(X, Father, Mother),parents(Y, Father,Mother), 26 =Y,write(X),tab(1),write(‘is'),tab(1), write(Y),write('s sister.'). is_sister_of(X,Y):-female(X),parents(X, Father, Mother),parents(Y, Father, Mother). X\ ?- is sister_of(X,Y). te(Y),write('s sister.'). jennifer is greg’s sister. X = jennifer Y = greg; jennifer is andrew’s sister. X = jennifer

صفحه 17:
?- likes(mary,X). X= john Yes ?- likes(mary,X), fail. No ?- likes(mary,X),write(X),nl, fail. john fruit book No ?- likes(mary,X),write(X),nl,fail ; true. john Fruit

صفحه 18:
2- likes(mary,X), write(‘Mary likes '), write(X),put(’."), nl, fail. Mary likes john. Mary likes fruit. Mary likes book. No ?- likes(mary,X),write(X),nl,fail;write(‘reached the OR part’). john fruit book reached the OR part X=_G471 Yes

صفحه 19:
?- write('Enter a number X to be raised to the power of 5: '), read(N), | P5 is N**5,write(N),write(' to the power of 5 is: ‘),write(P5). Enter a number X to be raised to the power of 5: ۳ ‘ ۱ 2 to the power of 5 is: 32 | N=2 P5 = 32 ?- repeat,write(‘Enter a number X to be raised to the power of 5: '), | read(N), P5 is N**5,write(N),write(' to the power of 5 is: (۶

صفحه 20:

صفحه 21:
Prolog accepts input from an input stream send output to an output stream Streams can be files or I/O devices Active streams are the keyboard and the screen by default (user) We may only have one stream open for input and one open for output

صفحه 22:
tell(Filename) Opens the file named by Filename for writing and makes it the current output stream. It creates the file if it does not exist. telling(X) Retrieves the current input stream. told Closes the file onened by the built-in see(Filename) Opens the file named by Filename for input and makes it the current input straeam. seeing(X) Retrieves the current input stream. seen Closes the file opened by the built-in

صفحه 23:
?- tell('D:/test.txt'). Yes ?- telling(X). X = '$stream'(1996248) Yes ?- write(‘testing tell’). Yes ?- told. Yes telling(Old),tell(‘D:/test.txt’),write(‘testing’), told, tell(Old). 23

صفحه 24:
likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary, fruit). likes(john,book). likes(mary,book). likes(tom,john). ?- tell(‘Iksout.txt’),likes(mary,X), write(‘Mary likes ‘),write(X), put(’.’),nl, fail; told. X = _G662 Wes tents of the file “Iks.tkt” Mary likes john. Mary likes fruit. Mary likes book.

صفحه 25:
Eliminates choices Always succeeds but stops backtracking a:-b,c,!,d. a:-e,f. max(X,Y,Y) :- Y>X. max(X,Y,Y) -- Y>X, |. max(X,Y,X). ?- max(1,2,X). ‏و‎ ‎227 x ae ‏فك‎ ‎X=1; No , No ? 7 25

صفحه 26:
suilt-in predicate cut, ۳ » Cut makes programs less readable. » Using cut can make your programs mor efficient. » You have to be careful when using cut. may alter the way your program behaves. When use cut in a rule, you have to be certain of how y rule will be used. If you are not careful your progr may behave strangely. 26

صفحه 27:
Using member with cut we only get one value for X, which is not what we expect. member1(X,[X|_]). member1(X,[_|TI):-member1(X,1). ?- trace. Yes [trace] ?- member1(X,[1,2,3,4]). Call: (6) member1(_G401, [1, 2, 3, 4]) ? creep Exit: (6) member1(1, [1, 2, 3, 4]) ? creep Re iy Fail > 5 ‘ | 6) member1(1, [1, 2, 3, 4]) ? Unexpected Member without cut membernocut(X,[X|_]). membernocut(X,[_|T]):- membernocut(X,T). ?- membernocut(X,[1,2,3,4]).

صفحه 28:
membernocut(X,[X{_]). membernocut(X,[_|T]. Using member without 0 predicate when we know that it will fail. ?- trace. Yes [trace] ?- membernocut(1,[1,2,3,4]), write(‘member succeeded’), fail. Exit: (7) membernocut(1, [1, 2, 3, 4]) ? creep member succeeded Redo: (7) membernocut(1, [1, 2, 3, 4]) ? creep Call: (8) membernocut(1, [2, 3, 4]) ? creep Call: (9) membernocut(1, [3, 4]) ? creep Call: (10) membernocut(1, [4]) ? creep Call: (11) membernocut(1, []) ? creep Fail: (11) membernocut(1, []) ? creep (10) membernocut(1, [4]) ? creep : (9) membernocut(1, [3, 4]) ? creep (8) membernocut(1, [2, 3, 4]) ? creep Fail: (7) membernocut(1, [1, 2, 3, 4]) ? creep Fail

صفحه 29:
29 Using member with cut: member (X,[X|_]) member1(X,[_|T)):- <- 200. member (X,T). Yes [trace] ?- member1(1,[1,2,3,4]),write(member succeeded’), fail. Call: (7) member1(1, [1, 2, 3, 4]) ? creep Exit: (7) member1(1, [1, 2, 3, 4]) ? creep member succeeded Fail: (7) member1(1, [1, 2, 3, 4]) ? creep No 2

صفحه 30:
g not instead of cut makes your program more cl€& member1 (X,[X|_]):-! member1 (X,[_|T]):-member1(X,T). member(X, [X|T]). member(X, [H|T]) :- not(X=H), member(X, T). 30

صفحه 31:
male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). is brother_of(X,Y):-male(X),parents(X, Father, Mother),parents(Y, “ather, Mother),parents(Y, Father, 5 ?- male(X), X = andrew ; No 31 Father, Mother), X\= ?- male(X).

صفحه 32:
male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). is_brother_of(X,Y):-male(X),!,parents(X, Father, Mother), parents(y, Father,Mother), X\=Y. is_sister_of(X,Y):-female(X),!,parents(X, Father, Mother),parents(y, Father, Mother),X\=Y.

صفحه 33:
apend(T1,L2,T2). 4], G409) ? creep 4], (1, 2, 3, 41)? apend({],L,L). apend([{H|T1],L2,[H|T2]):-apend(T1,L2,T2). [trace] ?- apend((],[1,2,3,41,L). Call: (6) apend({], [ Exit: (6) apend({], [1, 2, creep L=[1, 2,3, 4]; Fail: (6) apend({], [1, 2, 3, 4], G409) ? creep No apend([],L,L):-!. apend([H|T1],L2,[H|T2]):- [trace] ?- apend({],[1,2,3,4],L). Call: (6) apend({], [1, 2, 3, 4], G409) ? creep Exit: (6) apend([], [1, 2, 3, 4], [1, 2, 3, 4]) ? creep T= [hy 253,41; Fail: (6) apend({], [1, 2, 3, 4], [1, 2, 3, 41) ? creep No

صفحه 34:
consult(Filename) :- see(Filename), repeat, read(X), assert(X), X=end_of_file, !, seen. ?- tell(‘fam.pl’), listing(male/1), told. SoYAents of the file fam male(andrew). male(john). male(george). male(greg). male(adam).

صفحه 35:
?-male(X). —?- female(X). male(andrew). X= andrew; X= mary; male(john). X= john; ennifer; 5 male(george). X= george; X= eve; SS male(greg). X= greg; nee male(adam). X= adam; female(mary). ‏ی‎ ‎female(jennifer). ?- parents(X, adam, eve). female(eve). X= greg; parents(john,george,ma X= jennifer; ry). X= andrew; parents(greg,adam,eve). a parents(jennifer, ?- findall(X, male(X), List). adam,eve). List= [andrew, john, george, greg, ada parents(andrew, ?- findall(X, female(X), List). adam,eve). List= [mary, jennifer, eve] ?- findall(X, parents(X,adam,eve), List). List= [greg, jennifer, andrew] oy

صفحه 36:
male(andrew). male(john). male(george) male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john, george, mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). ?- findall(t(X,Y),parents(X,Y,Z),Li X = _G353 G354 G358 List = [t(Gjohn, george), t(greg, adam), t(jennifer, adam), t(andrew, adam)] Yes 2» findall(t(X,Y),append(X,Y, [a,b]),L). X= 68 Y= 69 1 [t((1,[a,b]),t(Lal, [b)),t(La,b], ves

صفحه 37:
?- call(a=a). Yes ?- [user]. 9 3 2 |: likes(mary,joha). ?- call(likes(X,Y)). ‏مه‎ ‎I: ‏سجن‎ 2 % user compiled 0.02 sec, Y = john 152 bytes Yes Yes 2- call(likes(X,mary)). ?- call(likes(mary,john)). No Yes a 2- call(likes(mary,X)). ۳ 2 > ‏حطمز‎ ‎Yes ‎37

صفحه 38:
not(likes (mary,john)) No ?- not(likes(mary,X)). No 2 not(likes(mary,georg e)). Yes 7

صفحه 39:
rolog, programs and data are made of terms. A term may bi 4 constant (an atom or a number) 10, ‘abc’, likes, mary 1 variable ۹ ۹ ١ compound term likes(mary,john), likes(? ‏بر تحص‎ are several built-in predicates that succeed or fail dependin e of the term their argument is. COR atomic(X) Succeeds if X is bound to an atom, a string ora number (integer or floating point). atom(X) Succeeds if X is bound to an atom. 2 number(X) Succeeds if X is bound to a number (integer or float) float(X) Succeeds if X is bound to a floating point number

صفحه 40:
2- atom(1). No 2- atom(1.2). No ?- atomic(1.2). Yes ?- number(5). Yes ?- number(1.2). Yes ?- float(1). No ?- float(1.1). Yes ?- atom(a). Yes ?- atom(X). No 2- X=a,atom(X). X=a Yes 2- X=a,atomic(X). X=a Yes ?- atomic(a). Yes ?- atomic(likes). Yes ?- atom(likes). Yes

صفحه 41:
9-1-2 [1,2,3,X,2],write(L),count(2,L,N). [1, 2, 3, _G368, 2] L= [1, 2,3, 2, 2) X=2 N=3 % incorrect Yes 2 ?- count(1,[1,2,3,1],N). N=2 Yes ?- count(1,[1,X,Y,Z],N). 26 < 1 Y=1 Z=1 N=4 % incorrect Yes 2

صفحه 42:
Soluti count(_,[],0). count(Atom,[Head | Tail], N):- atomic(Head), Atom=Head, count(Atom,Tail,N1), Nis N1 + 1;count(Atom,Tail,N). las ?- count(1, ?L= [1,2,3,11,N). (1,2,3,X,2],write(L),count(2,L,N) N=2 : [233 ec371 21 Yes ?- count(1, ([LXY,Z1N). L=[1, 2, 3, _G371, 2] X = _G269 X =_G371 Y= 22 N=2 Yes 15

Recursion and Lists member(X,[Y| _ ] ) :- X = Y. member(X, [ _ | Y]) :member(X, Y). It would be easier to write this as: member(X,[X| _ ]). member(X, [ _ | Y]) :member(X, Y). ?- member(1, [3,4,5,8,1,9]). Yes ?- member(X, [prolog, c, ada, haskell]). X= prolog; 1 Other Examples ?- alter([you,are,a,computer],R). R = [i, [am, not], a, computer] Yes ?- alter([you,are,french],R). R = [i, [am, not], australian] Yes ?- hange(you, i). hange(are, [am, not]). hange(french, australian). hange(do, no). hange(X, X). /* catchall */ lter([ ], [ ]). lter([H|T], [X|Y]) :- change(H, X), alter(T,Y). 2 asserta/1. assertz/1 assert/1 retract/1 retractall/1 Example: assertz(fib(N,F)). 3 :-dynamic fib fib(1,1). fib(2,1). fib(N,F) :N > 2, N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), F is F1 + F2, asserta(fib(N,F)). ?- fib(8, F). F = 21 ?- fib(6,F). F=8 4 F(6) + f(5) + f(4) + f(3) + f(3) + f(2)f(2) f(2) 1 f(1) 1 1 f(4) + 1 f(3) + f(1) 1 f(2) 1 f(2) f(1) 1 1 5 asserta/1. assertz/1 assert/1 retract/1 retractall/1 Example: assertz(fib(N,F)). 6 :-dynamic animal/1. directive. %A animal(tiger). animal(lion). animal(monkey). ?- animal(dog). animal(X):-mamal(X), Yes asserta(animal(X)). ?- listing(animal). :- dynamic mamal(cat). animal/1. animal(dog). mamal(dog). animal(tiger). animal(lion). animal(monkey). animal(A) :mamal(A), 7 Finding the length of a list leng([ ], 0). leng([H|T], Len) :- leng(T, Len1), Len is Len1 + 1. 8 eng([ ], 0). eng([H|T], Len) :- leng(T, Len1), Len is Len1 + 1. [trace] ?- leng([1,2,3,4,5],Ln). Call: (6) leng([1, 2, 3, 4, 5], _G405)?creep Call: (7) leng([2, 3, 4, 5], _G474) ? creep Call: (8) leng([3, 4, 5], _G474) ? creep Call: (9) leng([4, 5], _G474) ? creep Call: (10) leng([5], _G474) ? creep Call: (11) leng([], _G474) ? creep Exit: (11) leng([], 0) ? creep ^ Call: (11) _G479 is 0+1 ? creep ^ Exit: (10) 2 is 1+1 ? creep Exit: (9) leng([4, 5], 2) ? creep ^ Call: (9) _G485 is 2+1 ? creep ^ Exit: (9) 3 is 2+1 ? creep Exit: (8) leng([3, 4, 5], 3) ? creep ^ Call: (8) _G488 is 3+1 ? creep ^ Exit: (8) 4 is 3+1 ? creep Exit: (7) leng([2, 3, 4, 5], 4) ? creep ^ Call: (7) _G405 is 4+1 ? creep ^ Exit: (7) 5 is 4+1 ? creep Exit: (6) leng([1, 2,9 3, 4, 5], 5) ? creep Removing an element from a list remove(X, [ ], [ ]). remove(X, [X|T], C):remove(X,T,C). remove(X, [H|T], [H|T1]):- X \= H, remove(X,T,T1). ?- remove(1,[4,9,8,7,1,9,7,5,1],R). R = [4, 9, 8, 7, 9, 7, 5] 10 Insertion Sort isort([], []). isort([H|T],R):- isort(T,Q), ins(H,Q,R). ins(X, [], [X]). ins(X, [H|T], [H|R]):- X>H, ins(X,T,R). ins(X, [H|T], [X,H|T]):- X=<H. ?- isort([4,1,9,5,8,3,2],S). S = [1, 2, 3, 4, 5, 8, 9] ?- isort([a,b,c,d],S). ERROR: Arithmetic: `c/0' is not a function 11 I/O 12 A full-stop must follow a term to be read ?- read(X). |: massey. X = massey Yes ?- read(X). |: Y. X = _G185 Yes ?- read(X). |: likes(john,mary). X = likes(john, mary) Yes ?- read(X), name(X,List). |: massey. X = massey List = [109, 97, 115, 115, 101, 121] 13 ?- write('hello world'). hello world Yes ?- write('hello world'),nl,nl,tab(9),write('I am here :)'). hello world I am here :) Yes ?- write("abc"). [97, 98, 99] Yes ?- write('abc'). abc Yes 14 ?- read(X),nl,write(‘Here is what was entered: ‘),write(X). |: massey. Here is what was entered: massey X = massey predicate for printing a list printlst([ ]) :- put('.'), nl. printlst([H|T]) :- write(H), tab(1), printlst(T). ?- write([‘This’,is,a,list]). [This, is, a, list] ?- printlst([‘This’,is, a,list]). This is a list . 15 male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). is_brother_of(X,Y):-male(X),parents(X, Father, Mother),parents(Y, Father,Mother), X\=Y,write(X),tab(1),write('is'),tab(1),write(Y),write('s sister.'). is_sister_of(X,Y):-female(X),parents(X, Father, Mother),parents(Y, Father, Mother), ?- is_sister_of(X,Y). X\=Y,write(X),tab(1),write('is'),tab(1),write(Y),write('s sister.'). jennifer is greg`s sister. X = jennifer Y = greg ; jennifer is andrew`s sister. X = jennifer 16 Y = andrew uilt-in predicates “fail” and “true” ail always fails and true always succeeds likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book). likes(tom,john). ?- likes(mary,X). X = john Yes ?- likes(mary,X),fail. No ?- likes(mary,X),write(X),nl,fail. john fruit book No ?- likes(mary,X),write(X),nl,fail ; true. john Fruit OR book 17 X = _G354 Output predicates do not re-succeed on backtracking Yes likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book). likes(tom,john). ?- likes(mary,X), write('Mary likes '), write(X),put('.'), nl, fail. Mary likes john. Mary likes fruit. Mary likes book. No ?- likes(mary,X),write(X),nl,fail;write('reached the OR part'). john fruit book reached the OR part X = _G471 18 Yes uilt-in predicate “repeat” write('Enter a number X to be raised to the power of 5 '), read(N), P5 is N**5,write(N),write(‘ to the power of 5 is: ?- write('Enter '),write(P5). a number X to be raised to the power of 5: '), read(N), | P5 is N**5,write(N),write(' to the power of 5 is: '),write(P5). Enter a number X to be raised to the power of 5: |: 2. 2 to the power of 5 is: 32 N=2 P5 = 32 Yes ?- repeat,write('Enter a number X to be raised to the ?power of 5: '), | read(N), P5 is N**5,write(N),write(' to the power of 5 19 is: '), File I/O 20 Prolog accepts input from an input stream send output to an output stream Streams can be files or I/O devices Active streams are the keyboard and the screen by default (user) We may only have one stream open for input and 21 one open for output tell(Filename) Opens the file named by Filename for writing and makes it the current output stream. It creates the file if it does not exist. telling(X) Retrieves the current input stream. told Closes the file opened by the built-in see(Filename) predicate tell. Opens the file named by Filename for input and makes it the current input straeam. seeing(X) seen Retrieves the current input stream. 22 Closes the file opened by the built-in ?- tell('D:/test.txt'). Yes ?- telling(X). X = '$stream'(1996248) Yes ?- write('testing tell'). Yes ?- told. Yes Contents of D:/test.txt testing tellend_of_file telling(Old),tell(‘D:/test.txt’),write(‘testing’), told,tell(Old). 23 likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book). likes(tom,john). ?- tell(‘lksout.txt’),likes(mary,X), write(‘Mary likes ‘),write(X), put(‘.’),nl, fail; told. X = _G662 Yes Contents of the file “lks.txt” Mary likes john. Mary likes fruit. Mary likes book. 24 Cut: ! Eliminates choices Always succeeds but stops backtracking a:-b,c,!,d. a:-e,f. max(X,Y,Y) :- Y>X. max(X,Y,X). ?- max(1,2,X). X=2; X=1; No ?- max(X,Y,Y) :- Y>X, !. max(X,Y,X). ?- max(1,2,X). X=2; No ?- 25 Built-in predicate cut, “!”  Cut makes programs less readable.  Using cut can make your programs more efficient.  You have to be careful when using cut. Cut, may alter the way your program behaves. When you use cut in a rule, you have to be certain of how your rule will be used. If you are not careful your program may behave 26 strangely. Unexpected Member without cut results: Using member with cut we only get one value for X, which is not what we expect. membernocut(X,[X|_]). membernocut(X,[_|T]):membernocut(X,T). member1(X,[X|_]):-!. ?- membernocut(X,[1,2,3,4]). ?- trace. member1(X,[_|T]):-member1(X,T). X=1; Yes X=2; X=3; [trace] ?- member1(X,[1,2,3,4]). Call: (6) member1(_G401, [1, 2, 3, 4]) ? creep Exit: (6) member1(1, [1, 2, 3, 4]) ? creep X=4; X=1; No 27 Fail: (6) member1(1, [1, 2, 3, 4]) ? Efficiency: membernocut(X,[X|_]). membernocut(X,[_|T]):membernocut(X,T). Using member without cut here, we are wasting time trying out the second part of the predicate when we know that it will fail. ?- trace. Yes [trace] ?- membernocut(1,[1,2,3,4]),write('member Call: (7) membernocut(1, [1, 2, 3, 4]) ? creep succeeded'),fail. Exit: (7) membernocut(1, [1, 2, 3, 4]) ? creep member succeeded Redo: (7) membernocut(1, [1, 2, 3, 4]) ? creep Call: (8) membernocut(1, [2, 3, 4]) ? creep Call: (9) membernocut(1, [3, 4]) ? creep Call: (10) membernocut(1, [4]) ? creep Call: (11) membernocut(1, []) ? creep Fail: (11) membernocut(1, []) ? creep Fail: (10) membernocut(1, [4]) ? creep Fail: (9) membernocut(1, [3, 4]) ? creep Fail: (8) membernocut(1, [2, 3, 4]) ? creep Fail: (7) membernocut(1, [1, 2, 3, 4]) ? creep No 28 Using member with cut: member1(X,[X|_]):-!. member1(X,[_|T]):?- trace. member1(X,T). Yes [trace] ?- member1(1,[1,2,3,4]),write('member succeeded'),fail. Call: (7) member1(1, [1, 2, 3, 4]) ? creep Exit: (7) member1(1, [1, 2, 3, 4]) ? creep member succeeded Fail: (7) member1(1, [1, 2, 3, 4]) ? creep No ?- 29 ng not instead of cut makes your program more clear member1(X,[X|_]):-!. member1(X,[_|T]):-member1(X,T). member(X, [X|T]). member(X, [H|T]) :- not(X=H), member(X, T). 30 male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). is_brother_of(X,Y):-male(X),parents(X, Father, Mother),parents(Y, Father,Mother), X\=Y. is_sister_of(X,Y):-female(X),parents(X, Father, Mother),parents(Y, Father, ?- male(X). Mother),X\=Y. X= X= X= X= X= No ?- andrew ; john ; george ; greg ; adam ; ?- male(X), ! . X = andrew ; No 31 male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). is_brother_of(X,Y):-male(X),!,parents(X, Father, Mother),parents(Y, Father,Mother), X\=Y. is_sister_of(X,Y):-female(X),!,parents(X, Father, Mother),parents(Y, Father, Mother),X\=Y. 32 apend([],L,L). apend([H|T1],L2,[H|T2]):-apend(T1,L2,T2). [trace] ?- apend([],[1,2,3,4],L). Call: (6) apend([], [1, 2, 3, 4], _G409) ? creep Exit: (6) apend([], [1, 2, 3, 4], [1, 2, 3, 4]) ? creep L = [1, 2, 3, 4] ; Fail: (6) apend([], [1, 2, 3, 4], _G409) ? creep No apend([],L,L):-!. apend([H|T1],L2,[H|T2]):- apend(T1,L2,T2). [trace] ?- apend([],[1,2,3,4],L). Call: (6) apend([], [1, 2, 3, 4], _G409) ? creep Exit: (6) apend([], [1, 2, 3, 4], [1, 2, 3, 4]) ? creep L = [1, 2, 3, 4] ; Fail: (6) apend([], [1, 2, 3, 4], [1, 2, 3, 4]) ? creep No 33 Definition of “consult” consult(Filename) :- see(Filename), repeat, read(X), assert(X), X=end_of_file, !, seen. ?- tell(‘fam.pl’), listing(male/1), told. Yes Contents of the file fam male(andrew). H male(john). male(george). male(greg). male(adam). 34 findall(X,Term,List). ?- male(X). X= andrew; X= john; X= george; X= greg; X= adam; ?- female(X). X= mary; X= jennifer; X= eve; male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). ?- parents(X, adam, eve). female(eve). X= greg; parents(john,george,ma X= jennifer; ry). X= andrew; parents(greg,adam,eve) . ?- findall(X, male(X), List). List= [andrew, john, george, greg, ada parents(jennifer, ?- findall(X, female(X), List). adam,eve). List= [mary, jennifer, eve] parents(andrew, ?- findall(X, parents(X,adam,eve), List). adam,eve). List= [greg, jennifer, andrew] 35 Other examples male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). ?- findall(t(X,Y),parents(X,Y,Z),List). X = _G353 Y = _G354 Z = _G358 List = [t(john, george), t(greg, adam), t(jennifer, adam), t(andrew, adam)] Yes ??- findall(t(X,Y),append(X,Y, [a,b]),L). X = _68 Y = _69 L = [t([],[a,b]),t([a],[b]),t([a,b], [])] yes 36 call(Goal) Invoke Goal as a goal. ?- call(a=a). Yes ?- [user]. |: likes(mary,john). |: ?- call(likes(X,Y)). X = mary % user compiled 0.02 sec, 152 bytes Y = john Yes ?- call(likes(X,mary)). ?- call(likes(mary,john)). No Yes ?- call(likes(mary,X)). Yes ?- X = john Yes 37 not(Goal) Succeeds when Goal fails. ?not(likes(mary,john)) . No ?- not(likes(mary,X)). No ?not(likes(mary,georg e)). Yes Implementation of not using call ?- not(P):-call(P),!,fail. not(P). 38 rolog, programs and data are made of terms. A term may be a constant (an atom or a number) 10, ‘abc’, likes, mary a variable X, Y a compound term likes(mary,john), likes(X are several built-in predicates that succeed or fail dependin pe of the term their argument is. atomic(X) Succeeds if X is bound to an atom, a string or a number (integer or floating point). atom(X) Succeeds if X is bound to an atom. number(X) Succeeds if X is bound to a number (integer or float) float(X) Succeeds if X is bound to a floating point number 39 ?- atom(a). Yes ?- atom(X). No ?- X=a,atom(X). X=a Yes ?- X=a,atomic(X). X=a Yes ?- atomic(a). Yes ?- atomic(likes). Yes ?- atom(likes). Yes ?- atom(1). No ?- atom(1.2). No ?- atomic(1.2). Yes ?- number(5). Yes ?- number(1.2). Yes ?- float(1). No ?- float(1.1). Yes 40 example unting the number of occurances of an atom nt(Atom,List,N). nt( _ , [ ] , 0 ). nt(Atom, [Atom | Tail] , N):- count(Atom,Tail,N1), N is N1 + 1. nt(Atom,[ _ | Tail], N):- count(Atom,Tail,N). ?- count(1,[1,2,3,1],N). N=2 Yes ?- count(1,[1,X,Y,Z],N). X=1 Y=1 Z=1 N=4 % incorrect Yes ?- ?- L= [1,2,3,X,2],write(L),count(2,L,N). [1, 2, 3, _G368, 2] L = [1, 2, 3, 2, 2] X=2 N=3 % incorrect Yes ?- 41 Soluti on count( _ , [ ] , 0 ). count(Atom,[Head | Tail], N):- atomic(Head), Atom=Head, count(Atom,Tail,N1), N is N1 + 1;count(Atom,Tail,N). ?- count(1, [1,2,3,1],N). N=2 Yes ?- L= [1,2,3,X,2],write(L),count(2,L,N) . [1, 2, 3, _G371, 2] ?- count(1, [1,X,Y,Z],N). L = [1, 2, 3, _G371, 2] X = _G269 X = _G371 Y = _G272 N=2 Z = _G275 Yes N=1 ?- Yes 42

51,000 تومان