Next: Recursion
Up: Recursion and lists
Previous: The anonymous variable
Rewrite the definitions of second/2 and tail/2
above to include explicit calls to the unification operator =/2.
Define a predicate fifth/2 that, given a list as first argument,
returns the fifth element of the list as its second argument.
E.g.
|?- fifth([1,2,3,4,5,6,7], X).
X = 5
yes
Recall that every list (the empty list) has both a head and
a tail. Use this fact, and the headtail notation to
define a predicate is_list/1 that returns true if its argument
is a list (including the empty list) and false otherwise.
Define a predicate cons/3, which takes a list as its second
argument, anything as its first argument and returns as its third
argument a new list in with the first argument as head and the
second argument as tail.
Steve Harlow
2001-11-26