Andrew0082
Andrew0082

Reputation: 21

defparameter values do not change automatically

In my program that I am writing I have some parameters like for example the followings :

(defparameter foo 3)

and then after that down in my code I have another parameter that has a value that depends on the previous one :

(defparameter bar (+ 2 foo)) 

When I evaluate the code in this example i get foo that equals 3 and bar that equals 5.

Since I am new in Common Lisp, and new in programming in general, I was expecting that by updating the value of foo, for example by doing (setf foo 6), I would have had the value of bar also automatically updated to 8.

But this is not the case. If I update foo to 6 although the definition of bar is (+ 2 foo) if I call bar I still get its initially evaluated value of 5.

How do I get to have variables that update themselves instantaneously every time another variable to which they are dependent gets modified?

Upvotes: 2

Views: 81

Answers (2)

coredump
coredump

Reputation: 38789

Since I am new in Common Lisp, and new in programming in general, I was expecting that by updating the value of foo, for example by doing (setf foo 6), I would have had the value of bar also automatically updated to 8.

This is a nice thing to expect a system to do, but generally you don't have this kind of behaviour, ie. dataflow programming, implemented automatically in the base language (a famous example of ones that do are spreadsheets). In simple cases like the one you describe, the behaviour is well-defined, but as programs tend to get larger you want to control when expressions are evaluated, otherwise you might trigger a lot of computations for nothing, or do side-effects more often than you want.

The building blocks for computing things on demand are functions, in your case you could define foo and bar as functions:

(defun foo () 
  3)

(defun bar () 
  (+ 2 (foo)))

Unless you make the effort to have those functions inlined, changing the definition of foo will affect the value computed by bar, which is the behaviour you want to have.

Upvotes: 3

Sylwester
Sylwester

Reputation: 48745

When Common Lisp encounters (defparameter *name* expression) it will ensure that *name* exist as a special variable, then evaluate the expression and set the binding to the result. Eg. no matter how much you update foo it will no longer be able to affect bar.

If you want something to dynamically be calculated you create a function and apply it every time you want the value:

(defparameter *foo* 3)
(defun bar ()
  (+ 2 *foo*))
(bar) ; ==> 5
(setf *foo* 6)
(bar) ; ==> 8

Upvotes: 2

Related Questions