序言问题..无法调用谓词
prolog questions.. fails to invoke predicate
我试图理解为什么不调用 duck2。我是否应该涵盖两个列表为空、非空的所有情况。
duck([H|T], something ) :-
write("*"),
write("?"),
duck2([H|T], T, something_else),
write("Over").
duck2([], [], something_else) :- write("AllDone").
duck2([H|T], [H1,T1], something_else) :-
write("I am here").
trace 给出了这个..
Call: (16) duck([dog, eats], [eats], something) ? creep
Call: (17) write("*") ? creep
*
Exit: (17) write("*") ? creep
Call: (17) write("?") ? creep
?
Exit: (17) write("?") ? creep
Call: (17) duck2([dog, eats], [eats], something_else) ? creep
Fail: (17) duck2([dog, eats], [eats], something_else) ? creep
Fail: (16) duck([dog, eats], [eats], something) ? creep
duck2([H|T], [H1,T1], something_else) :-
% spaced out to line up with the below trace
duck2([H|T], [H1,T1], something_else) :-
Call: (17) `duck2([dog, eats], [eats], something_else)` ? creep
[H1, T1]
是两个项目的列表,[eats]
是一个项目的列表;如果您尝试统一它们,它们不会统一:
?- [H1, T1] = [eats].
false
所以调用失败。
Should I cover all cases
好吧,至少涵盖了足够多的案例以使代码正常工作。所有情况由您决定。
我试图理解为什么不调用 duck2。我是否应该涵盖两个列表为空、非空的所有情况。
duck([H|T], something ) :-
write("*"),
write("?"),
duck2([H|T], T, something_else),
write("Over").
duck2([], [], something_else) :- write("AllDone").
duck2([H|T], [H1,T1], something_else) :-
write("I am here").
trace 给出了这个..
Call: (16) duck([dog, eats], [eats], something) ? creep
Call: (17) write("*") ? creep
*
Exit: (17) write("*") ? creep
Call: (17) write("?") ? creep
?
Exit: (17) write("?") ? creep
Call: (17) duck2([dog, eats], [eats], something_else) ? creep
Fail: (17) duck2([dog, eats], [eats], something_else) ? creep
Fail: (16) duck([dog, eats], [eats], something) ? creep
duck2([H|T], [H1,T1], something_else) :-
% spaced out to line up with the below trace
duck2([H|T], [H1,T1], something_else) :-
Call: (17) `duck2([dog, eats], [eats], something_else)` ? creep
[H1, T1]
是两个项目的列表,[eats]
是一个项目的列表;如果您尝试统一它们,它们不会统一:
?- [H1, T1] = [eats].
false
所以调用失败。
Should I cover all cases
好吧,至少涵盖了足够多的案例以使代码正常工作。所有情况由您决定。