Skip to main content.
-->
June 4th, 2007

Solution to SICP Exercise 1.42

Structure and Interpretation of Computer Programs

Solution to Exercise 1.42:

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

Posted by Ken Dyck as Programming at 6:23 PM EDT

No Comments »

Solution to SICP Exercise 1.41

Structure and Interpretation of Computer Programs

Solution to Exercise 1.41:

(define (double f)
  (lambda (x) (f (f x))))

Output:

> (define (inc x) (+ x 1))
> (((double (double double)) inc) 5)
21

Posted by Ken Dyck as Programming at 5:31 AM EDT

No Comments »

Solution to SICP Exercise 1.40

Structure and Interpretation of Computer Programs

Solution to Exercise 1.40:

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

Posted by Ken Dyck as Programming at 5:21 AM EDT

No Comments »

June 3rd, 2007

Solution to SICP Exercise 1.39

Structure and Interpretation of Computer Programs

Solution to Exercise 1.39:

(define (cont-frac n d k)
  (define (iter i sub-expr)
    (if (= i 0)
        sub-expr
        (iter (- i 1) (/ (n i) (+ (d i) sub-expr)))))
  (iter (- k 1) (/ (n k) (d k))))

(define (tan-cf x k)
  (let ((x-squared-negated (- (* x x))))
    (cont-frac (lambda (i) (if (= i 1) x x-squared-negated))
               (lambda (i) (- (* 2 i) 1))
               k)))

Posted by Ken Dyck as Programming at 2:54 PM EDT

No Comments »

Solution to SICP Exercise 1.38

Structure and Interpretation of Computer Programs

Solution to Exercise 1.38:

(define (cont-frac n d k)
  (define (iter i sub-expr)
    (if (= i 0)
        sub-expr
        (iter (- i 1) (/ (n i) (+ (d i) sub-expr)))))
  (iter (- k 1) (/ (n k) (d k))))

(define (e k)
  (+ (cont-frac (lambda (i) 1.0)
             (lambda (i) (if (= (remainder (+ i 1) 3) 0)
                             (* 2 (/ (+ i 1) 3))
                             1.0))
             k)
     2))

Posted by Ken Dyck as Programming at 10:50 AM EDT

No Comments »

Solution to SICP Exercise 1.37

Structure and Interpretation of Computer Programs

Solution to Exercise 1.37:

; part a (recursive process)
(define (cont-frac n d k)
  (define (sub-expr i)
    (if (= i k) 
        (/ (n i) (d i))
        (/ (n i) (+ (d i) (sub-expr (+ i 1))))))
  (sub-expr 1))

(define (golden-ratio k) 
  (cont-frac (lambda (i) 1.0)
             (lambda (i) 1.0)
             k))

(define (iterations-required)
  (define (iter i previous)
    (let ((current (golden-ratio i)))
      (if (= (round (* current 10000))
             (round (* previous 10000)))
          (- i 1)
          (iter (+ i 1) current))))
  (iter 1 0.0))

; > (iterations-required)
; 12

; part b (iterative process)
(define (cont-frac-i n d k)
  (define (iter i sub-expr)
    (if (= i 0)
        sub-expr
        (iter (- i 1) (/ (n i) (+ (d i) sub-expr)))))
  (iter (- k 1) (/ (n k) (d k))))

Posted by Ken Dyck as Programming at 10:35 AM EDT

3 Comments »

Solution to SICP Exercise 1.36

Structure and Interpretation of Computer Programs

Solution to Exercise 1.36:

(define tolerance 0.00001)
(define (fixed-point f first-guess)
  (define (close-enough? v1 v2)
    (< (abs (- v1 v2)) tolerance))
  (define (try guess)
    (display guess)
    (newline)
    (let ((next (f guess)))
      (if (close-enough? guess next)
          next
          (try next))))
  (try first-guess))

(define (average a b) (/ (+ a b) 2))

(define result-with-damping
  (fixed-point (lambda (x) (average x (/ (log 1000) (log x))))
               2.0))

(display "result-with-damping: ")
(display result-with-damping)
(newline)
(newline)

(define result-without-damping
  (fixed-point (lambda (x) (/ (log 1000) (log x)))
               2.0))

(display "result-without-damping: ")
(display result-without-damping)

Output:

2.0
5.9828921423310435
4.922168721308343
4.628224318195455
4.568346513136242
4.5577305909237005
4.555909809045131
4.555599411610624
4.5555465521473675
result-with-damping: 4.555537551999825

2.0
9.965784284662087
3.004472209841214
6.279195757507157
3.759850702401539
5.215843784925895
4.182207192401397
4.8277650983445906
4.387593384662677
4.671250085763899
4.481403616895052
4.6053657460929
4.5230849678718865
4.577114682047341
4.541382480151454
4.564903245230833
4.549372679303342
4.559606491913287
4.552853875788271
4.557305529748263
4.554369064436181
4.556305311532999
4.555028263573554
4.555870396702851
4.555315001192079
4.5556812635433275
4.555439715736846
4.555599009998291
4.555493957531389
4.555563237292884
4.555517548417651
4.555547679306398
4.555527808516254
4.555540912917957
result-without-damping: 4.555532270803653

Posted by Ken Dyck as Programming at 7:33 AM EDT

No Comments »

Solution to SICP Exercise 1.35

Structure and Interpretation of Computer Programs

Solution to Exercise 1.35:

(define golden-ratio 
  (fixed-point (lambda (x) (+ 1 (/ 1 x))) 
               1.0))

> golden-ratio
1.6180327868852458

Posted by Ken Dyck as Programming at 7:18 AM EDT

No Comments »

Solution to SICP Exercise 1.34

Structure and Interpretation of Computer Programs

Solution to Exercise 1.34:

DrScheme prints out an error message that reads:

procedure application: expected procedure, given: 2; arguments were: 2.

This is hardly surprising since (f f) evaluates to (f 2), which evaluates to (2 2), which is nonsense. The number 2 is not a procedure.

Posted by Ken Dyck as Programming at 6:55 AM EDT

No Comments »

June 2nd, 2007

Solution to SICP Exercise 1.33

Structure and Interpretation of Computer Programs

Solution to Exercise 1.33:

(define (filtered-accumulate predicate combiner null-value term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (if (predicate a) 
                           (combiner (term a)  result) 
                           result))))
  (iter a null-value))

; part a
(define (square x) (* x x))

(define (sum-of-squared-primes a b)
  (define (square-and-add i result) (+ (square i) result))
  (filtered-accumulate prime? square-and-add 0 identity a inc b))

; part b
(define (product-of-positives-relatively-prime-to n)
  (define (relatively-prime? i)
    (= 1 (gcd i n)))
  (filtered-accumulate relatively-prime? * 1 identity 1 inc (- n 1)))

Posted by Ken Dyck as Programming at 7:06 AM EDT

4 Comments »

« Previous Page« Previous Entries  Next Entries »Next Page »