Learning Prolog: Lab #8 - findall/3
This week's lab was all about
findall/3, and a little bit more about lists. To summarise,
findall lets you, well, um, find everything that matches a given rule or set of rules. For example, if we have the following dataset:
mountain(everest). mountain(k2). mountain(matterhorn). mountain(kangchenjunga). desert(sahara). desert(gobi). desert(atacama). tall(everest). tall(k2). tall(kangchenjunga).
We could ask Prolog to give us a list of all the mountains in it's world. It's rather like issuing a database with an SQL query, asking it to find all the records that match a set of criteria and return them.
?- findall(Mountain, mountain(Mountain), MountainList). MountainList = [everest, k2, matterhorn, kangchenjunga] . ?- findall(Mountain, (mountain(Mountain), tall(Mountain)), List). MountainList = [everest, k2, kangchenjunga] . ?- findall(Desert, desert(Desert), DesertList). DesertList = [sahara, gobi, atacama]. ?- findall(Place, (desert(Place); mountain(Place)), Places). Places = [sahara, gobi, atacama, everest, k2, matterhorn, kangchenjunga] .
As I demonstrated above, you can combine multiple criteria with a set of brackets and a few commas or semi-colons.
The other thing that we looked at was a rule that would return the last item in a list. Here's what I came up with:
% Stopping condition - stop when we reach the last item lastitem([ LastItem |  ], LastItem). % If we haven't reached the end of the list, recurse on the rest of the list. lastitem([ _ | Rest ], LastItem) :- lastitem(Rest, LastItem).
The above keeps throwing away the first item in the given list until it finds the last item, which it then stores in the variable
LastItem. It knows which the last item is because lists in Prolog are always terminated with an empty list (
). Here's an example of the above in action:
?- findall(Place, (desert(Place); mountain(Place)), Places), lastitem(Places, LastPlace), write('The last place is '), write(LastPlace), write('.'), nl. The last place is kangchenjunga. Places = [sahara, gobi, atacama, everest, k2, matterhorn, kangchenjunga], LastPlace = kangchenjunga .
That brings us to the end of the 8th Prolog lab session. This post feels a bit short, but I'm sure that there will be more to post about next time. If you're interested, here is a link to the Prolog file that I have written whilst writing this post.