From: quadraticformula
Subject: (setq a ... ) vs (set 'a ... )
Date: 
Message-ID: <1189311192.555999.281320@50g2000hsm.googlegroups.com>
Hi, I'm beginning to learn common lisp, and I wrote a pointless
function makereallybig to help me understand how some things work.
Unfortunately It's made me more confused.

Why is it that when I use this version of the function
------------------------------------------------------------------------------
(defun makereallybig (number)
        (print (set 'number (* number number) ) )
        (print number)
)
------------------------------------------------------------------------------
and I execute (makereallybig 3)
the first number printed is 9, as I would expect
but then when it prints "number" again, it goes back to being 3, even
though it seems as though "number" should have been set to nine
However when I do this:
------------------------------------------------------------------------------
(defun makereallybig (number)
        (print (setq number (* number number) ) )
        (print number)
)
-------------------------------------------------------------------------------
(makereallybig 3) prints 9 and then 9 like I would expect. Everything
I've read has said that (setq a ...) is just short for (set 'a ...).
If that is the case, why doesn't the change from 3 to 9 stick in the
first version of this function. How could it make such a difference?

From: Geoff Wozniak
Subject: Re: (setq a ... ) vs (set 'a ... )
Date: 
Message-ID: <1189315481.963384.178690@22g2000hsm.googlegroups.com>
On Sep 9, 12:13 am, quadraticformula <·············@gmail.com> wrote:
> Hi, I'm beginning to learn common lisp, and I wrote a pointless
> function makereallybig to help me understand how some things work.
> Unfortunately It's made me more confused.
>
> Why is it that when I use this version of the function
> --------------------------------------------------------------------------- ---
> (defun makereallybig (number)
>         (print (set 'number (* number number) ) )
>         (print number)
> )
> --------------------------------------------------------------------------- ---
> and I execute (makereallybig 3)
> the first number printed is 9, as I would expect
> but then when it prints "number" again, it goes back to being 3, even
> though it seems as though "number" should have been set to nine

Because the (SET 'NUMBER ...) code is setting the value of the symbol
NUMBER, which is not the same as the parameter NUMBER given in the
function.  The former form is setting the value cell of the dynamic
variable NUMBER whereas the latter form is printing the value of the
lexical variable NUMBER.

> However when I do this:
> --------------------------------------------------------------------------- ---
> (defun makereallybig (number)
>         (print (setq number (* number number) ) )
>         (print number)
> )
> --------------------------------------------------------------------------- ----
> (makereallybig 3) prints 9 and then 9 like I would expect. Everything
> I've read has said that (setq a ...) is just short for (set 'a ...).
> If that is the case, why doesn't the change from 3 to 9 stick in the
> first version of this function. How could it make such a difference?

SET changes the value cell of the dynamic variable named by the
symbol, which is not the lexical version, as in your example.  Also,
note that

  (set 'a value) == (setf (symbol-value 'a) value)

which is not as you have outlined.  SYMBOL-VALUE accesses the value
cell of the dynamic variable named by symbol, which is why NUMBER
apparently doesn't equal NUMBER in your first case.

If you want this to work the same way, you have to use SETQ instead of
SET (and you don't have to quote the symbol).  However, it generally
considered good style to just use SETF (which probably expands into
SETQ anyway).

Check out the HypserSpec for all the gory details.

http://www.lispworks.com/documentation/HyperSpec/Body/f_set.htm
http://www.lispworks.com/documentation/HyperSpec/Body/s_setq.htm
http://www.lispworks.com/documentation/HyperSpec/Body/m_setf_.htm
From: D Herring
Subject: Re: (setq a ... ) vs (set 'a ... )
Date: 
Message-ID: <j86dnUbhB-T_HH7bnZ2dnUVZ_rGrnZ2d@comcast.com>
quadraticformula wrote:
> Hi, I'm beginning to learn common lisp, and I wrote a pointless
> function makereallybig to help me understand how some things work.
> Unfortunately It's made me more confused.
...
> (defun makereallybig (number)
>         (print (set 'number (* number number) ) )
>         (print number)
> )

Here, "'number" represents "the symbol named number"; whereas "number" 
represents the value passed as the parameter to the function.  During 
the compilation process, parameters and other symbols defined inside 
functions all lose their names; this enables better optimizations.

> (defun makereallybig (number)
>         (print (setq number (* number number) ) )
>         (print number)
> )

Here, setq is directly modifying the function parameter; it isn't 
doing any lookup to find out "which symbol is currently known as number".

> Everything I've read has said that (setq a ...) is just short for (set 'a ...).
> If that is the case, why doesn't the change from 3 to 9 stick in the
> first version of this function. How could it make such a difference?

One of the differences between Lisp and other languages is the concept 
of time -- when something is evaluated.  For most languages, a 
compiler converts source code into an executable; except for macros, 
everything happens at runtime.   With Common Lisp, the source code 
adds functionality to the compiler/core system; you can change how the 
compiler operates and control when the core system evaluates each 
instruction.  Several evaluation times are available -- at file read, 
macroexpansion, compilation, function execution.  Most of this is 
explained in Chapter 3 of the CLHS[1].

- Daniel

[1] http://www.lisp.org/HyperSpec/FrontMatter/Chapter-Index.html
From: ······@gmail.com
Subject: Re: (setq a ... ) vs (set 'a ... )
Date: 
Message-ID: <1189329755.142216.289440@22g2000hsm.googlegroups.com>
On Sep 9, 7:13 am, quadraticformula <·············@gmail.com> wrote:
> Hi, I'm beginning to learn common lisp,

You're just starting to learn CL and you're using SET? Please find
whoever told you to use SET (and SETQ for that matter) and beat him
with a copy of Practical Common Lisp.
From: David Trudgett
Subject: Re: (setq a ... ) vs (set 'a ... )
Date: 
Message-ID: <ufy1owh9m.fsf@yahoo.com>
quadraticformula writes:

> (defun makereallybig (number)
>         (print (set 'number (* number number) ) )
>         (print number))
> )

> Everything I've read has said that (setq a ...) is just short for
> (set 'a ...). If that is the case, why doesn't the change from 3 to
> 9 stick in the first version of this function. How could it make
> such a difference?

Warning: some of the terminology I use below, such as "lexically
bound," might not be precisely technically correct. If so, I'm sure
the resident language lawyers will correct me! ;-)

When you use (set 'number (* number number)), you are saying the same
as (setf (symbol-value 'number) (* number number)). That is done via
the symbol table.

The NUMBER parameter in the function definition is a lexically bound
variable and does not appear in the symbol table. Therefore, (set
'number (* number number)) does not set the NUMBER that is the
function's parameter, but sets the value associated with the NUMBER
symbol in the current symbol table.

Changing the function to read:

(defun makereallybig (number)
    (print (set 'number (* number number)))
    (print (symbol-value 'number)))

will print two nines when given 3 as the argument.

So, it would seem that (set 'var n) is only the same as (setq var n)
when VAR is in the symbol table.

Regards,
David Trudgett

-- 
These are not the droids you are looking for. Move along.
From: Cameron
Subject: Re: (setq a ... ) vs (set 'a ... )
Date: 
Message-ID: <1189379912.140228.262820@k79g2000hse.googlegroups.com>
On Sep 8, 11:13 pm, quadraticformula <·············@gmail.com> wrote:
> (defun makereallybig (number)
>         (print (set 'number (* number number) ) )
>         (print number)
> )

As an addendum to the other responses, here's a concise approximation
of an answer:
  * Don't use SET until you are experienced (and probably not then
either).
  * SETQ is probably not appropriate for you either.  Look into LET.
  * You probably want to make a habit of using PRINC instead of PRINT.
  * Indent like a Lisp programmer (better yet let your editor do it
for you! Try emacs):
    (defun makereallybig (number)
      (print (set 'number (* number number)))
      (print number))

I hope my answer doesn't sound rude, it's really not meant to be.

When you come from the land of imperative programming, lisp looks
weird.  But it doesn't just LOOK weird, it really IS weird. It's not
just a new syntax; things are done in a fundamentally different way.
Definitely pick up a book on lisp programming, don't just look up
functions and compare them to your favorite Java/C++ methods/functions
because you'll likely miss the real point (and power) of lisp and
you'll just end up writing imperative code in funny parenthesis
syntax.
--
Cameron Desautels <······@gmail.com>
From: Cameron
Subject: Re: (setq a ... ) vs (set 'a ... )
Date: 
Message-ID: <1189380559.758545.303540@w3g2000hsg.googlegroups.com>
Oh, and here's a lispier version of your function:

(defun make-really-big (n)
  "Takes a number, N, makes it really big and prints it."
  (let ((big-n (expt n 2)))
    (princ big-n)))

Happy hacking!
--
Cameron Desautels <······@gmail.com>
From: Matthias Buelow
Subject: Re: (setq a ... ) vs (set 'a ... )
Date: 
Message-ID: <5kjdsaF3v4fcU1@mid.dfncis.de>
quadraticformula wrote:

> Everything
> I've read has said that (setq a ...) is just short for (set 'a ...).

Then you've only been reading texts that predate lexical variables in Lisp.