Lecture 1
Lecture 2
Lecture 3
Lecture 4
Lecture 5

List processing: computing different return values

Define a predicate all_as(+List) which succeeds if List is a list containing only a's as elements (if it contains any elements at all).

Remark: Maybe you wonder what the + in all_as(+List) is all about. It's a notation that is commonly used in specifications of what particular Prolog predicates do (or should do). The plus expresses that the argument that it is attached to should be instantiated when calling the predicate. Similarly, arguments can be prefixed by a minus to express that the argument should not be instantiated or by a question mark to express that the argument may or may not be instantiated.

Define a predicate replace_a_b_c(+InList,?OutList) where OutList is obtained from InList by replacing all a's in InList with b's, all b's with c's, and all c's with a's.

Define a predicate list_length(+List,?Length) where Length is the number of elements in list List.

Compare the structure of the predicates that you had to define for the above exercises. In all cases, you have to recurse through the list until you reach the empty list. The predicates differ, however, in their return values. Predicate all_as/1 just performs a test on the list which will succeed or fail. No additional return value is computed. Predicate replace_a_b_c/2, on the other hand, maps the input list to an output list of equal length. To obtain the output list, something is done to each element of the input list. Predicate list_length/2, finally, computes one single return value.

Back to the practical session of day 3.