guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strange behavior with delayed objects


From: Linas Vepstas
Subject: Re: Strange behavior with delayed objects
Date: Mon, 10 May 2010 11:26:04 -0500

On 7 May 2010 03:09, user8472 <address@hidden> wrote:
>
> Please find the code for streams and the integration below.
> ;; The troublesome procedure
> (define (solve f y0 dt)
>  (define y (integral (delay dy) y0 dt))
>  (define dy (stream-map f y))
>  y)
>
> ;; This works
> (define y (integral (delay dy) 1 0.001))
> (define dy (stream-map (lambda (x) x) y))
> (stream-ref y 1000)
>
> ;; This doesn't work
> ;(stream-ref (solve (lambda (x) x) 1 0.001) 1000)
> </code>

Well, you modified your code enough so that let* now works fine,
at least for me:

guile> (define (solve f y0 dt)
... (let* ((y (integral (delay dy) y0 dt))
... (dy (stream-map f y))
... )
... y))
guile> (stream-ref (solve (lambda (x) x) 1 0.001) 1000)
2.7169239322359

Superficially, I want to say "this is why let* was invented, to
ensure ordering of definitions".  In practice, I see that dy is
used in both, so there's a circular reference going on here.
The thing that saves you, and allows the  let* to work, is the
(delay dy) which avoids evaluating dy (which is undefined,
at the time its encountered).  Without the delay, the circular
references would cause the whole thing would fall apart,
and not even letrec would save you.

As proof of this, here's another example, without let*, which
does enforce ordering (and which depends on the (delay dy)
to avoid the "use of variable dy before its defined" error):

guile> (define (solve f y0 dt)
... (define y (integral (delay dy) y0 dt))
... (let ((dy (stream-map f y)))
... y))
guile> (stream-ref (solve (lambda (x) x) 1 0.001) 1000)
2.7169239322359
guile>


I'm an amateur at scheme, but I beleive what I say is more
or less correct ...

--linas



reply via email to

[Prev in Thread] Current Thread [Next in Thread]