0

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?

false
  • 10,264
  • 13
  • 101
  • 209
morax
  • 1

2 Answers2

1

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].
brebs
  • 3,462
  • 2
  • 3
  • 12
0

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].
Enigmativity
  • 113,464
  • 11
  • 89
  • 172