Problem Set 5
Prepare the following exercises for oral presentation in tutorial:
(define A (cons-stream 1 (scale-stream 2 A))) (define (mul-streams a b) (cons-stream (* (stream-car a) (stream-car b)) (mul-streams (stream-cdr a) (stream-cdr b)))) (define B (cons-stream 1 (mul-streams B integers)))
Louis Reasoner wants to create an abstract constructor adjoin-term similar to that used for polynomials in the Generic Arithmetic System of Problem Set 4. He tries to define the following:
(define adjoin-term cons-stream)
but gets an error he doesn't understand. What is wrong?
He next tries the following:
(define (adjoin-term term series) (cons-stream term series)
Since this does not produce an error, he shows his definition to Alyssa P Hacker, who says, "Louis, you really don't understand the differences between special forms and procedures, do you?" What is she talking about? What would happen if Louis used his constructor in the following manner to define the series whose coefficients are all 1:
(define (one-series k) (adjoin-term (make-term k 1) (ones-series (+ k 1))))
and then attemped to evaluate (stream-car (ones-series 0))?
Given a stream s the following procedure returns the stream of all pairs of elements from s:
(define (stream-pairs s) (if (stream-null? s) the-empty-stream (stream-append (stream-map (lambda (sn) (list (stream-car s) sn)) (stream-cdr s)) (stream-pairs (stream-cdr s)))))
(a) Suppose that integers is the (finite) stream {1, 2, 3, 4, 5}. What is (stream-pairs s)? (b) Give the clearest explanation that you can of how stream-pairs works. (c) Suppose that s is the stream of positive integers. What are the first few elements of (stream-pairs s)? Can you suggest a modification of stream-pairs that would be more appropriate in dealing with infinite streams?
In lecture a few weeks ago, we saw how polynomials could be represented
as lists of terms. In a similar way, we can represent power
series, such as
will be represented as the infinite stream whose elements are .1
We provide two ways to construct a series: coeffs->series and proc->series. The procedure coeffs->series takes a list of initial coefficients and pads it with zeroes to produce a powers series. For example, (coeff->series '(1 3 4)) produces the power series .
(define (coeffs->series list-of-coeffs) (define zeros (cons-stream 0 zeros)) (define (iter lst) (if (null? lst) zeros (cons-stream (car lst) (iter (cdr lst))))) (iter list-of-coeffs))
The other constructor, proc->series, takes as argument a procedure
p of one numeric argument and returns the series
(define (proc->series proc) (stream-map proc non-neg-integers))
With this representation, we can use basic stream operations such as:
(define (add-streams s1 s2) (cond ((stream-null? s1) s2) ((stream-null? s2) s1) (else (cons-stream (+ (stream-car s1) (stream-car s2)) (add-streams (stream-cdr s1) (stream-cdr s2)))))) (define (scale-stream c stream) (stream-map (lambda (x) (* x c)) stream))
to implement series operations, and thus arrive at a complete power series data abstraction:
(define add-series add-streams) (define scale-series scale-stream) (define (negate-series s) (scale-series -1 s)) (define (subtract-series s1 s2) (add-series s1 (negate-series s2))) (define (show-series s nterms) (if (= nterms 0) 'done (begin (write-line (stream-car s)) (show-series (stream-cdr s) (- nterms 1))))) {\small \begin{verbatim} (define (series-coeff s n) (stream-ref s n))
You will find the show-series procedure helpful because it allows you to examine the series that you will generate in this problem set. You can also examine an individual coefficient (of xn) in a series using series-coeff.
Note: Loading the code for this problem set will change Scheme's basic arithmetic operations +, -, *, and / so that they will work with rational numbers. For instance, (/ 3 4) will produce 3/4 rather than .75. You'll find this useful in doing the exercises below.
(define (mul-series s1 s2) (cons-stream < ??E1?? > (add-series < ??E2?? > < ??E3?? > )))To test your procedure, demonstrate the the product of S1 (from exercise 1) and S1 is S2. What is the coefficient of x10 in the product of S2 and S2? Turn in your definition of mul-series. (Optional: Give a general formula for the coefficient of xn in the product of S2 and S2.)
Let S be a power series whose constant term is 1. We'll call such a power series a ``unit power series.'' Suppose we want to find the inverse of S, 1/S, which is the power series X such that . Write S=1+SR where SR is the rest of S after the constant term. Then we can solve for X as follows:
In other words, X is the power series whose constant term is 1 and whose rest is given by the negative of SR times X.
(define exp-series (cons-stream 1 (integrate-series-tail exp-series)))
Explain the reasoning behind this definition. Show how to generate the series for sine and cosine, in a similar way, as a pair of mutually recursive definitions.
(define (integrate-series series constant-term) (cons-stream constant-term (integrate-series-tail series)))
He would prefer to define the exponential series as
(define exp-series (integrate-series exp-series 1))
Write a two or three sentence clear explanation of why this won't work, while the definition in exercise 6 does work.
Turn in answers to the following questions along with your answers to the questions in the problem set:
This document was generated using the LaTeX2HTML translator Version 98.1p1 release (March 2nd, 1998)
Copyright © 1993, 1994, 1995, 1996, 1997, Nikos Drakos, Computer Based Learning Unit, University of Leeds.
The command line arguments were:
latex2html ps5-series.
The translation was initiated by Tony Eng on 1998-10-16