From: Brian Kendig
Subject: What's the difference between setf, setq, defvar, defparameter, &c.?
Date: 
Message-ID: <bskendigCH0Bo0.M44@netcom.com>
What's the difference between the various operators that set a variable,
and under what circumstances should I use each one?  (I'm especially
confused about the distinction between setf and setq.)

Say I want to have a global variable, in my program; let's call it
*textbuffer*, and I want it to start out nil, but I want any of my routines
to be able to read and change it.  Is (setf *textbuffer* nil) the ideal way
to handle this?

Say I know that *textbuffer* will only ever be accessed from one function,
so I may as well make it local to that function, but I want it to retain
its value across calls, like "static" variables in C.  How is this done?

-- 
_/_/_/  Brian Kendig                              Je ne suis fait comme aucun
/_/_/  ········@netcom.com                 de ceux que j'ai vus; j'ose croire
_/_/                             n'etre fait comme aucun de ceux qui existent.
  /  Be insatiably curious.   Si je ne vaux pas mieux, au moins je suis autre.
 /     Ask "why" a lot.                                           -- Rousseau

From: Mark S. Riggle
Subject: Re: What's the difference between setf, setq, defvar, defparameter, &c.?
Date: 
Message-ID: <CH0EJ4.CE0@unx.sas.com>
In article <··················@netcom.com>, ········@netcom.com (Brian Kendig) writes:
 
|> Say I want to have a global variable, in my program; let's call it
|> *textbuffer*, and I want it to start out nil, but I want any of my routines
|> to be able to read and change it.  Is (setf *textbuffer* nil) the ideal way
|> to handle this?

yes
|> 
|> Say I know that *textbuffer* will only ever be accessed from one function,
|> so I may as well make it local to that function, but I want it to retain
|> its value across calls, like "static" variables in C.  How is this done?
|> 

Use a let to create local variables that are in the lexical scope
of the function, as so:

(let ((a 1))
  (defun ggg ()
   (setf a (+ 1 a))
   a))

(ggg) ==> 2
(ggg) ==> 3
...


-- 
=========================================================
Mark Riggle                         | "Give me LAMBDA or
······@unx.sas.com                  |  give me death"
SAS Institute Inc.,                 |
SAS Campus Drive, Cary, NC, 27513   |  
(919) 677-8000                      |
From: Peter Dudey, Order of the Golden Parentheses
Subject: "Static" variables in LISP (Lexical closures)
Date: 
Message-ID: <DUDEYP.93Nov24125455@godel.cs.orst.edu>
In article <··········@unx.sas.com> ······@zinfande.unx.sas.com (Mark S. Riggle) writes:

   In article <··················@netcom.com>, ········@netcom.com (Brian Kendig) writes:

   |> Say I know that *textbuffer* will only ever be accessed from one function,
   |> so I may as well make it local to that function, but I want it to retain
   |> its value across calls, like "static" variables in C.  How is this done?

   Use a let to create local variables that are in the lexical scope
   of the function, as so:

   (let ((a 1))
     (defun ggg ()
      (setf a (+ 1 a))
      a))

   (ggg) ==> 2
   (ggg) ==> 3
   ...

Pedant point: The last line of the function is redundant, because
(setf a (+ 1 a)) returns the new value of a.
--
Peter Dudey                     }{ What is Evolutionary Programming, as
MS student in AI (Oregon State) }{  opposed to GA and GP?
······@research.cs.orst.edu     }{ Does anybody know of a decent freeware
257 NE 13th, Salem, OR  97301   }{  WYSIWYG PS editor for X?
From: Neves
Subject: Re: What's the difference between setf, setq, defvar, defparameter, &c.?
Date: 
Message-ID: <2d0bbb$7dm@anaxagoras.ils.nwu.edu>
Brian Kendig (········@netcom.com) wrote:
: What's the difference between the various operators that set a variable,
: and under what circumstances should I use each one?  (I'm especially
: confused about the distinction between setf and setq.)

: Say I want to have a global variable, in my program; let's call it
: *textbuffer*, and I want it to start out nil, but I want any of my routines
: to be able to read and change it.  Is (setf *textbuffer* nil) the ideal way
: to handle this?

You declare and initialize a global variable that you want to change
with defvar or defparameter.  I personally like defparameter because
subsequent evaluations of it (e.g. by loading the file with it at the
top level) will reset the variable.
(defparameter *textbuffer* nil)
-----
: Say I know that *textbuffer* will only ever be accessed from one function,
: so I may as well make it local to that function, but I want it to retain
: its value across calls, like "static" variables in C.  How is this done?

One way of getting static variables in Lisp is to define a closure in
Lisp.
e.g.
(setq fn
   (let ((static_variable 0))
     #'(lambda (x) (incf static_variable x))) ;return a function that
                                              ;incfs static-variable
   )
(funcall fn 10)  ;increment static_variable by 10
(funcall fn 20)  ; "                           20

or 

(let ((static_variable 0))
  (defun myfn (x)
    (incf static_variable x)))

(myfn 10) ;increment static_variable by 10
(myfn 20) ;increment static_variable by 20
-----

You could also use CLOS class stuff to do static variables.

-David
From: William M. York
Subject: defvar vs defparameter (was "What's the difference between setf, setq..")
Date: 
Message-ID: <YORK.93Nov24125419@sparkie.PARC.Xerox.COM>
In article <··········@anaxagoras.ils.nwu.edu> ·····@aristotle.ils.nwu.edu (Neves ) writes:

   Brian Kendig (········@netcom.com) wrote:
   : What's the difference between the various operators that set a variable,
   : and under what circumstances should I use each one?

   You declare and initialize a global variable that you want to change
   with defvar or defparameter.  I personally like defparameter because
   subsequent evaluations of it (e.g. by loading the file with it at the
   top level) will reset the variable.

That is, of course the distinction between defvar and defparameter,
but one isn't automatically better than the other.

Defparameter, as its name implies, is best used to hold a parameter or
option value.  If you want to change that value later, you just edit
the defparameter form and re-evaluated it.

But that's not always what you want.  Sometimes you use a global
variable to keep some kind of "database" for your application.  If you
are writing an address book application, you might define the
*addresses* variable with initial value nil to hold the list of
address entries.  But, if in the course of incremental development you
re-load or re-evaluate your source file you may not want the variable
to be reset to nil, trashing your existing database.  So that's why
defvar doesn't change the value on subsequent evaluations.

Genera had this right: if you reloaded a source file the defvar'd
variables would retain their values, but if you explictly re-evaluated
a single defvar form in the editor it would reset the value (printing
a warning).  I hate having to edit "defvar" into "setq" in order to
revert to the initial value in lesser Lisp environments.
From: Barry Margolin
Subject: Re: What's the difference between setf, setq, defvar, defparameter, &c.?
Date: 
Message-ID: <2d5jqbINNhj1@early-bird.think.com>
In article <··················@netcom.com> ········@netcom.com (Brian Kendig) writes:
>What's the difference between the various operators that set a variable,
>and under what circumstances should I use each one?  (I'm especially
>confused about the distinction between setf and setq.)

SETQ can only be used to set variables, while SETF can be used to assign to
any generalized reference.  When assigning to a variable, the choice of
whether to use SETQ or SETF is a stylistic one; some people prefer to use
SETF for everything to be consistent, while others use SETQ to make it
clear that they intend to assign to a variable (i.e. it provides
redundancy).

DEFVAR, DEFPARAMETER, and DEFCONSTANT are used to declare and initialize
global variables.

>Say I want to have a global variable, in my program; let's call it
>*textbuffer*, and I want it to start out nil, but I want any of my routines
>to be able to read and change it.  Is (setf *textbuffer* nil) the ideal way
>to handle this?

No, you should use

(defvar *textbuffer* nil
  "Documentation string")

This indicates that *textbuffer* is a global variable, and declares it
SPECIAL.  If you want the variable reinitialized every time you reload the
program (as your SETF would), you would use DEFPARAMETER rather than
DEFVAR; this is normally used for configuration parameters (hence the
function name), not for variables containing global state (you don't want
to lose your state just because you're loading a program fix).

>Say I know that *textbuffer* will only ever be accessed from one function,
>so I may as well make it local to that function, but I want it to retain
>its value across calls, like "static" variables in C.  How is this done?

In this case you would use SETQ (or SETF) to initialize the variable, and
use a SPECIAL declaration local to that function:

(locally (declare (special *textbuffer*))
  (unless (boundp '*textbuffer*)
    (setq *textbuffer* nil)))

(defun foo-function (...)
  (declare (special *textbuffer*))
  ...)
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar