num(N) :- No=N, write(No), nl.
check(S) :- No==S -> write(Ok) ; write(Not ok).
When i call num(5)
, it prints 5. However after calling num(5)
, when i call check(5)
, it prints Not ok. I think its because of scope of variables..How can i make it work, i mean variable No like a 开发者_JAVA技巧global variable, so that i can check its value in other clauses?
you can use the global variables of swipl or assert/retract
however, using global variables is a bit against the declarative programming paradigm since it violates referential transparency
There's no global variable in Prolog. If you need a variable in all the clauses, pass it as an argument in those clauses. For example, you use:
check(S, N0) :- N0==S -> write('Ok') ; write('Not ok').
and call check(5, 5)
or check(S, 5)
as you want.
精彩评论