I want to add the first element of a list to another list using this:
findall(X, nth1(1, List1, X,), List2).
but it returns false.
Someone knows why?
I want to add the first element of a list to another list using this:
findall(X, nth1(1, List1, X,), List2).
but it returns false.
Someone knows why?
Simple:
ListFirst = [a, b, c],
ListSecond = [d, e, f],
ListFirst = [Head|Tail],
ListSecondNew = [Head|ListSecond].
Result:
ListFirst = [a,b,c],
ListSecond = [d,e,f],
Head = a,
Tail = [b,c],
ListSecondNew = [a,d,e,f].
If you get the syntax right (no third comma in nth1/3
) and we put in some concrete lists, then it seems to do what you ask:
?- findall(X, nth1(1, [a,b,c], X), List2).
List2 = [a].
If you actually have two lists and you're trying to generate a third, then this can work:
?- List2=[x,y,z], List1=[a,b,c], [H|_]=List1, append([H],List2,Output).
List2 = [x, y, z],
List1 = [a, b, c],
H = a,
Output = [a, x, y, z].