Learn faster and stay on-track by joining this free class with other self-learners.
Register for Structure and Interpretation of Computer Programs now.
|
Structure and Interpretation of Computer ProgramsClass length: 13 weeks. Start anytime. Creator: kday Status: Established |
Join this class! |
|
Lesson 13: Assignment 1Do every third problem in section 3.5 of SICP, starting with 3.50. Total of 10 problems. Homework Submissions1 total3.50To test this exercise though I needed a version of cons-stream. The SICP language for Racket that I've been using has one, but since I switched to using the racket language I needed to use the definition below. 3.53s is the stream of powers of 2 or the stream where each value is double the previous value. 3.68Louis’ version of pairs causes an infinite recursive loop because the second argument to the initial call to interleave is the result of a recursive call to pairs, which will never return. Despite the fact that the streams are built with a regular value and a promise – interleave still needs the first value from both streams. The recursive call to pairs needs to return a value for the initial call to evaluate, but to obtain that first value it must recursively call itself again. ;; Exercise 3.50
;;
(define-syntax cons-stream
(syntax-rules ()
((_ A B) (cons A (delay B)))))
(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))))))
;; Exercise 3.56
;;
(define hammings
(cons-stream
1
(merge (scale-stream hammings 2)
(merge (scale-stream hammings 3)
(scale-stream hammings 5)))))
;; Exercise 3.59a
;;
(define (integrate-series coeffs)
(stream-map / coeffs integers))
;; Exercise 3.59b
;;
(define cosine-series
(cons-stream 1 (integrate-series sine-series)))
(define sine-series
(cons-stream 0 (scale-stream (integrate-series cosine-series) -1)))
;; Exercise 3.62
;;
(define (div-series num denom)
(let ((denom-const (stream-car denom)))
(if (zero? denom-const)
(error ("DIV-SERIES -- denominator constant term must be non-zero" ))
(mul-series num
(scale-stream ; restore the scaling factor
(invert-unit-series ; requires a stream that has a unit constant term
(scale-stream denom (/ 1 denom-const)))
denom-const)))))
(define tan-series (div-series sine-series
cosine-series))
;; Exercise 3.62
;;
(define (ln-summands n)
(cons-stream (/ 1.0 n)
(stream-map - (ln-summands (+ n 1)))))
(define ln-stream
(partial-sum (ln-summands 1)))
(stream-ref ln-stream 5)
0.6166666666666666
(stream-ref (euler-transform ln-stream) 5)
0.6928571428571428
(stream-ref (accelerated-sequence euler-transform ln-stream) 5)
0.6931471806635636
;; Exercise 3.71
;;
(define (ramujan-numbers)
(define (sum-cubed x)
(let ((i (car x)) (j (cadr x)))
(+ (* i i i) (* j j j))))
(define (ramujans all-sum-cubes)
(let* ((current (stream-car all-sum-cubes))
(next (stream-car (stream-cdr all-sum-cubes)))
(ramujan-candidate (sum-cubed current)))
(cond ((= ramujan-candidate
(sum-cubed next))
(cons-stream (list ramujan-candidate current next)
(ramujans (stream-cdr (stream-cdr all-sum-cubes)))))
(else (ramujans (stream-cdr all-sum-cubes))))))
(ramujans (weighted-pairs integers
integers
sum-cubed)))
;; Exercise 3.74
;;
(define (sign-change-detector current previous)
(cond ((and (>= current 0)
(< previous 0)) 1)
((and (< current 0)
(>= previous 0)) -1)
(else 0)))
(define zero-crossings
(stream-map sign-change-detector
sense-data
(cons-stream 0 sense-data)))
;; Exercise 3.77
;;
(define (solve f y0 dt)
(define y (integral (delay dy) y0 dt))
(define dy (stream-map f y))
y)
(define (integral delayed-integrand initial-value dt)
(cons-stream initial-value
(if (stream-null? delayed-integrand)
the-empty-stream
(let ((integrand (force delayed-integrand)))
(integral (delay (stream-cdr integrand))
(+ (* dt (stream-car integrand))
initial-value)
dt)))))
;; Exercise 3.80
;;
(define (solve-2nd f dt y0 dy0)
(define y (integral (delay dy) y0 dt))
(define dy (integral (delay ddy) dy0 dt))
(define ddy (stream-map f dy y))
y)
|
Comments:
1 year ago
Well that's it; I've finished this course, but I'm only half way through the book. For anyone still reading or interested in comparing notes I'm writing a blog to post my results as I work my way through the book.
Sign up or log in to comment