Home | Notes | Languages | Programs | Homework |
Re: binding variables |
Stephen Edwards (edwards@CS.VT.EDU)
Mon, 10 Dec 2001 11:39:21 -0500
Message-ID: <3C14E539.1A53AC9E@cs.vt.edu> Date: Mon, 10 Dec 2001 11:39:21 -0500 From: Stephen Edwards <edwards@CS.VT.EDU> Subject: Re: binding variables
> If a variable is already bound, is there a way to force it to rebind to a
> new value instead of failing as a comparison?
No. That is called "assignment." In Prolog (as in functional languages),
variables are "immutable", meaning that you cannot change their value.
In particular, "=" is NOT assignment. It is unification-based equality
comparison. A clause like "A = B" only succeeds if A and B are equal.
If one (or both) of the terms contains a variable that is currently unbound,
then Prolog "binds" that variable so that the equality comparison can
succeed (if possible). If some later failure forces backtracking, this
binding is undone as part of the backtracking process.
Once bound, a variable cannot be "rebound" later, unless failure and
backtracking remove the previous binding and a different execution
path is chosen.
-- Steve
-- Stephen Edwards 604 McBryde Hall Dept. of Computer Science e-mail : edwards@cs.vt.edu U.S. mail: Virginia Tech (VPI&SU) office phone: (540)-231-5723 Blacksburg, VA 24061-0106 -------------------------------------------------------------------------------
Home | Notes | Languages | Programs | Homework |