I am having trouble to understand how value of Z is constantly changing. The specific step has been indicated in the stack trace output.
Here is the code I am using for finding sum of N natural numbers -
sum1(1,1).
sum1(N, Sum) :-
Next is N-1,
sum1(Next, Z),
Sum is Z + N.
Here is the stack trace -
?- sum1(3,_).
Call: (8) sum1(3, _2668) ? creep
Call: (9) _2860 is 3+ -1 ? creep
Exit: (9) 2 is 3+ -1 ? creep
Call: (9) sum1(2, _2862) ? creep
Call: (10) _2866 is 2+ -1 ? creep
Exit: (10) 1 is 2+ -1 ? creep
Call: (10) sum1(1, _2868) ? creep
Exit: (10) sum1(1, 1) ? creep
Call: (10) _2872 is 1+2 ? creep
Exit: (10) 3 is 1+2 ? creep
Exit: (9) sum1(2, 3) ? creep **%How is Z assigned value 3 ?**
Call: (9) _2668 is 3+3 ? creep
Exit: (9) 6 is 3+3 ? EOF: exit
Thanks in advance!
Z = A
, a new anonymous variable_1234
is created, assigned to both A and all locations where Z is used. – G_V