这是我在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
答案 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.