Prolog's list notation ([ a, b , ... ]
) is just simple syntactic sugar on top of ordinary prolog terms. The empty list is denoted by the atom []
; a non-empty list is denoted by the term ./2
, where the first argument in the term is the head of the list, and the second the tail of the list, being either the empty list or a non-empty list (.(a,.(b,[]))
being the actual representation of [a,b]
).
See my answer here for a full explanation.
This is the canonical implementation of append/3
:
append( [] , L , L ) .
append( [H|T] , L , [H|R] ) :- append(T,L,R) .
Eliminating the syntactic sugar gives you this:
append( [] , L , L ) .
append( .(H,T) , L , .(H,R) ) :- append(T,L,R) .
That should get you on your way.
Edited to add:
If you you just want to append a single item to a list, it's a simple matter of traversing the list. When you reach the end, you insert it. Something like (using normal list notation):
append_item( X , [] , [X] ) .
append_item( X , [Y|Ys] , [Y|Zs] ) :- append_item(X,Ys,Zs).
or, sans syntactic sugar,
append_item( X , [] , .(X,[]) ) .
append_item( X , .(Y,Ys) , .(Y,Zs) ) :- append_item(X,Ys,Zs).
Edited to note: As noted by @keldan-chapman in the comments, as of SWI Prolog v7+, lists are no longer ./2
for a non-empty list, and the atom []
for the empty list. As of SWI Prolog v7+, a non-empty list is denoted by '[|]'/2
and the empty list by "a special constant which is printed as []
".
Go figure.
For details, see the SWI Prolog documentation at § 5.1, Lists Are Special.