Assuming that you call neda(H,A)
with A
being a free variable you end up calling append(A,[1575,1740],A)
with A
being a free variable as well. However, traditionally, append
is implemented as
append([],Ys,Ys).
append([X|Xs],Ys,[X|Zs]) :- append(Xs,Ys,Zs).
Unification of append(A,[1575,1740],A)
with append([],Ys,Ys)
fails since it requires A
to be []
, on the one hand, and [1575,1740]
, on the other hand. Hence, Prolog tries to unify append(A,[1575,1740],A)
with append([X|Xs],Ys,[X|Zs])
and succeeds with {A/[X|Xs], Ys/[1575,1740], Xs/Zs}
which leads to the call append(Xs,[1575,1740],Xs)
and the process repeats, i.e., Prolog enters an infinite recursion and ultimately runs out of stack.
If I guess your intention correctly, you first would like to append [1575,1740]
to A
and then to append Z2
. This would mean that you need to introduce two new variables, say A1
and A2
, to record the state of A
after each appending step:
neda(H,A):- hasA(H,Z1),
append(A,[1575,1740],A1),
state(Z1,Z2),
append(A1,Z2,A2).
Beware, however, that with A
being a free variable on backtracking this code will generate lists of increasing length; is this what you want?