"Staggering power of closures..."

From: Dave Roberts (ldave-re-move_at_re-move.droberts.com)
Date: 03/28/04


Date: Sun, 28 Mar 2004 19:14:07 GMT

I managed to miss the great C vs. Lisp speed challenge earlier this month
when I was traveling (titled "Re: off-topic: open challenge to Christian
Lynbeck"). In the course of going back and reading that thread (good job,
guys, BTW! I learned a whole lot about Lisp optimization that I never would
have learned otherwise), I found a posting by Matthew Danish where he says:

> Perhaps one of the more difficult parts of learning Lisp from a C
> background is the realization of the power of the constructs, and
> acquiring the knowledge to know where and how to use them. A good
> example of this is closures. When I explain the concept to a typical C
> programmer, they often say something like ``oh, you mean static
> variables in functions'' or ``so what?'' Unless the person is versed in
> computability theory (and/or the lambda calculus) it is difficult for
> them to comprehend at first the staggering power of such a simple idea.
> The concept of a closure is a feature which is almost universal to
> high-level languages, and when you understand it is hard to imagine
> getting by without it.

This struck me. I was a C programmer, learned a whole lot of Java, and am
now trying to learn a whole lot of CL. As I have approached the language,
everybody keeps talking about closures as being one of the mind-blowing
pieces of it. Perhaps that's true when you're coming strictly from C, but I
find that I'm not as blown away with them after playing in the OOP world
for a while (C++, Java, etc. Yes, I know those object models have
issues--that's why I'm learning CL now). Whenever I see a closure, I simply
see an object. It's one or more functions connected to a local environment
that holds some private bindings.

In reading Paul Graham's papers on Lisp, I was struck that he was actually a
bit negative on OOP (CLOS, specifically), because he thought that most
everything you needed could be done with functions and closures.

So the question is, am I missing something staggering about closures, or
have I just grasped the idea so well before that I'm missing it now? Is
there something with closures that can't be done with objects in a Java
world, for instance? If so, what?

Note that I would actually rank first-class functions and powerful macros as
more staggering than closures as I have been learning CL. My point isn't
that CL doesn't have staggering features or even that closures aren't one
of them if you're coming from a world without objects. My question is
really, what am I missing about closures, if anything?

-- 
Dave Roberts
ldave-re-move@re-move.droberts.com


Relevant Pages

  • Re: Java or C++?
    ... >>> I don't see how scope comes into it, scope in lisp isn't that much ... > Do you mean that lisp is different in the presence of closures. ... In the presence of an generational GC like OCaml's, a pushed stack element ...
    (comp.programming)
  • Re: History of lexical scoping in Scheme and other Lisps?
    ... > Lisp standard) that Scheme introduced lexical scoping and lexical ... Does that mean that all Lisp dialects prior to Scheme used ... The first example of lexical scoping and lexical closures can be found in ...
    (comp.lang.lisp)
  • Re: The LOOP macro
    ... In Lisp, you can build your own pattern matching layer on top of what's ... language features from other languages and reimplement them on top of, ... everyone would ask him to use closures instead... ... make you think about what abstractions you _actually_ need. ...
    (comp.lang.lisp)
  • Matthew Danish must never say "stagger" again [was Re: "Staggering power of closures.
    ... I learned a whole lot about Lisp optimization that I never would ... >>example of this is closures. ... >>them to comprehend at first the staggering power of such a simple idea. ... > have I just grasped the idea so well before that I'm missing it now? ...
    (comp.lang.lisp)
  • Re: A simple interpreter
    ... I think this comes with lexical scopes. ... > (lambda nil x) ... I was hoping one of the ancient Lisp hackers would speak up; ... The issue of closures in a dynamic ...
    (comp.lang.lisp)