From: dominik dietrich
Subject: variables in functions
Date: 
Message-ID: <bdev6f$sc4b7$1@ID-140695.news.dfncis.de>
I want to define a function inc() which does the following:

first call of inc returns 1
second call of inc returns 2
....

To solve this problem, i have to save how often inc was called. I don't want
to use a global symbol (setq). i don't want to have access to the variable
outside of inc.

thank you

Dominik

From: Jim Bushnell
Subject: Re: variables in functions
Date: 
Message-ID: <t8ucndSyM8yfi2ajXTWJiQ@comcast.com>
"dominik dietrich" <·······@gmx.de> wrote in message
···················@ID-140695.news.dfncis.de...
> I want to define a function inc() which does the following:
>
> first call of inc returns 1
> second call of inc returns 2
> ....
>
> To solve this problem, i have to save how often inc was called. I don't
want
> to use a global symbol (setq). i don't want to have access to the variable
> outside of inc.
>
> thank you
>
> Dominik
>
>

What Nils is referring to is probably something like:

(let ((inc-var 0))
    (defun inc ()
         (incf inc-var)))

Jim Bushnell
From: Kaz Kylheku
Subject: Re: variables in functions
Date: 
Message-ID: <cf333042.0306261015.628fbae5@posting.google.com>
"dominik dietrich" <·······@gmx.de> wrote in message news:<··············@ID-140695.news.dfncis.de>...
> I want to define a function inc() which does the following:
> 
> first call of inc returns 1
> second call of inc returns 2
> ....
> 
> To solve this problem, i have to save how often inc was called. I don't want
> to use a global symbol (setq). i don't want to have access to the variable
> outside of inc.

One solution is to use a symbol which is in your own package, and not
exported.

Another is lexical scope:

  (let ((variable 0))
    (defun inc ()
      (incf variable))

    (defun dec ()
      (decf variable)))


A DEFUN expression doesn't have to be at the top level: it can be
enclosed in a lexical environment. The body of a function defined by
DEFUN is in fact wrapped in a closure which captures the lexical
environment at the time the DEFUN is evaluated. This means that there
is just one instance of the variable; all the calls to INC and DEC see
the same variable.
From: Pascal Costanza
Subject: Re: variables in functions
Date: 
Message-ID: <bdevh4$v4i$1@f1node01.rhrz.uni-bonn.de>
Is this a homework assignment? Then you should say so.

Do you know about closures?

Pascal

dominik dietrich wrote:
> I want to define a function inc() which does the following:
> 
> first call of inc returns 1
> second call of inc returns 2
> ....
> 
> To solve this problem, i have to save how often inc was called. I don't want
> to use a global symbol (setq). i don't want to have access to the variable
> outside of inc.
> 
> thank you
> 
> Dominik

-- 
Pascal Costanza               University of Bonn
···············@web.de        Institute of Computer Science III
http://www.pascalcostanza.de  R�merstr. 164, D-53117 Bonn (Germany)
From: dominik dietrich
Subject: Re: variables in functions
Date: 
Message-ID: <bdf3dc$rkh6j$1@ID-140695.news.dfncis.de>
no, it isn't. so far, i have used sml and now i'm trying to learn lisp.

"Pascal Costanza" <········@web.de> schrieb im Newsbeitrag
·················@f1node01.rhrz.uni-bonn.de...
> Is this a homework assignment? Then you should say so.
>
> Do you know about closures?
>
> Pascal
>
> dominik dietrich wrote:
> > I want to define a function inc() which does the following:
> >
> > first call of inc returns 1
> > second call of inc returns 2
> > ....
> >
> > To solve this problem, i have to save how often inc was called. I don't
want
> > to use a global symbol (setq). i don't want to have access to the
variable
> > outside of inc.
> >
> > thank you
> >
> > Dominik
>
> -- 
> Pascal Costanza               University of Bonn
> ···············@web.de        Institute of Computer Science III
> http://www.pascalcostanza.de  R�merstr. 164, D-53117 Bonn (Germany)
>
From: Pascal Costanza
Subject: Re: variables in functions
Date: 
Message-ID: <bdf56g$kuu$1@f1node01.rhrz.uni-bonn.de>
dominik dietrich wrote:
> no, it isn't. so far, i have used sml and now i'm trying to learn lisp.

OK, so here we go: ;)

(let ((i 0))
   (defun inc ()
     (incf i)))

...and if you want to make sure that you can have more than instance of 
this:

(defun make-inc (&optional (value 0))
   (lambda ()
     (incf value)))

(setf myinc (make-inc 4))

(funcall myinc)
=> 5


BTW, this is Common Lisp. In Scheme, things are a little bit different. 
Check out comp.lang.scheme for more details.

Furthermore, try to get one of the tutorials / specs on the net. It's 
better to learn with those.

Recommendations:

- CLtL2, 
http://www-2.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/html/cltl/cltl2.html

- HyperSpec, http://www.lispworks.com/reference/HyperSpec/

- Paul Graham, On Lisp, http://www.paulgraham.com/onlisp.html

See also http://www.lisp.org/table/learn.htm, 
http://www.paulgraham.com/booklinks.html and 
http://www.paulgraham.com/resources.html

Ah, yes, and perhaps http://www.pascalcostanza.de/lisp/guide.html ;)

For Scheme, http://www.scheme.org and http://www.schemers.org are good 
starting points.

Pascal

>>dominik dietrich wrote:
>>
>>>I want to define a function inc() which does the following:
>>>
>>>first call of inc returns 1
>>>second call of inc returns 2
>>>....

-- 
Pascal Costanza               University of Bonn
···············@web.de        Institute of Computer Science III
http://www.pascalcostanza.de  R�merstr. 164, D-53117 Bonn (Germany)
From: Nils Goesche
Subject: Re: variables in functions
Date: 
Message-ID: <87he6cnasg.fsf@darkstar.cartan>
"dominik dietrich" <·······@gmx.de> writes:

> I want to define a function inc() which does the following:
> 
> first call of inc returns 1
> second call of inc returns 2
> ....
> 
> To solve this problem, i have to save how often inc was
> called. I don't want to use a global symbol (setq). i don't
> want to have access to the variable outside of inc.

Looks like what you want is a lexical variable, then.  These are
created with LET...

Regards,
-- 
Nils G�sche
Ask not for whom the <CONTROL-G> tolls.

PGP key ID #xD26EF2A0