Remember Taylor Series from Calculus? One important application of Taylor
Series is that we can use them to calculate approximations of transcendental
numbers like and e. For instance,
can be defined to be the sum of the infinite series:
To approximate , we simply chop off the tail of
the series at some point and add up all the fractions.
In the problems below, you will build a Scheme representation of
as a stream of rational numbers using only the rational and
stream-processing functions defined below, together with cons-stream, heads, and
tails. In particular, none of the code you write should include lambda or letrec.
Unfortunately, whoever wrote this code had no idea how to choose identifiers so
he or she made your job really hard.
(define (nats-from <function>) (lambda ((n <integer>)) (cons-stream n (nats-from (+ n 1))))) (define (f <function>) (lambda ((s <stream>)) (cons-stream (heads s) (f (tails (tails s)))))) (define (g <function>) (lambda ((s1 <stream>) (s2 <stream>)) (cons-stream (heads s1) (g s2 (tails s1))))) (define (h <function>) (lambda ((f <function>) (s1 <stream>) (s2 <stream>)) (cons-stream (f (heads s1) (heads s2)) (h f (tails s1) (tails s2))))) (define (k <function>) (lambda ((f <function>) (s <stream>)) (cons-stream (f (heads s)) (k f (tails s)))))
Recall that <rational> is a Scheme primitive class with accessors numerator and denominator.
Fortunately, we're going to walk you through it...
(define fours
(define odd-naturals
(define denoms
(define pi