Home > Back-end >  Merging two lists into a list of lists
Merging two lists into a list of lists

Time:04-27

I have two lists:

X = [a,b,c]
Y = [d,e,f]

I want the outcome to be:

Z = [[a,b,c], [d,e,f]]

However, when I use append I get:

 Z=[a,b,c,d,e,f].

Any suggestions on how I should do this?

CodePudding user response:

You may just create a list with both lists:

add_lists(X, Y, [X, Y]).

then add_lists([a,b,c], [d,e,f], Z) would yield Z = [[a,b,c], [d,e,f]].

You can also just inline this, Z=[X, Y]

CodePudding user response:

gusbro's answer is what you should do using plain unification, but just commenting on what you said:

when I use append:

You can use append differently, and get the answer you want:

?- X=[a,b,c],
   Y=[d,e,f],
   append([X], [Y], Z).

X = [a, b, c],
Y = [d, e, f],
Z = [[a, b, c], [d, e, f]].
  • Related