In a prolog program as stated below:
town(a).
town(b).
town(c).
town(d).
dam(e).
dam(f).
link(a,b).
link(a,c).
link(c,d).
link(b,d).
link(b,c).
link(c,e).
link(a,e).
link(d,f).
neighbour(X,Y):- link(X,Y) ; link(Y,X).
Would this be the correct procedure all_neighbours(L,X) which returns a list L of all neighbouring towns to X: all_neighbours(L,X):- town(Y), findall(Y, neighbour(X,Y), L).
Would this be the correct procedure has_dam(L) which returns a list L of all towns that have at least one neighbouring dam: has_dam(L):- dam(Y), town(X), findall(X, neighbour(X,Y), L).
Would this be the correct procedure no_dam(L) which returns a list L of all towns that have no neighbouring dam: no_dam(L):- town(X), not dam(Y), findall(X, neighbour(X,Y), L).
Nope, none of these are correct. In the first, the call to
townshould be done within the scope of thefindall:and similarly for the rest. Note the parentheses around the second argument to
findall. These are required, sincewould be parsed as a call to (the probably non-existant)
findall/4.To understand the
findallquery, spell it out: