[plt-scheme] Closures

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Nov 5 16:48:17 EST 2007

On Nov 5, 2007, at 4:38 PM, Joel J. Adamson wrote:

> Matthias Felleisen <matthias at ccs.neu.edu> writes:
>
>
> [...]
>
>> ( This may sound expensive; it really isn't if your
>> compiler is any good.)
>
> So, what you're saying is that at any time that I need a particular
> environment, I can resurrect it with a data structure, be that a


Nope. Again, the environment is a data representation of the lexical  
context of some phrase in your program. Every single phrase has a  
lexical context. It is the interpreter that turns the lexical context  
into an environment as it processes your program.

If the phrase is a function definition

   ... (define (f x) ...) ...   ;; in Scheme
   ... int f(int x) { ... } ... ;; in a C-ish syntax

then the interpreter uses the current environment to build another  
data structure: the closure.

The closure is a data representation that is just a struct with two  
fields:

  -- one field that points to the environment of the function definition
  -- another field that points to the code of the function definition

That's all.

Time to read HtDP and PLAI now. -- Matthias










> struct, hash, anything else?  To get into details, how do programmers
> usually do this (which data structure)?  How is this different or any
> better than invoking the continuation of the original definition?







>
> Thanks,
> Joel
> -- 
> Joel J. Adamson
> Biostatistician
> Pediatric Psychopharmacology Research Unit
> Massachusetts General Hospital
> Boston, MA  02114
> (617) 643-1432
> (303) 880-3109
>
>
> The information transmitted in this electronic communication is  
> intended only for the person or entity to whom it is addressed and  
> may contain confidential and/or privileged material. Any review,  
> retransmission, dissemination or other use of or taking of any  
> action in reliance upon this information by persons or entities  
> other than the intended recipient is prohibited. If you received  
> this information in error, please contact the Compliance HelpLine  
> at 800-856-1983 and properly dispose of this information.



Posted on the users mailing list.