Self-test 5, Task 3, Solutions

insert_after/4

This requires one change from the code given for replace/4, namely that both elements should be added to the head of the second list when the first element is found in the first list.
     /* ************************************************ */
     /*                                                  */
     /*   insert_after/4                                 */
     /*      Arg 1:  Elem1 to be found                   */
     /*      Arg 2:  List1 with Elem1 in                 */
     /*      Arg 3:  Elem2 to be added                   */
     /*      Arg 4:  List2 with Elem2 in                 */
     /*   Summary: True if List2 is List1 with Elem1     */
     /*            followed by Elem2.                    */
     /*   Author: P J Hancox                             */
     /*   Date:   19 October 1994                        */
     /*                                                  */
     /* ************************************************ */

     % 1 terminating condition
     insert_after(Elem1, [Elem1|Tail], Elem2, [Elem1, Elem2|Tail]).
     % 2 recursive
     insert_after(Elem1, [Head|Tail1], Elem2, [Head|Tail2]) :-
          insert_after(Elem1, Tail1, Elem2, Tail2).