[racket] What does "delay" delay?

From: Deren Dohoda (deren.dohoda at gmail.com)
Date: Fri Nov 18 14:20:25 EST 2011

It would have helped more had I actually put "delay" in like I
*thought* did, yes. How embarrassing.

When I put delay in, it does behave exactly as I expect, and all
values are identical (and used the later set value).

Sorry about that.

On 11/18/11, The Configurator <configurator at gmail.com> wrote:
> d uses the new value of x. You clearly understood why - because it wasn't
> calculated until it was forced.
>
> However, none of the others do; your call to apply actually computes the
> values; d2, d3, d4 and d5 all have the value 100 before you set x to 20,
> while d has the value "calculate (* x x) at some point in the future".
>
> Hope this helps,
> configurator
>
> 2011/11/18 Deren Dohoda <deren.dohoda at gmail.com>
>
>> Sorry about the other message, the enter key got in the way.
>>
>> Consider this code:
>> ----------------
>> #lang racket
>> (define x 10)
>> (define d (delay ((λ(x) (* x x)) x)))
>> (define d2 (* x x))
>> (define d3 (apply * (list x x)))
>> (define d4 (apply * `(,x ,x)))
>> (define d5 (apply (λ(x) (* x x)) (list x)))
>> (set! x 20)
>>
>> (define (show)
>>  (for-each (λ(v) (displayln (force v))) (list d d2 d3 d4 d5)))
>> -----------------
>> I had expected a call to (show) to either display identical values, or
>> consider a few of them different. Instead, only the very first value
>> yields the substituted value for the variable x; all the others have
>> captured the value of 'x', including 'd5', which seems like it should
>> be identical to 'd' if any of them are.
>>
>> Not that I make a habit of mixing mutation and delay---frankly I
>> forget now why I even fired up DrRacket to test this---but it was
>> quite surprising. The definition of delay says only: "Creates a
>> promise that, when forced, evaluates the bodys to produce its value."
>> I can't find an obvious reason why these five values aren't identical
>> under such a definition.
>>
>> Deren
>>
>> _________________________________________________
>>  For list-related administrative tasks:
>>  http://lists.racket-lang.org/listinfo/users
>



Posted on the users mailing list.