How do I append lists in Prolog?

Viewed 108595

How do I append lists in Prolog? I've searched on the Internet and I found this (from http://www.csupomona.edu/~jrfisher/www/prolog_tutorial/2_7.html)

append([X|Y],Z,[X|W]) :- append(Y,Z,W).  
append([],X,X).

So it gets the Z by removing the elements of [X|Y] in [X|W]. But how do I append two lists together?

Example,

appendlist([1,2],[3,4,5],X).

The result will be X = [1,2,3,4,5].

Also I don't know what happening in the recursion. (I traced it but didn't understand)

EDIT: What I want to know is how it should be coded to function like the predefined append() in Prolog.

2 Answers
Related