Discussions related to Visual Prolog
daveplummermd
VIP Member
Posts: 80
Joined: 18 Jul 2006 17:18

Change variable value employing expression of same variable

Unread post by daveplummermd »

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 ":=".

Thanks in advance.

dave
Dave Plummer
Paul Cerkez
VIP Member
Posts: 106
Joined: 6 Mar 2000 0:01

Re: Change variable value employing expression of same variable

Unread post by Paul Cerkez »

I could be completely wrong but isn't == an equality comparison and = an assignment?

Why not use = ?

P.
AI Rules!
P.
daveplummermd
VIP Member
Posts: 80
Joined: 18 Jul 2006 17:18

Re: Change variable value employing expression of same variable

Unread post by daveplummermd »

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
Dave Plummer
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Re: Change variable value employing expression of same variable

Unread post by Thomas Linder Puls »

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) .
Regards Thomas Linder Puls
PDC
Harrison Pratt
VIP Member
Posts: 439
Joined: 5 Nov 2000 0:01

Re: Change variable value employing expression of same variable

Unread post by Harrison Pratt »

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() = _.
daveplummermd
VIP Member
Posts: 80
Joined: 18 Jul 2006 17:18

Re: Change variable value employing expression of same variable

Unread post by daveplummermd »

perfect. thanks
Dave Plummer
Post Reply