basic question about define and set!

Discussion of Scheme and Racket
Post Reply
Posts: 1
Joined: Mon Mar 08, 2010 4:21 am

basic question about define and set!

Post by reynard » Mon Mar 08, 2010 4:39 am


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))


(set! balance (some-function balance))


Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX

Re: basic question about define and set!

Post by findinglisp » Mon Mar 22, 2010 6:16 pm

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 ... _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.

Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: basic question about define and set!

Post by nuntius » Tue Mar 23, 2010 11:12 pm

Code: Select all

Post Reply