Just a small question about Prolog. Say I have used the built in predicate findall/3
to obtain a list and used the variable X
as my output.
I'm wondering how I could then use this list in another predicate such as last/2
to find the last element of this list. If you could include a small example too that would help greatly.
First of all, since Prolog aims to be a logic programming programming language, there is nu such thing as output variables.
Nevertheless, say you know a variable
X
is bounded after a certain predicate and you intend to use this value when calling a new predicate, you can use Prolog's logical "and",/2
. I'm putting "and" between quotes because this and differs sometimes from the natural understanding of how "and" in natural language behaves.You can thus use a predicate:
To first find all occurences of
foo/1
, extract the variableA
, put these into a listX
and finally get thelast/2
element ofX
.You can then for instance use this in a defined predicate:
If you run this for instance with:
The results are:
and:
Now the result to obtain the last is:
or: