This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

basic question about define and set!

3 posts · 5141 views

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!

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.)
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

Re: basic question about define and set!

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