#lang planet neil/sicp (#%require (only racket random)) (define (stream-car s) (car s)) (define (stream-cdr s) (force (cdr s))) (define (stream-ref s n) (if (= n 0) (stream-car 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] [(pred? (stream-car s)) (cons-stream (stream-car s) (stream-filter pred? (stream-cdr s)))] [else (stream-filter pred? (stream-cdr s))])) (define (stream-for-each proc s) (if (stream-null? s) 'done (begin (proc (stream-car s)) (stream-for-each proc (stream-cdr s))))) (define (display-stream s) (stream-for-each display-line s)) (define (display-line x) (newline) (display x)) ;; stream-enumerate-interval (define (stream-enumerate-interval low high) (if (> low high) the-empty-stream (cons-stream low (stream-enumerate-interval (+ low 1) high)))) ;; prime? (define (square x) (* x x)) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n test-divisor) (cond ((> (square test-divisor) n) n) ((divides? test-divisor n) test-divisor) (else (find-divisor n (+ test-divisor 1))))) (define (divides? a b) (= (remainder b a) 0)) (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)