From: Jim Cheng
Subject: Declaring Variables in Lisp ??
Date: 
Message-ID: <3988B4D8.DBAAA360@yahoo.com>
Hi,
     I am a new comer to Lisp. I just have the following questions about
declaring variables in Lisp. for example:

    (defvar *comp-count* nil)
    (defvar *font-width*)

  1). It seems to me that we can use variables in Lisp without
declaring, so why bother to declare it?
  2). Why should we put * before and after a variable name, is it a
convention?

  Thanks!

Jim

From: Coby Beck
Subject: Re: Declaring Variables in Lisp ??
Date: 
Message-ID: <%g5i5.18335$47.281112@news.bc.tac.net>
"Jim Cheng" <·······@yahoo.com> wrote in message
······················@yahoo.com...
> Hi,
>      I am a new comer to Lisp. I just have the following questions about
> declaring variables in Lisp. for example:
>
>     (defvar *comp-count* nil)
>     (defvar *font-width*)
>
>   1). It seems to me that we can use variables in Lisp without
> declaring, so why bother to declare it?

not all implementations will handle an undeclared variable the same way.
AllegroCL allows you to get away without it but does issue a warning when
you compile.  But if none of your special variables are declared, you won't
notice the warnings when you really have forgotten to create a local
variable with a 'let form.  I think it is also a matter of good style and
not being sloppy.

Plus this is the place to document what this variable is for and give it an
initial value.

(defvar *foo-state* :stable "this hold the current state of blah blah,
either t, nil or :unknown which is used to control blah blah...")

>   2). Why should we put * before and after a variable name, is it a
> convention?
>

*global* is a convention.  It is a visual signal to anyone reading the code
that this variable is (at least SHOULD BE) defined somewhere outside of the
current function.  It is a GOOD convention (IMO)!!

Coby
From: Johan Kullstam
Subject: Re: Declaring Variables in Lisp ??
Date: 
Message-ID: <m37l9y77xz.fsf@sysengr.res.ray.com>
Jim Cheng <·······@yahoo.com> writes:

> Hi,
>      I am a new comer to Lisp. I just have the following questions about
> declaring variables in Lisp. for example:
> 
>     (defvar *comp-count* nil)
>     (defvar *font-width*)
> 
>   1). It seems to me that we can use variables in Lisp without
> declaring, so why bother to declare it?

1) to announce that this name refers to a dynamic variable.
2) to get an initial value.
3) it's good form.

>   2). Why should we put * before and after a variable name, is it a
> convention?

because the variable is a special, i.e., dynamic scope.  iirc there is
some difference between special and dynamic but i confess to not
understanding the distinction.

defvar (and defparameter) establish a dynamic variable.  these behave
differently than a lexical variable.  the *'s are there to avoid
surprise (the compiler knows, but humans can only deal with so much).

-- 
J o h a n  K u l l s t a m
[········@ne.mediaone.net]
sysengr
From: Thom Goodsell
Subject: Re: Declaring Variables in Lisp ??
Date: 
Message-ID: <7vlmyeqxu9.fsf@shalott.cra.com>
Jim Cheng <·······@yahoo.com> writes:

> Hi,
>      I am a new comer to Lisp. I just have the following questions about
> declaring variables in Lisp. for example:
> 
>     (defvar *comp-count* nil)
>     (defvar *font-width*)
> 
>   1). It seems to me that we can use variables in Lisp without
> declaring, so why bother to declare it?

IIRC, the fact that you can use a variable without first declaring it is
_not_ in the standard.  Most implementations allow you to do this, but
they aren't required to.  Besides issues of portability, it's generally
considered good style to declare your variables, rather than just having
them pop up randomly in your code.

Finally, a good compiler will warm you when it encounters an undeclared
variable (and say something like 'declaring *foo* special').  If you
have declared your variables, this will allow you to catch a large
percentage of dumb typos.

There are, undoubtedly, more reasons.  I'll allow others more
knowledgable than I to provide them.

>   2). Why should we put * before and after a variable name, is it a
> convention?

It's a convention for _global_ variables.


Thom

-- 
Scientist				···@cra.com
Charles River Analytics		(617) 491-3474 x574
Cambridge, MA, USA		http://www.cra.com/
From: Kent M Pitman
Subject: Re: Declaring Variables in Lisp ??
Date: 
Message-ID: <sfwd7jqi7nz.fsf@world.std.com>
Thom Goodsell <···@shalott.cra.com> writes:

> IIRC, the fact that you can use a variable without first declaring it is
> _not_ in the standard.

Yes.  This allows room for implementations to experiment with alternate
semantics for this undefined case--such as defining them instead to be
global lexicals (a la Scheme) instead of global specials.  Most 
implementations do not engage in this experimentation, leading to a 
widespread misbelief on the part of people who "just try things" instead
of "read the spec" that the default behavior applied by the implementation
("undeclared, so assumed special") is one forced by the standard.

The problem is further complicated by the fact that some (many? most?)
teaching texts, for simplicity, use SETQ a lot rather than DEFVAR or a
LET binding when trying to illustrate the semantics of something.  It is
generally assumed that the example makes sense regardless of the scoping,
or that the variable has already somehow been made lexical.  But it 
nevertheless confuses some.

> Finally, a good compiler will warm you when it encounters an undeclared
> variable (and say something like 'declaring *foo* special').  If you
> have declared your variables, this will allow you to catch a large
> percentage of dumb typos.

Well, a good compiler will have a switch controlling whether you're warned.
Whether it's the default is another question--depending on whether the
implementation means to define the semantics locally to not be an error.
Certainly the behavior is not portable CL, but some implementors might argue
that it is not an error in their implementation, and that the standard should
be amended to agree with that.  (I'd argue they were wrong, but that wouldn't
make them wrong.  It's a political issue, not a technical one.  Right and 
wrong in that arena aren't facts, just tools of persuasion and bullying. ;-)

> >   2). Why should we put * before and after a variable name, is it a
> > convention?
> 
> It's a convention for _global_ variables.

IMO, it's a good convention for all specials unless there is a strong
reason otherwise.  And usually such "strong" reasons are accidents waiting
to happen...  This is again a matter of personal style, though.  Certainly
the spec imposes no such burden.