From 55a9acfb667a74e057b6664fb97e85c9a3baf067 Mon Sep 17 00:00:00 2001 From: Taylor R Campbell Date: Sun, 10 Feb 2019 22:38:44 +0000 Subject: [PATCH] Convert multi-LETREC to internal definitions in stream.scm. --- src/runtime/stream.scm | 47 +++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/runtime/stream.scm b/src/runtime/stream.scm index 8da5bbf86..6e1ef29a4 100644 --- a/src/runtime/stream.scm +++ b/src/runtime/stream.scm @@ -274,32 +274,27 @@ USA. (define prime-numbers-stream) (define (make-prime-numbers-stream) - (cons-stream - 2 - (letrec - ((primes (cons-stream 3 (fixnum-filter 5))) - (fixnum-filter - (let ((limit (fix:- (fix:largest-value) 2))) - (lambda (n) - (if (fix:<= n limit) - (let loop ((ps primes)) - (cond ((fix:< n (fix:* (car ps) (car ps))) - (cons-stream n (fixnum-filter (fix:+ n 2)))) - ((fix:= 0 (fix:remainder n (car ps))) - (fixnum-filter (fix:+ n 2))) - (else - (loop (force (cdr ps)))))) - (generic-filter n))))) - (generic-filter - (lambda (n) - (let loop ((ps primes)) - (cond ((< n (square (car ps))) - (cons-stream n (generic-filter (+ n 2)))) - ((= 0 (remainder n (car ps))) - (generic-filter (+ n 2))) - (else - (loop (force (cdr ps))))))))) - primes))) + (let ((limit (fix:- (fix:largest-value) 2))) + (define odd-primes (cons-stream 3 (fixnum-filter 5))) + (define (fixnum-filter n) + (if (fix:<= n limit) + (let loop ((ps odd-primes)) + (cond ((fix:< n (fix:* (car ps) (car ps))) + (cons-stream n (fixnum-filter (fix:+ n 2)))) + ((fix:= 0 (fix:remainder n (car ps))) + (fixnum-filter (fix:+ n 2))) + (else + (loop (force (cdr ps)))))) + (generic-filter n))) + (define (generic-filter n) + (let loop ((ps odd-primes)) + (cond ((< n (square (car ps))) + (cons-stream n (generic-filter (+ n 2)))) + ((= 0 (remainder n (car ps))) + (generic-filter (+ n 2))) + (else + (loop (force (cdr ps))))))) + (cons-stream 2 odd-primes))) (define (initialize-package!) (let ((reset-primes! -- 2.25.1