#lang planet neil/sicp
+(#%require (only racket random))
+
(define (stream-car s) (car s))
(define (stream-cdr s) (force (cdr s)))
(stream-ref (stream-cdr s)
(- n 1))))
+(define (stream-map proc . argstreams)
+ (if (stream-null? (car argstreams))
+ the-empty-stream
+ (cons-stream
+ (apply proc (map stream-car argstreams))
+ (apply stream-map
+ (cons proc (map stream-cdr argstreams))))))
+
+(define (scale-stream stream factor)
+ (stream-map (lambda (x) (* x factor)) stream))
+
+#|
(define (stream-map proc s)
(if (stream-null? s)
the-empty-stream
(cons-stream (proc (stream-car s))
(stream-map proc (stream-cdr s)))))
+|#
(define (stream-filter pred? s)
(cond [(stream-null? s) the-empty-stream]
(define (prime? n)
(= (smallest-divisor n) n))
+
+;; infinite streams
+(define (integers-starting-from n)
+ (cons-stream n
+ (integers-starting-from (+ n 1))))
+
+(define integers (integers-starting-from 1))
+
+(define (add-streams s1 s2)
+ (stream-map + s1 s2))
+
+;; integers which are not a multiple of 7
+(define (divisible? a b) (= 0 (remainder a b)))
+
+(define no-sevens
+ (stream-filter (lambda (x) (not (divisible? x 7)))
+ integers))
+
+;; fibonaci
+(define (fib-gen a b)
+ (cons-stream a (fib-gen b (+ a b))))
+
+(define fibs (fib-gen 0 1))
+
+;; sieve
+(define (sieve stream)
+ (cons-stream
+ (stream-car stream)
+ (sieve (stream-filter (lambda (x)
+ (not (divisible? x (stream-car stream))))
+ (stream-cdr stream)))))
+
+(define primes (sieve (integers-starting-from 2)))
+
+(define (interleave s1 s2)
+ (if (stream-null? s1)
+ s2
+ (cons-stream (stream-car s1)
+ (interleave s2 (stream-cdr s1)))))
+
+(define (pairs s t)
+ (cons-stream
+ (list (stream-car s) (stream-car t))
+ (interleave
+ (stream-map (lambda (x) (list (stream-car s) x))
+ (stream-cdr t))
+ (pairs (stream-cdr s) (stream-cdr t)))))
+
+(define (integral integrand initial-value dt)
+ (define int
+ (cons-stream initial-value
+ (add-streams (scale-stream integrand dt)
+ int)))
+ int)