#lang racket (require "stream-base.rkt") ; stream examples ; some operations on streams ; add two streams together, pairwise (define (add-streams s1 s2) (s-map + s1 s2)) ; apply an operation (op v e) to each element e of the stream (define (op-on-stream op v s) (s-map (lambda (e) (op v e)) s) ) ; infinite streams ; a function that recursively generates an stream of integers starting ; at n (define (integers-starting-from n) (s-cons n (integers-starting-from (+ n 1)))) ; the natural numbers (define naturals (integers-starting-from 0)) ; true if a divides b evenly (define (divides? a b) (= (remainder b a) 0)) ; true if x is evenly divisible by y (define (divisible? x y) (= (remainder x y) 0)) ; the stream consisting of any natural number not divisible by 7 (define no-sevens (s-filter (lambda (x) (not (divisible? x 7))) naturals)) ; generate the fibonacci stream with initial terms a, b ; note the recursive generator function (define (fibgen a b) (s-cons a (fibgen b (+ a b)))) ; the usual fibonacci stream (define fibs (fibgen 0 1))