Prolog - List Append

The Append program can be used to append two lists:

append(c(H,T), B, c(H,TB)) <= append(T, B, TB).
append(nil, B, B).

? append( c(1, c(2, nil)), c(3, c(4, nil)), X).

{ Append run Forwards. }




 
The same program can also be run "backwards" to take a list apart:

append(c(H,T), B, c(H,TB)) <= append(T, B, TB).
append(nil, B, B).

? append( X, Y, c(1, c(2, c(3, nil))) ).

{ Append run Backwards. }




In this case there are four pairs of lists, X and Y, which give c(1, c(2, c(3, nil))) when appended.