border(sussex,kent). border(sussex,surrey). border(surrey,kent). border(hampshire,sussex). border(hampshire,surrey). border(hampshire,berkshire). border(berkshire,surrey). border(wiltshire,hampshire). border(wiltshire,berkshire). adjacent(X,Y) :- border(X,Y). adjacent(X,Y) :- border(Y,X). affordable(X,Y) :- adjacent(X,Z), adjacent(Z,Y). /* What happens with for the following goals? ?-affordable(wiltshire,sussex). ?-affordable(wiltshire,kent). ?-affordable(hampshire,hampshire). ?-affordable(X,kent). ?-affordable(sussex,X). ?-affordable(X,Y). */
a(g,h). a(g,d). a(e,d). a(h,f). a(e,f). a(a,e). a(a,b). a(b,f). a(b,c). a(f,c). path(X,X). path(X,Y) :- a(X,Z), path(Z,Y). /* What happens for each goal? Does backtracking provide multiple answers? Why? ?-path(f,f). ?-path(a,c). ?-path(g,e). ?-path(g,X). ?-path(X,h). */
member(X,[X|T]). member(X,[H|T]) :- member(X,T). /* 1. In ther first clause, what is the T for? 2. In the second clause, what is the H for? 3. What do the following goals do (what is the first answer if any, and then what are the subsequent answers if any on backtracking? ?-member(john,[paul,john]). ?-member(X,[paul,john]). ?-member(joc,[marx,darwin,freud]). ?-member(foo,X). */ mystery(X,A,B) :- member(X,A), member(X,B). /* 4. What do the following goals do? ?-mystery(a,[b,c,a],[p,a,l]). ?-mystery(b,[b,l,u,e],[y,e,l,l,o,w]). ?-mystery(X,[r,a,p,i,d],[a,c,t,i,o,n]). ?-mystery(X,[w,a,l,n,u,t],[c,h,e,r,r,y]). 5. The first clause of member may be written with an anonymous variable. member(X,[X|_]). How may the second clause of member be written with an anonymous variable?Assignments Spring 05