summaryrefslogtreecommitdiff
path: root/sieve
diff options
context:
space:
mode:
Diffstat (limited to 'sieve')
-rw-r--r--sieve/euler.scm16
-rw-r--r--sieve/sundaram.scm16
2 files changed, 18 insertions, 14 deletions
diff --git a/sieve/euler.scm b/sieve/euler.scm
index b3ba946..ac23a94 100644
--- a/sieve/euler.scm
+++ b/sieve/euler.scm
@@ -16,17 +16,21 @@
-(define-stream (sub-merge x y)
- (if (eq? (stream-car x) (stream-car y))
- (sub-merge (stream-cdr x) (stream-cdr y))
- (stream-cons (stream-car x) (sub-merge (stream-cdr x) y))))
+(define-stream (stream-ordered-diff xstrm ystrm)
+ (stream-match xstrm (() '()) ((x . xs)
+ (stream-match ystrm (() xstrm) ((y . ys)
+ (cond ((< x y) (stream-cons x (stream-ordered-diff xs ystrm)))
+ ((> x y) (stream-ordered-diff xstrm ys))
+ (else (stream-ordered-diff xs ys))))))))
+
(define-stream (sieve input)
(stream-cons
(stream-car input)
- (sieve (sub-merge (stream-cdr input)
- (stream-map ((curry *) (stream-car input)) input)))))
+ (sieve (stream-ordered-diff
+ (stream-cdr input)
+ (stream-map ((curry *) (stream-car input)) input)))))
diff --git a/sieve/sundaram.scm b/sieve/sundaram.scm
index 457efb8..384bc3d 100644
--- a/sieve/sundaram.scm
+++ b/sieve/sundaram.scm
@@ -19,9 +19,9 @@
(define-stream (stream-ordered-diff xstrm ystrm)
(stream-match xstrm (() '()) ((x . xs)
(stream-match ystrm (() xstrm) ((y . ys)
- (if (eq? x y)
- (stream-ordered-diff xs ys)
- (stream-cons x (stream-ordered-diff xs ystrm))))))))
+ (cond ((< x y) (stream-cons x (stream-ordered-diff xs ystrm)))
+ ((> x y) (stream-ordered-diff xstrm ys))
+ (else (stream-ordered-diff xs ys))))))))
@@ -30,7 +30,7 @@
(stream-match ystrm (() xstrm) ((y . ys)
(cond ((< x y) (stream-cons x (stream-merge xs ystrm)))
((> x y) (stream-cons y (stream-merge xstrm ys)))
- ((eq? x y) (stream-cons x (stream-merge xs ys)))))))))
+ (else (stream-cons x (stream-merge xs ys)))))))))
@@ -48,11 +48,11 @@
(define ij-stream
(stream-let loop ((n 1) (strm (i-stream 1)))
- (let* ((cutoff ((curry >) (stream-car (i-stream n))))
- (next (+ n 1))
+ (let* ((next (+ n 1))
+ (cutoff ((curry >) (stream-car (i-stream next))))
(available (stream-take-while cutoff strm))
- (remainder (stream-drop-while cutoff strm)))
- (stream-append available (loop next (stream-merge remainder (i-stream next)))))))
+ (remaining (stream-drop-while cutoff strm)))
+ (stream-append available (loop next (stream-merge remaining (i-stream next)))))))