% lastoflist1.pl:  What is the last element of a list?
%
% This is an interesting example because, thanks to pattern-matching,
% the implementation looks incomplete -- at first glance, it seems
% that at least a couple of important details are missing.
%
% The first fact handles the single-element-list case -- when there's
% only one element, it will unify with the OnlyElem variable, and we'll
% send the element back as the last item.
%
% The rule handles lists of two or more elements.
% The pattern in the rule's head will discard the first element of the
% list, and will pass the tail to itself recursively.  Eventually, the end
% of the list will be reached, and the last element located and passed out
% of the recursion.
%
% The lingering question:  What about the empty list?  Because it won't
% unify with either the fact or the rule, the result will be "false,"
% which is what we want.
%
% This version suffers from Prolog's desire to keep searching when it's
% able, even when we know there's no point.  We'll see a new way to solve
% that problem in the next version.

lastoflist([OnlyElem],OnlyElem).
lastoflist([_|T],Rest) :- lastoflist(T,Rest).
