summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sieve/eratosthenes.scm37
1 files changed, 37 insertions, 0 deletions
diff --git a/sieve/eratosthenes.scm b/sieve/eratosthenes.scm
new file mode 100644
index 0000000..d9da30d
--- /dev/null
+++ b/sieve/eratosthenes.scm
@@ -0,0 +1,37 @@
+
+
+
+(add-to-load-path (dirname (current-filename)))
+
+(import
+ (srfi srfi-41)
+ (extra-functional)
+ (my-streams))
+
+
+
+(define base (stream-from 2))
+
+
+
+(define (mark n item)
+ (if (eq? (remainder (car item) n) 0)
+ (cons (car item) #f)
+ item))
+
+
+
+(define-stream (sieve input)
+ (stream-let loop ((strm (stream-map (part (flip cons) #t) input)))
+ (stream-match strm ((item . rest)
+ (if (cdr item)
+ (stream-cons
+ (car item)
+ (loop (stream-map (part mark (car item)) rest)))
+ (loop rest))))))
+
+
+
+(define eratosthenes (sieve base))
+
+