Page 1 of 1

basic question about define and set!

Posted: Mon Mar 08, 2010 4:39 am
by reynard
Hello,

Suppose I want to store a value for later use. I define it using:

(define balance 10)

Later on I apply a function to #'balance and want to store the result for later use. The old value of #'balance is useless, and I do not want to introduce a new name every time for the result of function application, so I do either one of the followings. I would like to ask what is the real difference between the followings:

(define balance (some-function balance))

vs

(set! balance (some-function balance))

Thanks.

Re: basic question about define and set!

Posted: Mon Mar 22, 2010 6:16 pm
by findinglisp
DEFINE actually creates a global variable if one doesn't already exist before setting it to a new value. SET! simply sets an already-existing variable to a new value (mutation).

See http://www.schemers.org/Documents/Stand ... _sec_5.2.1

(Note that phpBB botches the URL above. You might have to copy and paste it directly into your browser.)

Re: basic question about define and set!

Posted: Tue Mar 23, 2010 11:12 pm
by nuntius

Code: Select all

http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-8.html#%_sec_5.2.1