From 2cc6c470d252fd1f30b1f3697615c667d6d6b8f6 Mon Sep 17 00:00:00 2001 From: Joe Marshall Date: Sat, 16 Jan 2016 15:53:58 -0800 Subject: [PATCH] Don't blow out the stack in STREAM->LIST. --- src/runtime/stream.scm | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/runtime/stream.scm b/src/runtime/stream.scm index 6c49ebc35..e0a0e306e 100644 --- a/src/runtime/stream.scm +++ b/src/runtime/stream.scm @@ -265,13 +265,11 @@ USA. '()))) (define (stream->list stream) - (if (stream-pair? stream) - (cons (car stream) - (stream->list (force (cdr stream)))) - (begin - (if (not (null? stream)) - (error:illegal-stream-element stream 'STREAM->LIST 0)) - '()))) + (let loop ((s stream) + (elements '())) + (cond ((stream-pair? tail) (loop (tail s) (cons (head s) elements))) + ((null? s) (reverse elements)) + (else (error:illegal-stream-element stream 'STREAM->LIST 0))))) (define prime-numbers-stream) -- 2.25.1