Prolog Copy Lists

im trying to grab some basic prolog but struggling a bit. in the specific one, im trying to get a list of elements and copy it, by elements, to a new list. I can make him turn back, but I found that he does it without a reverse.

I am trying to do the following -

copy(L,R) :- accCp(L,R).

accCp([],R).
accCp([H|T],R) :- accCp(T,H).

When I run a trace on this - I can see that the individual elements are copied, but they are "lost" and do not form a growing list (on R, as I hoped). How could I do this?

Many thanks

+3
source share
1 answer

, , . H L R:

copy(L,R) :- accCp(L,R).
accCp([],[]).
accCp([H|T1],[H|T2]) :- accCp(T1,T2).

copy, , R . , , head H [H|T1] [H|T2]. , , R L.

+6

Source: https://habr.com/ru/post/1725791/


All Articles