List processing: different termination criteria
Define a predicate add_one(+InList,?OutList)
:
InList
is a list of integers and
OutList
is the list that results from adding
1 to each element of InList
.
Hint
Define a predicate contains_0(?List)
which succeeds
of the input list List
contains a 0.
Hint
The two exercises that you just did illustrate two different conditions for terminating the recursion. In the first case, recursion stops when the empty list is reached. In the second case, Prolog stops recursing down the list when it finds an an element with a certain property (in the example, the element has to be 0).
These are common termination criteria. All the predicates that
you had to define in the
previous section stop the recursion when the empty list
is reached. The predicates trans_a_b
and
concatenate
, which we saw in the lecture, also do
so, while the predicate element_of
is like
contains_0
in that it stops recursion when an
element with a particular property has been found.