From: sc
Subject: lexical closure simulation
Date: 
Message-ID: <1168889475.637337.25360@a75g2000cwd.googlegroups.com>
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

From: Jimmy
Subject: Re: lexical closure simulation
Date: 
Message-ID: <1168890505.324369.230030@11g2000cwr.googlegroups.com>
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))
From: Alex Mizrahi
Subject: Re: lexical closure simulation
Date: 
Message-ID: <45abdcaf$0$49201$14726298@news.sunsite.dk>
(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") 
From: Jimmy
Subject: Re: lexical closure simulation
Date: 
Message-ID: <1168892253.666542.106540@m58g2000cwm.googlegroups.com>
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.
From: Pascal Bourguignon
Subject: Re: lexical closure simulation
Date: 
Message-ID: <87mz4k12se.fsf@thalassa.informatimago.com>
"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!"
From: Bruce Stephens
Subject: Re: lexical closure simulation
Date: 
Message-ID: <87slebyesh.fsf@cenderis.demon.co.uk>
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.)
From: Alex Mizrahi
Subject: Re: lexical closure simulation
Date: 
Message-ID: <45abde42$0$49199$14726298@news.sunsite.dk>
(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")