2 Apr 2003 12:39
Delay and dynamic extent
> Note that the lambda form does not close over the dynamic
> environment so why should it be different from delay? Well if you
> did this the "dynamic environment" would in fact be the lexical
> environment so you would lose all the benefits of dynamic binding.
> When one writes a function with lambda there is an expectation that
> the dynamic environment is abstracted over. However when using a
> DELAY the expectation is that only the evaluation time is changed,
> not the computation being performed (unless side-effects are
> performed that expose the evaluation order).
I'm afraid I can see problems with that characterization of delay. I'd
also like to show an example that seems to demonstrate why delay
should _not_ be closed over the dynamic environment.
It is sometimes convenient to invert an iteration "inside-out" --
turn an iteration over a collection into a lazy list of the traversed
elements. Generators in Icon, Ruby and now Python use a similar
trick. Here's a generic iteration inverter:
(define (foreach->lazy-list foreach-fn collection)
(delay
(call-with-current-continuation
(lambda (k-main)
(foreach-fn
(lambda (val)
(call-with-current-continuation
(lambda (k-reenter)
(k-main (cons val
(delay
(Continue reading)
RSS Feed