prolog变量不存在

时间:2014-10-28 12:43:29

标签: prolog swi-prolog

这是我在swi prolog中测试else和函数调用的简单程序。以下代码表示rule不存在

有什么问题
call_rule(Roll):-
(
    member(Roll,[123]),
    writeln('inside call rule'),
    nb_getval(rule, 'this is the rule')
).
print_roll(Roll) :-
    (   Roll < 2 ->
        writeln('not a roll')
    ;
        (  Roll > 1243 ->
           writeln('not a roll'),writeln('this is 2nd alternative'),writeln('this is third alternative')
        ;
           ( Roll =:= 12 ->
                writeln('boxcars')
            ;
                ( call_rule(Roll) ->
                    nb_getval(rule, RULE),
            writeln('snake eyes')
                ;
                    nb_getval(rule,SUBJECT),
            writeln(SUBJECT)    
                )
            )
        )
    ).

结果:

3 ?- print_roll(123).  
inside call rule
ERROR: nb_getval/2: variable `rule' does not exist

1 个答案:

答案 0 :(得分:1)

在您从非回溯商店获取价值之前,您需要nb_setval密钥:

示例

?- nb_getval(a,X).
ERROR: nb_getval/2: variable `a' does not exist
?- nb_setval(a,foo).
true.

?- nb_getval(a,X).
X = foo.