[plt-scheme] Closures
Sorry, very sorry, but I couldn't resist. This is exactly the
question/answer that launched my career as a PhD student in
programming languages:
Matthias arrives at Indiana on Jan 4, 1984 and drudges thru a foot of
snow to Lindley Hall. He ends up in Dan Friedman's office, whose name
he recognizes from some journal on logic programming. He tells him so.
Dan Friedman: Are you a new PhD student?
me: Yes, I am.
Dan Friedman: Do you know what a closure is?
me: Sure. You take a set, specifically a relation, and you ...
Dan Friedman: Never mind. Take my 311. And just to keep you off the
streets, take Mitch Wand's 616.
me: Okay?
Dan Friedman: And do you have anything to do over the weekend?
me: Why?
Dan Friedman: Let me give you a few things to read.
Under the giggles of Eugene Kohlbecker, Bruce Duba, and John* he
leads me from his office to Nancy's and hands me a stack of papers
and books that are about a foot tall.
Dan Friedman: Nice meeting you. See you Monday.
Shriram's response is pointing you to what I went thru and how I
taught him later.
;; ---
A closure is an internal representation of a first-class, higher-
order function. Mathematicians call these things "operators" on
occasion. For example, the differential operator is something like that:
d/dx : (R -> R) -> (R -> R)
i.e., it consumes a function on the reals and produces such a
function. So you can think of applying it like this:
;; f(x) = ...
(define (f x) ...)
;; f'(x) = d/dx(f)
(define fprime (d/dx f)) ;; do not use the name f' !! (I did!)
;; now use f' on many points ...
... (fprime x1) ...
... (fprime x2) ...
... (fprime x3) ...
and so on.
The representations of f and fprime (and d/dx for that matter) are
closures.
Ditto for integrals, definite integrals, adjuncts, etc.
I am sorry to say that I don't recall much statistics, otherwise I'd
illustrate the point with operators from your field.
;; ---
It turns out that operators are also near-idea mechanisms for working
computer scientists. They facilitate introduce single points of
control for functionality in software, which makes it easy to change
and improve software with little cost and little manpower.
Hth -- Matthias
On Nov 5, 2007, at 10:39 AM, Joel J. Adamson wrote:
> Howdy List,
>
> Can someone give me an intuitive idea of closures, perhaps a clever
> analogy or metaphor?
>
> My question is really motivated by some confusing usage: I understand
> what a closure is frome set perspective, but people speak of closures
> as though they are something function-like, as in one can invoke a
> closure. Schemers speak of "using closures" fairly often.
>
> Please direct me to a relevant FAQ ;)
>
> Thanks,
> Joel
> --
> Joel J. Adamson
> Biostatistician
> Pediatric Psychopharmacology Research Unit
> Massachusetts General Hospital
> Boston, MA 02114
> (617) 643-1432
> (303) 880-3109