序言问题..无法调用谓词

发布于 2025-01-23 03:43:33 字数 705 浏览 0 评论 0原文

我试图理解为什么不调用鸭子2。


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").

痕迹给了这个..

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

I am trying to understand why duck2 is not called.. Should I cover all cases of the two list being empty, non empty.


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 gives this..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷你仙 2025-01-30 03:43:33
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

因此电话会失败。

我应该覆盖所有情况

,至少涵盖了足够的案例以使代码正常工作。所有案件取决于您。

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] is a list of two items and [eats] is a list of one item; if you try and unify them, they don't unify:

?- [H1, T1] = [eats].
false

so the call fails.

Should I cover all cases

Well, at least cover enough cases to make the code work. All cases is up to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文