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
"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
"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.
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)
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)
>
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)
"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