Problem 6: Primes (optional, 0pts)
We can even represent the sequence of all prime numbers as an infinite stream! Define a function sieve
, which takes in a stream of increasing numbers and returns a stream containing only those numbers which are not multiples of an earlier number in the stream. We can define primes
by sifting all natural numbers starting at 2. Look online for the Sieve of Eratosthenes if you need some inspiration.
Hint: You might find using filter-stream
as defined earlier helpful.
(define (sieve s)
'YOUR-CODE-HERE
)
(define primes (sieve (naturals 2)))
;;; Tests
; scm> (slice primes 0 10)
; (2 3 5 7 11 13 17 19 23 29)