Using recursion an append in prolog

Posted by Adrian on Stack Overflow See other posts from Stack Overflow or by Adrian
Published on 2010-03-28T22:52:06Z Indexed on 2010/03/28 23:03 UTC
Read the original article Hit count: 358

Filed under:
|
|

Lets say that I would like to construct a list (L2) by appending elements of another list (L) one by one. The result should be exactly the same as the input. This task is silly, but it'll help me understand how to recurse through a list and remove certain elements.

I have put together the following code:

create(L, L2) :- (\+ (L == []) -> L=[H|T], append([H], create(T, L2), L2);[]).

calling it by

create([1,2,3,4], L2)

returns

L2 = [1|create([2,3,4], **)\.

which is not a desired result.

© Stack Overflow or respective owner

Related posts about prolog

Related posts about append