Home > Archive > Scheme > March 2007 > Lazy streams in scheme?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Lazy streams in scheme?
|
|
| swizard 2007-03-27, 4:22 am |
| Hello. I'm stuck with a lazy stream implementation in scheme, maybe
someone can help me?
Here are my base definitions (partially stolen from SICP):
(define-macro (stream-cons p q)
(let
((pv (gensym)) (qv (gensym)))
`(let
((,pv (lambda () ,p)) (,qv (lambda () ,q)))
(lambda (f) (f ,pv ,qv)))))
(define stream-car
(lambda (s) (s (lambda (p q) (p)))))
(define stream-cdr
(lambda (s) (s (lambda (p q) (q)))))
(define stream-for-each
(lambda (func stream)
(if (null? stream)
'done
(begin
(func (stream-car stream))
(stream-for-each func (stream-cdr stream))))))
The following stream "ones" work pretty fine:
(define ones (stream-cons 1 ones))
"stream-for-each" across such list can run infinitely. But when I try to
define something more complex, it works also, but consumes memory. For
example:
(define stream-map
(lambda (func . streams)
(if (or
(null? streams)
(< 0
(apply +
(map (lambda (s) (if (null? s) 1 0)) streams))))
'()
(stream-cons
(apply func (map stream-car streams))
(apply
stream-map
(cons func (map stream-cdr streams)))))))
(define nums (stream-cons 1 (stream-map + ones nums)))
or even
(define number-stream
(lambda (number) (stream-cons number (number-stream number))))
The "stream-for-each" procedure across such streams fails on some big
iteration count, complaining "Exhausted storage" for Pocket Scheme or
even with crash on signal 11 for qscheme.
Is it bug of GC, algorithm or maybe my brain?
| |
| andreuri2000@yahoo.com 2007-03-28, 7:12 pm |
| On Mar 27, 3:33 am, swizard <m...@swizard.info> wrote:
> But when I try to
> define something more complex, it works also, but consumes memory.
You may refer to SRFI-45 at srfi.schemers.org
for a discussion of this issue.
Andre
| |
| swizard 2007-03-30, 7:07 pm |
| andreuri2000@yahoo.com wrote:
> On Mar 27, 3:33 am, swizard <m...@swizard.info> wrote:
>
> You may refer to SRFI-45 at srfi.schemers.org
> for a discussion of this issue.
>
> Andre
>
Thank you, that is exactly what I need!
|
|
|
|
|