Re: Implementing closures using first-class environments in LAMBDA

From: Nils M Holm (nmh_at_despammed.com)
Date: 09/30/04


Date: Thu, 30 Sep 2004 06:57:07 +0000 (UTC)

Matthew Danish <mrd+nospam@cmu.edu> wrote:
> Congratulations. This is how closures are usually implemented.

Thanks.

> Well, ok, not like that. At first, I didn't realize you meant
>
> (LAMBDA (Y) (CONS X Y) ((X . VALUE)))

In what way are closures implemented usually? Since you emphasize
the dot: using lists of lists rather than lists of pairs? That would
not make a big difference, would it?

> Rather than make it complicated by referring to ``escape'' why don't
> you just make this evaluation rule for LAMBDA:

I had thought about implementing it this way, but this method
would attempt to create closures in a lot of situations where
it is not really neccessary. In particular, functions of the form

(LABEL ((F (LAMBDA ...))
        (G (LAMBDA ...))
        ...
    EXPR))

would involve a lot of superflous computations when none of its
functions actually escapes.

And then, detecting an escaping function is not that hard. When
unbinding function arguments, you check whether the result
expression is a lambda function and if it is, you create a
closure.

In my implementation, these are two lines of code and save a
lot of unneccessary calls to the routine that creates closures.

Nils

-- 
Nils M Holm <nmh@despammed.com> -- http://www.t3x.org/nmh/


Relevant Pages

  • Re: Arity of a closure and a Lisp Community Process
    ... to handlers based on their definition, rather than requiring some ... which makes me very doubtful of closures. ... argument lists of these closures available to the code, ... European Common Lisp Meeting, Amsterdam, April 19/20, 2008 ...
    (comp.lang.lisp)
  • Re: newLISP is simple, terse, and well documented
    ... is "an apparent Schemer", that's really classic. ... It's so easy to mold your environment into something more ... And yeah, funcallable lists can do a lot of things that closures can, ...
    (comp.lang.lisp)
  • Re: easily embedding html into Lisp
    ... exactly as the example LIST-HTML-TABLE function I gave above did. ... You can also pass closures to other routines to let them do "callbacks" ... you to provide a callback function which is called at various points ... is well-suited to displaying a list of lists which are the results ...
    (comp.lang.lisp)