Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Scheme Programming Language

Why can't "define" double as "set!"?


hi

Is there any good reason that "define" shouldn't double as "set!"?

dillog@gmail.com wrote:
> Is there any good reason that "define" shouldn't double as "set!"?

Yes: DEFINE defines variables, SET! changes the values of (defined)
variables:

(set! x 1) => wrong!
(define x 0)
(set! x 1)
x => 1

Some interpreters may implicitly define variables when you try
to set! them for the first time, thereby making DEFINE and SET!
/appear/ to be the same.

--
Nils M Holm <n m h @ t 3 x . o r g> -- http://t3x.org/nmh/

On Thu, 5 Apr 2007 08:47:02 +0000 (UTC), Nils M Holm

<before-2007-07@online.de> wrote:
>dillog@gmail.com wrote:
>> Is there any good reason that "define" shouldn't double as "set!"?

>Yes: DEFINE defines variables, SET! changes the values of (defined)
>variables:

>(set! x 1) => wrong!
>(define x 0)
>(set! x 1)
>x => 1

>Some interpreters may implicitly define variables when you try
>to set! them for the first time, thereby making DEFINE and SET!
>/appear/ to be the same.

Additionally, implicit defines do not work in compiled code and may
work only at the top level in an interpreter.  

Within a function, setting a variable that hasn't been declared (at
the top level using define, in a lambda list or in a let) will fail if
the function is compiled and may fail if the function is interpreted.

(define x 0)
(define (f z)
  (let ((w 0))
     (set! w 1)
     (set! x 1)
     (set! z 1)
     (set! y 1)))
(f 0)
set!: cannot set undefined identifier: y

So, it may work in some situations, but don't count on it.

George
--
for email reply remove "/" from address

<dillog@gmail.com> wrote:
> hi

> Is there any good reason that "define" shouldn't double as "set!"?

Doesn't define extend the environment function with a new binding whereas
set! modifies the store function?
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc