Next: , Up: Programming Examples

9.11.1 Simple List Processing

The goal concatenate(L1,L2,L3) is true if list L3 consists of the elements of list L1 concatenated with the elements of list L2. The goal member(X,L) is true if X is one of the elements of list L. The goal reverse(L1,L2) is true if list L2 consists of the elements of list L1 in reverse order.

     concatenate([], L, L).
     concatenate([X|L1], L2, [X|L3]) :- concatenate(L1, L2, L3).
     member(X, [X|_]).
     member(X, [_|L]) :- member(X, L).
     reverse(L, L1) :- reverse_concatenate(L, [], L1).
     reverse_concatenate([], L, L).
     reverse_concatenate([X|L1], L2, L3) :-
             reverse_concatenate(L1, [X|L2], L3).

Send feedback on this subject.