From: Dave
Subject: [newbie] Local var.
Date: 
Message-ID: <S_rVa.210877$Ny5.6073886@twister2.libero.it>
I want to declare a static variable within a function.
Is it possible?
In C language it's equal to "static int x;".

thanks
Dave

From: Pascal Costanza
Subject: Re: [newbie] Local var.
Date: 
Message-ID: <bg5j6a$v0q$1@f1node01.rhrz.uni-bonn.de>
Dave wrote:
> I want to declare a static variable within a function.
> Is it possible?
> In C language it's equal to "static int x;".

Assuming that you are using Common Lisp you can do that as follows:

(let ((x 0))
   (defun do-something (...)
      ... x ...))

This makes sure that x is created once and keeps its value on each call 
of do-something. "Non-static" local variables are created like this:

(defun do-something (...)
   (let ((x 0))
      ... x ...))

If you want to know about the details why this works, make sure to look 
up the terms "closure" and "lexical closure".


Pascal


-- 
Pascal Costanza               University of Bonn
···············@web.de        Institute of Computer Science III
http://www.pascalcostanza.de  R�merstr. 164, D-53117 Bonn (Germany)
From: Dave
Subject: Re: [newbie] Local var.
Date: 
Message-ID: <3VwVa.211929$lK4.6156796@twister1.libero.it>
"Pascal Costanza" <········@web.de> ha scritto nel messaggio
·················@f1node01.rhrz.uni-bonn.de...
> Dave wrote:
> > I want to declare a static variable within a function.
> > Is it possible?
> > In C language it's equal to "static int x;".
>
> Assuming that you are using Common Lisp you can do that as follows:

I use CLISP and XLISP to learn.

>
> (let ((x 0))
>    (defun do-something (...)
>       ... x ...))
>

I understand,
but this me seems equivalent to the definition of one variable global,
or me mistake?
for example:

(defvar x 0)
(defun do-something (...)
       ... x ...))

greetings
Dave
From: Thomas A. Russ
Subject: Re: [newbie] Local var.
Date: 
Message-ID: <ymiadaxrxbw.fsf@sevak.isi.edu>
"Dave" <·········@iol.it> writes:

> "Pascal Costanza" <········@web.de> ha scritto nel messaggio
> > Dave wrote:
> > > I want to declare a static variable within a function.
> > > Is it possible?
> > > In C language it's equal to "static int x;".
> >
> > Assuming that you are using Common Lisp you can do that as follows:
> >
> > (let ((x 0))
> >    (defun do-something (...)
> >       ... x ...))
> >
> 
> I understand,
> but this me seems equivalent to the definition of one variable global,
> or me mistake?
> for example:
> 
> (defvar x 0)
> (defun do-something (...)
>        ... x ...))

No.  The difference is that the value of X is truly global, in the sense 
that anyone else can write code that can access and change the value of
X.  In the example above with LET, only those functions that are defined 
within the lexical scope of the LET have access to the variable.  It is
truly limited in accessibility and will serve the same purpose as the
static variable of C.

In fact, because of the global nature of variables introduced with
DEFVAR (and DEFPARAMETER), the Lisp community has almost universally
adopted the convention of delimiting the names of such "SPECIAL"
variables with asterisks, thus:  (defvar *x* 0)




-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Marco Antoniotti
Subject: Re: [newbie] Local var.
Date: 
Message-ID: <3F269F1B.6050308@cs.nyu.edu>
Dave wrote:
> "Pascal Costanza" <········@web.de> ha scritto nel messaggio
> ·················@f1node01.rhrz.uni-bonn.de...
> 
>>Dave wrote:
>>
>>>I want to declare a static variable within a function.
>>>Is it possible?
>>>In C language it's equal to "static int x;".
>>
>>Assuming that you are using Common Lisp you can do that as follows:
> 
> 
> I use CLISP and XLISP to learn.
> 
> 
>>(let ((x 0))
>>   (defun do-something (...)
>>      ... x ...))
>>
> 
> 
> I understand,
> but this me seems equivalent to the definition of one variable global,
> or me mistake?

You mistake :)


> for example:
> 
> (defvar x 0)
> (defun do-something (...)
>        ... x ...))

cl-prompt> x
0

cl-prompt> (let ((y 42)) y)
42

cl-prompt> y
Error: UNBOUND-VARIABLE

Cheers
--
Marco
From: Barry Margolin
Subject: Re: [newbie] Local var.
Date: 
Message-ID: <FlxVa.25$EE2.23@news.level3.com>
In article <························@twister1.libero.it>,
Dave <·········@iol.it> wrote:
>"Pascal Costanza" <········@web.de> ha scritto nel messaggio
>·················@f1node01.rhrz.uni-bonn.de...
>> (let ((x 0))
>>    (defun do-something (...)
>>       ... x ...))
>>
>
>I understand,
>but this me seems equivalent to the definition of one variable global,
>or me mistake?
>for example:
>
>(defvar x 0)
>(defun do-something (...)
>       ... x ...))

The difference is that you can only have one global variable named X.  But
each time you create a closure, it's a *different* variable.

(let ((x 0))
  (defun do-something (...)
    ... x ...))

(let ((x 1))
  (defun do-something-else (...)
    ... x ...))

Both these functions make use of a variable named X, but they're unrelated
to each other.  This is like two C functions that have a local static
variable with the same name.

In Lisp if you *want* several functions to share the same "static"
variable, you can do it by defining them in the same lexical environment:

(let ((x 0))
  (defun do-thing1 (...)
    ... x ...)
  (defun do-thing2 (...)
    ... x ...))

The closest thing that C has to this is file-local static variables; it
doesn't let you get more specific than file-level granularity.

-- 
Barry Margolin, ··············@level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Dave
Subject: Re: [newbie] Local var.
Date: 
Message-ID: <ufBVa.212712$lK4.6188882@twister1.libero.it>
"Barry Margolin" <··············@level3.com> ha scritto nel messaggio
····················@news.level3.com...

[snip]

> The difference is that you can only have one global variable named X.
But
> each time you create a closure, it's a *different* variable.
>
> (let ((x 0))
>   (defun do-something (...)
>     ... x ...))
>
> (let ((x 1))
>   (defun do-something-else (...)
>     ... x ...))
>

It's wonderful!
But in C language the static variables remain allocated in memory during
all executing of program .
Does LISP run in the same way?
Thanks.

greetings
Dave
From: Johan Kullstam
Subject: Re: [newbie] Local var.
Date: 
Message-ID: <87d6fs8dj4.fsf@comcast.net>
"Dave" <·········@iol.it> writes:

> "Barry Margolin" <··············@level3.com> ha scritto nel messaggio
> ····················@news.level3.com...
> 
> [snip]
> 
> > The difference is that you can only have one global variable named X.
> But
> > each time you create a closure, it's a *different* variable.
> >
> > (let ((x 0))
> >   (defun do-something (...)
> >     ... x ...))
> >
> > (let ((x 1))
> >   (defun do-something-else (...)
> >     ... x ...))
> >
> 
> It's wonderful!
> But in C language the static variables remain allocated in memory during
> all executing of program .
> Does LISP run in the same way?

The enclosed variable will stick around for as long as you need it.
If you, say, redefine DO-SOMETHING, then its X will probably be reaped
by the garbage collector at some point.

-- 
Johan KULLSTAM
From: Edi Weitz
Subject: Re: [newbie] Local var.
Date: 
Message-ID: <87fzkpk3pr.fsf@bird.agharta.de>
"Dave" <·········@iol.it> writes:

> I want to declare a static variable within a function.  Is it
> possible?  In C language it's equal to "static int x;".

Something like that?

  * (let ((counter 0))
      (defun increment-counter (&optional (amount 1))
        (incf counter amount))
      (defun show-counter ()
        counter)
      (defun reset-counter ()
        (setq counter 0)))
  ; Converted INCREMENT-COUNTER.
  ; Converted SHOW-COUNTER.
  ; Converted RESET-COUNTER.

  RESET-COUNTER
  * (show-counter)

  0
  * (increment-counter)

  1
  * (show-counter)

  1
  * (increment-counter 41)

  42
  * (show-counter)

  42
  * (reset-counter)

  0
  * (show-counter)

  0

That's called a "closure".

Edi.
From: Dave
Subject: Re: [newbie] Local var.
Date: 
Message-ID: <4VwVa.211930$lK4.6156779@twister1.libero.it>
"Edi Weitz" <···@agharta.de> ha scritto nel messaggio
···················@bird.agharta.de...

[snip]

>
> That's called a "closure".
>
> Edi.

Thanks all for the explanations.
Closure is a term that I have not never used
and I will have to study very well, it seems to me... :-)

greetings
Dave
From: james anderson
Subject: Re: [newbie] Local var.
Date: 
Message-ID: <3F26AC5C.B08BF70D@setf.de>
Dave wrote:
> 
> "Edi Weitz" <···@agharta.de> ha scritto nel messaggio
> ···················@bird.agharta.de...
> 
> [snip]
> 
> >
> > That's called a "closure".
> >
> > Edi.
> 
> Thanks all for the explanations.
> Closure is a term that I have not never used
> and I will have to study very well, it seems to me... :-)

even though, as i now observe, you will not find the term "closure" indexed
there in this sense, and you will therefor have to acquaint yourself with the
related concept of lexical bindings, you might be well served to note that mit
press provides sicp online ( http://mitpress.mit.edu/sicp/ )

> 
> greetings
> Dave