'Prolog function with arity = 0
I am trying to write the following in prolog:
"hasarrow/0 returns true if the Agent has the arrow, and begins to return false after shoot action has been executed by the Agent."
The agent starts with having the arrow.
My code is as follows:
assert(hasArrow).
hasArrow :- (A = shoot -> false ; assert(hasArrow)).
Is this correct?
Solution 1:[1]
You can solve the problem as follows:
:- dynamic(has_arrow/0).
has_arrow.
shoot :- retract(has_arrow).
get_arrow :- assertz(has_arrow).
Examples:
?- has_arrow.
true.
?- shoot.
true.
?- has_arrow.
false.
?- shoot.
false.
?- get_arrow.
true.
?- has_arrow.
true.
?- shoot.
true.
?- has_arrow.
false.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | slago |
