I am trying to get the foreach/2
loop to work in Prolog (using tkeclipse). I understand it needs an element in the first argument and a list to search for that element within as a second argument.
My problem is that I haven't been able to make it work despite trying to rewrite my code and looking for solutions online.
In the following code, I try to write a predicate visite/2
that takes a list of museums and returns a list of cities where they are located.
Your help would be really appreciated as it would allow me to be better prepared for my upcoming exam.
musee(paris,louvre).
musee(rome,vatican).
musee(madrid,prado).
musee(berlin,kulturforum).
musee(londres,british_museum).
visite([X],L) :-
findall(V,musee(V,_),List),
(
foreach(X,List) do findall(C,musee(C,X),L)
).
This isn't Prolog (at least, not as I know it).
And if it were, the notion of an iterative loop is antithetic to Prolog.
Your problem statement:
could be better done in a way more idiomatic to Prolog. Something like this: