Self-test 6, Task 3, Solutions
|
insert_before_pos/4 | ||
This is based on replace_at_pos/4, with the major change being in the terminating condition: /* ************************************************ */ /* */ /* insert_before_pos/4 */ /* Arg 1: Counter: integer */ /* Arg 2: Elem to be added */ /* Arg 3: List1 */ /* Arg 3: List2 is List1 with Elem added */ /* Summary: is true if List2 is List1 with Elem */ /* before the nth position. */ /* Author: P J Hancox */ /* Date: 21 October 1994 */ /* */ /* ************************************************ */ % 1 terminating condition insert_before_pos(1, Elem, [Head|Tail], [Elem, Head|Tail]). % 2 insert_before_pos(Pos, Elem, [Head|Tail1], [Head|Tail2]) :- Pos1 is Pos - 1, insert_before_pos(Pos1, Elem, Tail1, Tail2). |
||