basic question about define and set!

Discussion of Scheme and Racket

basic question about define and set!

Postby reynard » Mon Mar 08, 2010 4:39 am

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.
reynard
 
Posts: 1
Joined: Mon Mar 08, 2010 4:21 am

Re: basic question about define and set!

Postby 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 http://www.schemers.org/Documents/Stand ... -H-8.html#%_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/
findinglisp
 
Posts: 440
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX

Re: basic question about define and set!

Postby nuntius » Tue Mar 23, 2010 11:12 pm

Code: Select all
http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-8.html#%_sec_5.2.1
User avatar
nuntius
 
Posts: 497
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA


Return to Scheme

Who is online

Users browsing this forum: No registered users and 1 guest

cron