This is a really simple quesiton but I don't get it.
insert(X, L, [X|L]).
insert(X, [H|T], [H|T2]) :-
insert(X, T, T2).
We run it with insert(1, [2,3], L).
When it's called the 1st insert produces (1, [2,3], [1|2,3]) > L = [1,2,3], then it looks at the second insert(1, [2|3], [2|T2]) // T2 is a variable im guessing.. and it calls insert(1, [3], T2) which calls the 1st insert again with insert(1, [3], [1|3]) > L = [1,3],
which is not true, because it actually returns L = [2,1,3]. What am I missing about recursions?
[1|[2,3]]
not[1|2,3]
and[2|[3]]
not[2|3]
. – lurkerinsert(1, [2|[3]], L)
matches the second clause withinsert(1, [2|[3]], [2|T2])
which callsinsert(1, [3], T2)
. That call yieldsT2 = [1,3]
as you show, but that leads to the final result[2|[1,3]]
or simplyL = [2,1,3]
. – lurker