The usage of findall you are looking for is findall(+Template, :Goal, -Bag)
(as Guy Coder said: findall/3).
What this means is find each set of variables that satisfy :Goal
as written (in this instance, you are looking for station(X,Line)
), then place them in the -Bag
list (which you call ListOfStations
) using the +Template
(which, for you, is presumably X
, to create [X1,X2,X3|...]
).
Put them all together and you get (spoiler warning, I guess?):
findall(X, station(X,Line), ListOfStations).
Which will output:
[kennington, embankment, tottenhamcourtroad, warrenstreet, euston]
Where the list is ordered by the order of the facts.
Additional Information:
If your template looks like [X]
instead of just X
:
findall([X], station(X,Line), ListOfStations).
Your output looks like this:
[[kennington], [embankment], [tottenhamcourtroad], [warrenstreet], [euston]]
And you could even make it look like (station,X)
to get:
findall((station,X), station(X,Line), ListOfStations).
Giving you:
[(station,kennington), (station,embankment), (station,tottenhamcourtroad), (station,warrenstreet), (station,euston)]
And if you want to find every combination of stations on a line, the :Goal
can be compound as well, like so:
findall((X,Y), (station(X,Line),station(Y,Line),X\=Y), ListOfStations).
Giving you something silly like this:
[(kennington,embankment), (kennington,tottenhamcourtroad), (kennington,warrenstreet), (kennington,euston), (embankment,kennington), (embankment,tottenhamcourtroad), (embankment,warrenstreet), (embankment,euston), (tottenhamcourtroad,kennington), (tottenhamcourtroad,embankment), (tottenhamcourtroad,warrenstreet), (tottenhamcourtroad,euston), (warrenstreet,kennington), (warrenstreet,embankment), (warrenstreet,tottenhamcourtroad), (warrenstreet,euston), (euston,kennington), (euston,embankment), (euston,tottenhamcourtroad), (euston,warrenstreet)]
Which I hope gives you a good idea of how powerful findall is.