140 likes | 244 Vues
Comparison. X = Y True if X equals to Y. X = Y True if X not equal to Y X < Y True if X is less than Y X > Y True if X is greater than Y
E N D
Comparison Lecture 15 Lists
X = Y True if X equals to Y X \= Y True if X not equal to Y X < Y True if X is less than Y X > Y True if X is greater than Y X =< Y True if X is less than or equal to Y X >= Y True if X is greater than or equal to Y Example: Given two positive integers X and Y, their greatest common devisor “D” is found according to the following: if X = Y then D = X if X < Y then D = greatest common devisor of X and Y – X if X > Y then D = do the same as above with X and Y interchanged greatest common devisor of 6 and 6 is 6 greatest common devisor of 27 and 9 is 9 greatest common devisor of 20 and 15 is 5 Lecture 15 Lists
gcd(X,X,X). gcd(X,Y,D) :- X < Y, Z is Y – X, gcd(X, Z, D). gcd(X,Y,D) :- Y < X, gcd(Y, X, D). We could have written the last clause above, as gcd(X,Y,D) :- Y < X, Z is X – Y, gcd(Z, Y, D). ?- gcd(20,15,D). D = 5 Yes ?- gcd(0,15,D). ERROR: Out of local stack Lecture 15 Lists
Another Example n*(n-1)…2*1 n=1,2,3… n!= 1 n=0 fact(0,1). fact(N,F) :- N > 0, N1 is N – 1, fact(N1, F1), F is N * F1. ?- edit(fact). % d:/prolog/fact.pl compiled 0.00 sec, 64 bytes Yes ?- fact(6,X). X = 720 Yes ?- fact(3,X). X = 6 Yes Lecture 15 Lists
Lists Lecture 15 Lists
The list is the main data structure in Prolog. A list may be empty: [ ] or it may contain one or more terms (constants, variables or structures). [a,b,c,d,e] [5,8,3,9,7] [the, boy, run] A list can be split into a head and a tail: [H|T]. grades(john, [70,87,90,58]). ?- grades(john, [H|T]). H = 70 T = [87,90,58] Lecture 15 Lists
Heads and tails of lists List Head Tail Lecture 15 Lists
?- [a, b, c] = [Head | Tail]. Head = a Tail = [b, c] ?- [the,[boy,run]]=[Head|Tail]. Head = the Tail = [[boy, run]] ?- [a, b, c] = [X, Y | Tail]. X = a Y = b Tail = [c] ?- [a, b, c] = [X|Y,Z]. No Lecture 15 Lists
Converting a structure to a list ?- likes(john,mary)=..X. X = [likes, john, mary] Anything in doubleis equivalent to a list of ASCIIvalues: ?- "abc"=X. X = [97, 98, 99] Lecture 15 Lists
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; X= c X= ada; X= haskell; No Lecture 15 Lists
Other Examples change(you, i). change(are, [am, not]). change(french, australian). change(do, no). change(X, X). /* catchall */ alter([ ], [ ]). alter([H|T], [X|Y]) :- change(H, X), alter(T,Y). ?- alter([you,are,french],R). R = [i, [am, not], australian] Yes ?- alter([you,are,a,computer],R). R = [i, [am, not], a, computer] Yes ?- Lecture 15 Lists
asserta/1. assertz/1 assert/1 retract/1 retractall/1 Example: assertz(fib(N,F)). Lecture 15 Lists
:-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). N = 21 ?- fib(6,F). F = 8 Lecture 15 Lists
+ f(5) f(4) + + f(4) f(3) f(3) f(2) + + + f(3)f(2) f(2) f(1) f(2) f(1) 1 + 1 1 1 1 1 f(2) f(1) 1 1 F(6) Lecture 15 Lists