notyouravgjoel

Just another WordPress.com weblog

SICP 1.44 Answer

leave a comment »

;; 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)

Written by JoelMcCracken

May 21, 2008 at 9:50 pm

Posted in Uncategorized

SICP 1.45 Answer

leave a comment »

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)

Written by JoelMcCracken

May 21, 2008 at 9:48 pm

Posted in Uncategorized

sicp-answers at github

leave a comment »

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.

Written by JoelMcCracken

May 21, 2008 at 1:25 pm

Posted in Uncategorized

SICP 1.43 Answer

leave a comment »

(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)))

Written by JoelMcCracken

May 20, 2008 at 7:50 pm

Posted in Uncategorized

SICP 1.42 Answer

leave a comment »

(define (compose f g)
  (lambda (x) (f (g x))))

Written by JoelMcCracken

May 20, 2008 at 5:33 pm

Posted in Uncategorized

SICP 1.41 Answer

leave a comment »

(define (double g)
  (lambda (x) (g (g x))))

And, 22 is returned.

Written by JoelMcCracken

May 20, 2008 at 5:30 pm

Posted in Uncategorized

SICP 1.40 Answer

leave a comment »

(define (cubic a b c)
  (define (cube x) (* x x x))
  (lambda (x) (+ (cube x) (* a (square x) ) (* b x) c)))

Written by JoelMcCracken

May 20, 2008 at 5:19 pm

Posted in Uncategorized

SICP 1.39 Answer

leave a comment »

(define (tan-cf x k)
  (cont-frac (lambda (n) (if (= n 1)
                              x
                              (* x x -1)))
             (lambda (n) (- (* 2.0 n) 1))
             k))

Written by JoelMcCracken

May 20, 2008 at 4:40 pm

Posted in Uncategorized

SICP 1.38 Answer

leave a comment »

(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)))

Written by JoelMcCracken

May 20, 2008 at 4:06 pm

Posted in Uncategorized

SICP 1.37 Answer

leave a comment »

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.

Written by JoelMcCracken

May 20, 2008 at 3:49 pm

Posted in Uncategorized