I have written the following predicate append/3 to implement the combination of two lists:
append([L|Ls],R,[L|Result]):-append(Ls,R,Result).
append([],X,X).
It gives a correct output, yet when I trace the execution flow of the code, here is what I get:
1 ?- edit.
true.
2 ?- make.
% //dougal/cs0u$/cyw03u/desktop/lab3 compiled 0.00 sec, 3 clauses
true.
3 ?- trace.
true.
[trace] 3 ?- append([a,b,c],[d,e],X).
Call: (6) append([a, b, c], [d, e], _G554) ? creep
Call: (7) lists:append([b, c], [d, e], _G636) ? creep
Exit: (7) lists:append([b, c], [d, e], [b, c, d, e]) ? creep
Exit: (6) append([a, b, c], [d, e], [a, b, c, d, e]) ? creep
X = [a, b, c, d, e].
It seems that Prolog was using my own append predicate in the first turn, but as it enters the second level of recursion, Prolog has used its own predicate as defined in the library.
How can I override Prolog's predefined predicate (besides giving my own predicate another name)?
append
version. Which prolog interpreter are you using? – lurkerswipl
and putting your code in as[user]
? – lurkermake.
and then[quicksort].
in the window that popped up. – Pinguswipl
command, get a prolog prompt, (2) type in[user].
followed by Enter, (3) enter just yourappend
code. Then at the prolog prompt, start trace (entertrace.
) and your example query and see if you get the same result. – lurker