Hi,
I use a version of lisp that does not support lexical closure.
For example, I want to do the following:
(defun makeAdder (x) (function (lambda (y) (+ x y))))
(defun test()
(setq plus3 (makeAdder 3))
(funcall plus3 5)
)
And my lisp interpreter says error: x is undefined.
I am not an expert at lisp but I think it is possible to simulate
lexical closure with list/structure/macros or something.
Does anybody know how to do it?
Any suggestions are very appreciated.
Thanks
sc wrote:
> Hi,
> I use a version of lisp that does not support lexical closure.
> For example, I want to do the following:
>
> (defun makeAdder (x) (function (lambda (y) (+ x y))))
>
> (defun test()
> (setq plus3 (makeAdder 3))
> (funcall plus3 5)
> )
>
> And my lisp interpreter says error: x is undefined.
> I am not an expert at lisp but I think it is possible to simulate
> lexical closure with list/structure/macros or something.
> Does anybody know how to do it?
> Any suggestions are very appreciated.
> Thanks
If you take out the 'function' you should be fine. You only need
lambda for closures:
(defun make-adder (x)
(lambda (y) (+ x y))
(defun test ()
(setq plus3 (make-adder 3))
(funcall plus3 5))
(message (Hello 'Jimmy)
(you :wrote :on '(15 Jan 2007 11:48:26 -0800))
(
J> If you take out the 'function' you should be fine. You only need
J> lambda for closures:
RT.. CLHS!
CL-USER> (macroexpand-1 '(lambda (x) x))
(FUNCTION (LAMBDA (X) X))
T
)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"People who lust for the Feel of keys on their fingertips (c) Inity")
Alex Mizrahi wrote:
> (message (Hello 'Jimmy)
> (you :wrote :on '(15 Jan 2007 11:48:26 -0800))
> (
>
> J> If you take out the 'function' you should be fine. You only need
> J> lambda for closures:
>
> RT.. CLHS!
>
> CL-USER> (macroexpand-1 '(lambda (x) x))
> (FUNCTION (LAMBDA (X) X))
> T
>
> )
> (With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
> "People who lust for the Feel of keys on their fingertips (c) Inity")
Thanks for pointing that out, I didn't know that.
"sc" <·······@inbox.ru> writes:
> Hi,
> I use a version of lisp that does not support lexical closure.
> For example, I want to do the following:
>
> (defun makeAdder (x) (function (lambda (y) (+ x y))))
>
> (defun test()
> (setq plus3 (makeAdder 3))
> (funcall plus3 5)
> )
>
> And my lisp interpreter says error: x is undefined.
> I am not an expert at lisp but I think it is possible to simulate
> lexical closure with list/structure/macros or something.
> Does anybody know how to do it?
Have a look at defun* and lexical-let in cl.el (and cl-macs.el).
Of course, if your lisp that does not support lexical closure is not
emacs lisp, you may have some work to port it, but it should give you
some ideas.
--
__Pascal Bourguignon__ http://www.informatimago.com/
"This machine is a piece of GAGH! I need dual Opteron 850
processors if I am to do battle with this code!"
Pascal Bourguignon <···@informatimago.com> writes:
[...]
> Have a look at defun* and lexical-let in cl.el (and cl-macs.el).
>
> Of course, if your lisp that does not support lexical closure is not
> emacs lisp, you may have some work to port it, but it should give you
> some ideas.
And if it *is* emacs lisp, then trying Miles Bader's lexbind branch
might be worth a go:
<http://article.gmane.org/gmane.emacs.devel/49352>
(When I tried it last, it didn't really seem to work for me. Seemed
to work in interpreted mode, but I couldn't get the byte compiler to
work properly. Possibly that was my fault, or it's changed since.)
(message (Hello 'sc)
(you :wrote :on '(15 Jan 2007 11:31:15 -0800))
(
s> I use a version of lisp that does not support lexical closure.
s> For example, I want to do the following:
s> (defun makeAdder (x) (function (lambda (y) (+ x y))))
s> (defun test()
s> (setq plus3 (makeAdder 3))
s> (funcall plus3 5)
s> )
s> And my lisp interpreter says error: x is undefined.
s> I am not an expert at lisp but I think it is possible to simulate
s> lexical closure with list/structure/macros or something.
s> Does anybody know how to do it?
i've got discussion with pythonista about that recently..
you'd better choose another Lisp :). adding closures should be roughly same
complexity as writting new interpreter.
you can do it with macro, but that macro will need to rewrite all the code
to find free variables in lambdas and replace them with env access, to
maintain env creation, to bundle env together with lambda code (and rewrite
funcall).
)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"People who lust for the Feel of keys on their fingertips (c) Inity")