From: Kevin P Chugh
Subject: static variable?
Date: 
Message-ID: <4fe6kg$q1n@azure.acsu.buffalo.edu>
hi - how can i setup a static variable so that the first time, and only
the first time a function is entered, the variable is declared and
set to 0 and every time after, i can do a test for the variables
value - i want to do something like :

if count does not exist, declare count, count=0
else
count=count+1 

thanks for any help,
kevin

From: Geert-Jan van Opdorp
Subject: Re: static variable?
Date: 
Message-ID: <GEERT.96Feb9225252@localhost>
In article <··········@azure.acsu.buffalo.edu> ·····@cs.buffalo.edu (Kevin P Chugh) writes:

   hi - how can i setup a static variable so that the first time, and only
   the first time a function is entered, the variable is declared and
   set to 0 and every time after, i can do a test for the variables
   value - i want to do something like :

   if count does not exist, declare count, count=0
   else
   count=count+1 

   thanks for any help,
   kevin


Hi Kevin,
You need to create a lexical variable and then define
your function within it's scope. 
(let ((x 0))
  (defun next-x ()
    (incf x))
  (defun reset-x () ; you can have more functions referring to
    (setf x 0)))    ; this variable. But outside the "Let" it is unknown

By the way, here you can see the difference between these:
'(lambda () (incf x))  ;no lexical closure
#'(lambda () (incf x)) ;#' is short for "(function ...)",
                       ; it creates a lexical closure. "defun"
                       ; does that too.

(let ((x 0))
  (setf f '(lambda () (incf x)))
  (setf g #'(lambda () (incf x))))


> (setf x 100)
100

> (funcall f)
101
> (funcall g)
1
> (funcall g)
2
> x
101


Geert-Jan

·····@aie.nl
From: Aldy Hernandez
Subject: Re: static variable?
Date: 
Message-ID: <m2lomaygxr.fsf@abulafia>
hi.

i'm trying to get yacc (or bison) to generate scheme (or lisp) instead
of C, but the code is fully of goto's.  is there any way of
accomplishing this without having to totally rewrite the generated
code?

thanks in advance

aldy
····@andrews.edu