about summary refs log tree commit diff stats
path: root/js/games/nluqo.github.io/~bh/61a-pages/Lectures/3.5/prime2.scm
blob: 4df4e920abe9c05bf69da713709d27feede1aff9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(define (stream-prime? n)
  (stream-null? (stream-filter (lambda (x) (= (remainder n x) 0))
			       (stream-range 2 (- n 1)))))

(define (stream-range from to)
  (if (> from to)
      the-empty-stream
      (cons-stream from (stream-range (+ from 1) to))))

(define (stream-filter predicate data)
  (cond ((stream-null? data) the-empty-stream)
        ((predicate (stream-car data))
         (cons-stream (stream-car data)
		      (stream-filter predicate (stream-cdr data))))
        (else (stream-filter predicate (stream-cdr data)))))

;; try (define s (stream-filter stream-prime? (stream-range 2 100)))
;; s
;; (display-stream s)