SICP 1.44 Answer
;; sicp exercise 1.44 ;; smooth will be moved to lib/smooth.scm ;; n-fold-smooth will be moved to lib/n-fold-smooth.scm (load "../lib/repeated.scm") (define (smooth f dx) (lambda (x) (/ (+ (f (- x dx)) (f x) (f (+ x dx))) 3))) (define (n-fold-smooth f n dx) ((repeated (lambda (f) (smooth f dx)) n) f)) ((n-fold-smooth square 5 .1) 2)
SICP 1.45 Answer
I don’t think I did the number of average dampening properly, but I am happy with what the current status.
;; sicp exercise 1.45
;; nth-root will be saved in lib/nth-root.scm
(load "../lib/fixed-point.scm")
(load "../lib/average-damp.scm")
(load "../lib/repeated.scm")
(define (nth-root x n)
(fixed-point ((repeated average-damp 2) (lambda (y) (/ x (expt y (- n 1)))))
2.0))
(nth-root 100 4)
sicp-answers at github
I have created a github repository for my answers. They aren’t all in there yet, and I probably slowly add and update older files.
SICP 1.43 Answer
(define (repeated f num)
(define (repeated-in f num x)
(cond ((= 1 num) (f x))
(else (f (repeated-in f (- num 1) x)))))
(lambda (x) (repeated-in f num x)))
SICP 1.40 Answer
(define (cubic a b c)
(define (cube x) (* x x x))
(lambda (x) (+ (cube x) (* a (square x) ) (* b x) c)))
SICP 1.39 Answer
(define (tan-cf x k)
(cont-frac (lambda (n) (if (= n 1)
x
(* x x -1)))
(lambda (n) (- (* 2.0 n) 1))
k))
SICP 1.38 Answer
(define (approx-e k)
(define (not-eq-1 n)
(= (remainder (+ n 1) 3) 0))
(define (di n)
(define (iter n tot)
(cond ((= n 0) tot)
((not-eq-1 n) (iter (- n 1) (+ tot 2)))
(else (iter (- n 1) tot))))
(if (not (not-eq-1 n))
1
(iter n 0)))
(+ 2
(cont-frac (lambda (x) 1.0)
di
k)))
SICP 1.37 Answer
Part a:
12 gives .6180, which is accurate to 4 decimal places.
(define (cont-frac n d k)
(define (cont-frac-rec at)
(if (= at k)
0
(/ (n at) (+ (d at) (cont-frac-rec (+ 1 at))))))
(cont-frac-rec 1))
Part b:
The recursive process:
(define (cont-frac n d k)
(define (iter at tot)
(if (= at 0)
tot
(iter (- at 1) (/ (n at) (+ (d at) tot)))))
(iter k (/ (n k) (d k))))
Interestingly, a k of 10 makes this accurate to 4 decimal places. I’m not sure why.