Search code examples
listcopyprolog

Prolog - copy a piece of list


I need to duplicate list in prolog.

I have list:

L = [a(string1,value1),a(string2,value2),a(string3,value3),a(string4,value4)].

Output will be: L = [string1, string2, string3, string4].

How can I do this?

I can copy whole list by code:

copy([],[]).
copy([H|L1],[H|L2]) :- copy(L1,L2).

I have tried something like:

copy2([],[]).
copy2([H|L1],[K|L2]) :- member(f(K,_),H), copy2(L1,L2). 

But it does not work properly.

But I need only strings from my original list. Can anyone help?


Solution

  • pattern matching is used to decompose arguments: you can do

    copy([],[]).
    copy([a(H,_)|L1],[H|L2]) :- copy(L1,L2).