diff options
author | Jed Barber <jjbarber@y7mail.com> | 2015-10-19 14:37:52 +1100 |
---|---|---|
committer | Jed Barber <jjbarber@y7mail.com> | 2015-10-19 14:37:52 +1100 |
commit | 148112e6e375c79aab7bf0456de1474410533762 (patch) | |
tree | 91cecd382c7575c20ccfcd1275225c4ef0292272 /sieve | |
parent | faeec19d3a971efdc94e86c5fc2d59239b04e84a (diff) |
Euler prime number sieve
Diffstat (limited to 'sieve')
-rw-r--r-- | sieve/euler.scm | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/sieve/euler.scm b/sieve/euler.scm new file mode 100644 index 0000000..b3ba946 --- /dev/null +++ b/sieve/euler.scm @@ -0,0 +1,35 @@ + + + +(use-modules (srfi srfi-41)) + + + +(define base (stream-from 2)) + + + +(define (curry f) + (lambda (x) + (lambda (y) + (f x y)))) + + + +(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 (sieve input) + (stream-cons + (stream-car input) + (sieve (sub-merge (stream-cdr input) + (stream-map ((curry *) (stream-car input)) input))))) + + + +(define euler (sieve base)) + + |