变量未声明

发布于 2025-01-18 11:57:23 字数 337 浏览 0 评论 0原文

我的功能可以从列表中获取第一个项目,并检查该项目是否是几个项目之一,可以相应地执行一些功能,并在列表上的下一个项目上递归执行。

我的麻烦是,在第6行中,变量不再定义为列表的第一项,我很难弄清楚原因。

我刚开始与KBSS合作,如果我的语言不正确,请原谅我。

1 f(L) :- 
2     [F|Ls] = L,
3     (
4        (F = value1 -> ...);
5        (F = value2 -> ...)
6    ) -> f(Ls); format('~w is not a valid action', [F]).

I have a function that takes the first item from a list, and checks if that item is one of several items, executes some functions accordingly and recursively executes on the next items on the list.

My trouble is that in line 6, the variable instantiated as the first item of the list is no longer defined, and I have trouble figuring out why.

I just started working with KBSs' so forgive me if my language is incorrect.

1 f(L) :- 
2     [F|Ls] = L,
3     (
4        (F = value1 -> ...);
5        (F = value2 -> ...)
6    ) -> f(Ls); format('~w is not a valid action', [F]).

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

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

发布评论

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

评论(2

七七 2025-01-25 11:57:23

编译代码后,询问以下查询:

?- listing(f).

f(L) :-
    (   L=[F|Ls],                              % <== unification IS PART OF the condition!
        (   F=value1
        ->  ...
        ;   F=value2
        ->  ...
        )
    ->  f(Ls)
    ;   format('~w is not a valid action', [F]) % <== when condition fails, unification is UNDONE
    ).

如您所见,统一是条件的一部分。因此,当条件失败时,统一为 undone ,转动f a 变量。

要解决问题,请按以下方式格式化代码:

f(L) :-
     [F|Ls] = L,                  % <== Now, unification IS NOT PART OF the condition!
     (   (   (F = value1 -> ...)
         ;   (F = value2 -> ...) )
     ->  f(Ls)
     ;   format('~w is not a valid action', [F]) ).

After compiling your code, ask the following query:

?- listing(f).

f(L) :-
    (   L=[F|Ls],                              % <== unification IS PART OF the condition!
        (   F=value1
        ->  ...
        ;   F=value2
        ->  ...
        )
    ->  f(Ls)
    ;   format('~w is not a valid action', [F]) % <== when condition fails, unification is UNDONE
    ).

As you can see, the unification is part of the condition. Thus, when condition fails, the unification is undone, turning F a single variable.

To solve the problem, format the code as follows:

f(L) :-
     [F|Ls] = L,                  % <== Now, unification IS NOT PART OF the condition!
     (   (   (F = value1 -> ...)
         ;   (F = value2 -> ...) )
     ->  f(Ls)
     ;   format('~w is not a valid action', [F]) ).
听风吹 2025-01-25 11:57:23

- &gt;/2用法的示例:

do_something_with_first_elem([Head|_]) :-
    do_something_with_elem(Head).
    
do_something_with_elem(Elem) :-
    ( Elem = a -> writeln('Head is a')
    ; Elem = b -> writeln('Head is b')
    ; Elem = 3 -> writeln('Head is 3 (a number)')
    ; format('~w is not a valid action', [Elem])
    ).

结果:swi -promog:

?- do_something_with_first_elem([b, c, d]).
Head is b
true.

?- do_something_with_first_elem([z, c, d]).
z is not a valid action
true.

Example of ->/2 usage:

do_something_with_first_elem([Head|_]) :-
    do_something_with_elem(Head).
    
do_something_with_elem(Elem) :-
    ( Elem = a -> writeln('Head is a')
    ; Elem = b -> writeln('Head is b')
    ; Elem = 3 -> writeln('Head is 3 (a number)')
    ; format('~w is not a valid action', [Elem])
    ).

Result in swi-prolog:

?- do_something_with_first_elem([b, c, d]).
Head is b
true.

?- do_something_with_first_elem([z, c, d]).
z is not a valid action
true.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文