Reputation: 35
I'll like to append an element to each element of a list. the element must be a list too.
Exemple :
A = [a,b,c]
B = [ele,ele2]
The result would be:
R = [[a,ele,ele2],[b,ele,ele2],[c,ele,ele2]]
I tried
maplist(custom_append,A,B,R).
But it returns false with
custom_append(X,Y,[X|Y]).
How can I achieve this ?
Upvotes: 1
Views: 138
Reputation: 441
The straightforward way would be like this:
append_list([], _, []).
append_list([A|As], B, [[A|B]|Cs]) :-
append_list(As, B, Cs).
Don't even need to use maplist
.
Upvotes: 0
Reputation: 476547
Note that B
is not a list over which you want to iterate, you want to append an element of A
to the same list B
.
The easiest way to achieve this, is probably by swapping the order of the elements in the custom_append/3
to:
custom_append(Y, X, [X|Y]).
and then we can obtain this by using a maplist/3
:
maplist(custom_append(B), A, R).
we thus already make something that behaves quite similar to partial application: we pass a functor custom_append(B)
, and Prolog will then make a call with custom_append(B, Ai, Ri)
(Ai
and Ri
are here used to denote the elements of the lists A
and R
).
Upvotes: 1