For loops with per-iteration variables

Mark S. Miller erights at google.com
Mon Oct 13 21:38:26 PDT 2008


On Mon, Oct 13, 2008 at 9:27 PM, David-Sarah Hopwood
<david.hopwood at industrial-designers.co.uk> wrote:
> MarkM is right that it just falls out of the natural tail-recursive
> encoding of a for loop. I got the above expansion wrong by trying to do
> it imperatively, which was silly -- the tail-recursive expansion is much
> simpler:
>
>  for (let i = initExpr; condExpr; updateExpr) { body }
> ==>
>  { let ($loop = lambda(i) {
>           if (condExpr) {
>             { body } (updateExpr); $loop(i);
>           }
>         }) { $loop(initExpr); } }

Close. The problem is that updateExpr modifies the same i that's in
scope in body, so any closure mentioning i in body captures an i
that's modified by the update expression.

-- 
    Cheers,
    --MarkM


More information about the Es-discuss mailing list