|
Self-test 6, Task 1, Solutions
|
| Adding to position/3 | ||
|
The requirement for the position to be zero when the list is empty can be satisfied by adding a fact to the position/3 procedure: /* ************************************************ */ /* */ /* position/3 */ /* Arg 1: Counter: integer */ /* Arg 2: Elem to be found */ /* Arg 3: List with Elem in */ /* Summary: is true if List contains Elem at the */ /* nth position. */ /* Author: P J Hancox */ /* Date: 21 October 1994 */ /* */ /* ************************************************ */ % 1 position(1, Elem, [Elem|_]). % 2 position(0, _, []). % 3 position(Cnt, Elem, [_|Tail]) :- Cnt1 is Cnt - 1, position(Cnt1, Elem, Tail). |
||