The essence of prolog and some other functional languages is that variables are immutable, so strictly speaking the answer is "no, you can't do that."
However, there are ways to use
fact variables or
mutable variable classes/objects to achieve the same result. You can find mutable objects here in pfc:
pfc\collection\M\varM class.
Fact variable example:
Code: Select all
class facts
myVar : integer := erroneous.
clauses
run() :-
myVar := 3,
stdio::write("\n", myVar),
myVar := myVar + 4,
stdio::write("\n", myVar),
stdio::readLine() = _.
Mutable variable example:
Note that here you can treat the value property of the varM object created as a mutable variable. This seems to be just another syntactic approach to modifying a fact variable, only in this case it is in a disposable object instead of inherent in the class or object using that variable. I suspect that with very large variables it might be preferable to use varM because it could be garbage-collected away later, but I am not certain about that.
Code: Select all
run() :-
MyVar = varM::new(3), % create a new mutable variable and initialize its value property to 3
stdio::write("\n", MyVar:value),
MyVar:value := MyVar:value + 5,
stdio::write("\n", MyVar:value),
stdio::readLine() = _.