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: user8472
Subject: Re: Strange behavior with delayed objects
Date: Fri, 7 May 2010 01:09:41 -0700 (PDT)

Please find the code for streams and the integration below. The macro
definition of cons-stream is not from SICP since it is implementation
dependent (for this reason I have also not tried the code on other Scheme
implementations except Guile). So this could be buggy.

I don't think it is only an issue of delay/force mismatch - the strange
thing is that the code *works* at the REPL, but does *not work* inside a
procedure definition.

<code>
;; Basic stream operations
(define-macro (cons-stream a b)
  `(cons ,a (delay ,b)))
(define (stream-car stream) (car stream))
(define (stream-cdr stream) (force (cdr stream)))
(define the-empty-stream '())
(define stream-null? null?)
(define (stream-ref s n)
  (if (= n 0)
      (stream-car s)
      (stream-ref (stream-cdr s) (- n 1))))

;; Other generic stream operations
(define (stream-map proc . argstreams)
  (if (stream-null? (car argstreams))
      the-empty-stream
      (cons-stream
       (apply proc (map stream-car argstreams))
       (apply stream-map
              (cons proc (map stream-cdr argstreams))))))
(define (add-streams s1 s2)
  (stream-map + s1 s2))
(define (scale-stream stream factor)
  (stream-map (lambda (x) (* x factor)) stream))

;; Integral with delayed integrand
(define (integral delayed-integrand initial-value dt)
  (define int
    (cons-stream initial-value
                 (let ((integrand (force delayed-integrand)))
                   (add-streams (scale-stream integrand dt)
                                int))))
  int)

;; 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>



Linas Vepstas-3 wrote:
> 
> On 4 May 2010 11:59, user8472 <address@hidden> wrote:
> [...]
>> streams.scm:601:14: While evaluating arguments to stream-map in
>> expression
>> (stream-map f y):
>> streams.scm:601:14: Variable used before given a value: y
>> ABORT: (unbound-variable)
>>
>> So something is still not quite right.
> 
> My bad, I completely failed to notice that delayed evaluation
> was a part of the problem.  Can you simplify the example to
> its barest elements that still provoke the error? Hopefully,
> this will provide a clue to what the true as to what the
> problem is. (Not having seen the definition of "integral",
> I'm wondering whether some failure to initialize y to y0
> might have something to do with it ... some missing force
> somewhere ...  but that's a wild and probably wrong guess).
> 

-- 
View this message in context: 
http://old.nabble.com/Strange-behavior-with-delayed-objects-tp28443452p28483371.html
Sent from the Gnu - Guile - User mailing list archive at Nabble.com.





reply via email to

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