Adding loop primitives/optimizations (was Making Set/Dictionaryetc. loops more robust)

Joshua Scholar jscholar at access4less.net
Thu Dec 2 04:35:25 UTC 2004


 Richard A. O'Keefe ok at cs.otago.ac.nz
Thu Dec 2 05:11:00 CET 2004
Previous message: Accessing a particular object in GOODS causes "Space is
low" warning
Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

> "Joshua Scholar" <jscholar at access4less.net> wrote:
>  I was thinking about iteration and closures the other day.  Smalltalk
blocks
>  are becoming the equivalent of lambdas, but in Scheme, the block inside
of a
>  loop does not have to be a lambda, and can use the outer environment I
>  think...  So when we slow our loops down in order to create a new
>  environment for each iteration, we're doing something that Scheme and
Lisp
>  didn't I think.  I'll have to write some Scheme programs to be sure.
>
> As an old Lisper I must say I find his description of Scheme
unrecognisable.
> The basic loop form in Scheme is named-LET:
>
>     (let <name> ((<v1> <e1>) ... (<vn> <en>)) <body>)
>
> which is precisely equivalent to
>
>     (letrec ((<name> (lambda (<v1> ... <vn>) <body>)))
>       (<name> <e1> ... <en>))
>
> "do" is also defined in terms of rewriting to a recursive "lambda".
> "In Scheme, the block inside a loop" _is_ (the body of) "a lambda".
> It "can use the outer environment" because so can all lambdas in Scheme.
> And so, of course, can blocks in Smalltalk.
> Scheme _does_ create a new environment (in principle) for each loop
> _entry_ but not for each loop _iteration_.
> And so does Smalltalk.  Squeak does *NOT* "slow our loops down in order
> to create a new environment for each iteration".

I was looking from this example from the Squeak Swiki

|blocks|
blocks := (1 to: 10) collect: [:each | [each]].
blocks first value.
"you would expect this to be 1, but it is 10".

They were claiming that with closures the value would be 1 not 10.

For it to be 1, the environment for each loop would have to be distinct from
the environment from the previous iteration, just as I said.
...

What they do in Scheme I'm not sure, but I have read papers (yes plural) on
plenty of Scheme compilers, and most Scheme compilers analyse the code to
see if they need a new environment (is any variable captured etc) and they
don't bother to created a full environment unless it's needed.

Joshua Scholar




More information about the Squeak-dev mailing list