Reputation: 23
I'm good at JAVA,now I'm learning Prolog,its so difficult that i need some help...thank you.
each element of ABs
is a term of the form A-B where A is the corresponding element of As and B is the corresponding element of Bs
.
zip(As,Bs,ABs)
:
some examples:
zip([1,2,3,4],[a,b,c,d],L).
L=[1-a,2-b,3-c,4-d].
zip(X,Y,[1-a,2-b,3-c]).
X=[1,2,3],Y=[a,b,c]
zip([1,2,3,4],[a,b,c],L).
fail.
can someone help me. thank you..
My implementation:
zip(As,Bs,ABs) :-
append(X,XS,As),
append(Y,YS,Bs),
this is what i have done..im lost..
Upvotes: 0
Views: 963
Reputation: 12972
It is not very efficient to use append/3. You could simply write something like:
zip([],[],[]).
zip([H|T],[H1|T1],[H-H1|T2]):-zip(T,T1,T2).
Some examples:
?- zip([1,2,3,4],[a,b,c,d],L).
L = [1-a, 2-b, 3-c, 4-d].
?- zip(X,Y,[1-a,2-b,3-c]).
X = [1, 2, 3],
Y = [a, b, c].
?- zip([1,2,3,4],[a,b,c],L).
false.
Upvotes: 1