Bratko, Chapter3: Lists, Operators and Arithmetic - lists - e.g., [] empty list [a,b,c] [bob,tom,jim] - elements of lists are any valid Prolog data object - lists built using the .(Head,Tail) functor (i.e., cons in Lisp) - e.g., [a,b,c] = .(a,.(b,.(c,[]))) - vetical bar | notation - e.g., [a,b,c] = [a|[b,c]] = [a|[b|[c]]] = [a|[b|[c|[]]]] [a,b|[c]] = [a,b,c|[]] - operations on lists - e.g., member(X,L) :- L = [X|_]. member(X,L) :- L = [A|B], member(X,B). OR member(X,[X|_]). member(X,[_|B]) :- member(X,B). - append(L1,L2), cons(X,L), remove(X,L), sublist(L1,L2), permute(L1,P) - operators - used for shorthand (more readable) notation - e.g., +, -, *, /, |, ., :-, ... - bob likes pam ---> likes(bob,pam) - pam likes tom and tom likes pam --> and(likes(pam,tom),likes(tom,pam)) - :- op(Precedence,Type,Name). - Precedence in [1,1200] [see Fig 3.8 for typical ops] - higher precedence ops appear further out in expression - lower precedence bind first - Type is one of: xfx, xfy, yfx, fx, fy, xf, yf - x: argument precedence less than operator precedence - y: argument precedence less than or equal to operator precedence - Name is an atom - e.g., :- op(600,xfy,and). :- op(500,xfx,likes). - (a and b and c) --> and(a,and(b,c)) not and(and(a,b),c) - a likes b and c likes d --> and(likes(a,b),likes(b,c)) - arithmetic - +, -, *, /, mod - assignment 'is' not '=' (unification) - >, <, >=, =<, =:= (equal), =\= (not equal) - examples: gcd, length Examples - factorial - member - square root - nth [HW1] - monkey and banana [fig2.14] - augment to get solution [HW1]