Page 1 of 1

### Change variable value employing expression of same variable

Posted: 20 Apr 2018 15:03
Hello
Is there a way to change the value of a variable using an expression employing the same variable?

For example if Slist is of type string*:

Code: Select all

``````...
changeVriable(Slist):
Slist==myProcedureThatReturnsNewList(Slist),
....
``````
I have blindly tied "==" and ":=".

dave

### Re: Change variable value employing expression of same variable

Posted: 20 Apr 2018 17:04
I could be completely wrong but isn't == an equality comparison and = an assignment?

Why not use = ?

P.

### Re: Change variable value employing expression of same variable

Posted: 20 Apr 2018 17:59
Well, the expression on the right requires the var Slist to be bound.
If the expression results in a change, then the "=" is a test of equality, that will always fail.

dave

### Re: Change variable value employing expression of same variable

Posted: 20 Apr 2018 21:50
There is no operator that changes the value of a Green variables.

= is a "locical/mathematical" euqality operator, it succeds if the two sides are equal. If there are unbound Green variables involved in the test then they will be bound to suitable values if that will "solve" the equaltion.

== is basically the same operation, except that the two sides must be equal (possibly after binding unbound Green variables).

= will fail if the two sides cannot be made equal. == will raise an exception if the two sides cannot be made equal.

But none of them will change the value of a Green variable that is already bound to a value (nor will any other operator) .

### Re: Change variable value employing expression of same variable

Posted: 20 Apr 2018 22:03
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),
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),