Skip to main content.
-->
June 23rd, 2007

Solution to SICP Exercise 2.3

Structure and Interpretation of Computer Programs

Solution to Exercise 2.3:

; rectangle procedures
(define (perimeter-rect r)
  (+ (* 2 (width-rect r))
     (* 2 (height-rect r))))

(define (area-rect r)
  (* (width-rect r) (height-rect r)))

; first representation (as a diagonal line segment)
(define (make-rect x1 y1 x2 y2)
  (make-segment (make-point x1 y1)
                (make-point x2 y2)))

(define (width-rect r)
  (abs (- (x-point (start-segment r)) 
          (x-point (end-segment r)))))

(define (height-rect r)
  (abs (- (y-point (start-segment r)) 
          (y-point (end-segment r)))))

; second representation (as corner point and dimensions)
(define (make-rect x1 y1 x2 y2)
  (cons (make-point x1 y1) ; corner point
        (cons (- x2 x1)    ; relative width
              (- y2 y1)))) ; relative height

(define (width-rect r)
  (abs (car (cdr r))))

(define (height-rect r)
  (abs (cdr (cdr r))))

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

No Comments »

Solution to SICP Exercise 2.3

Structure and Interpretation of Computer Programs

Solution to Exercise 2.3:

; rectangle procedures
(define (perimeter-rect r)
  (+ (* 2 (width-rect r))
     (* 2 (height-rect r))))

(define (area-rect r)
  (* (width-rect r) (height-rect r)))

; first representation (as a diagonal line segment)
(define (make-rect x1 y1 x2 y2)
  (make-segment (make-point x1 y1)
                (make-point x2 y2)))

(define (width-rect r)
  (abs (- (x-point (start-segment r)) 
          (x-point (end-segment r)))))

(define (height-rect r)
  (abs (- (y-point (start-segment r)) 
          (y-point (end-segment r)))))

; second representation (as corner point and dimensions)
(define (make-rect x1 y1 x2 y2)
  (cons (make-point x1 y1) ; corner point
        (cons (- x2 x1)    ; relative width
              (- y2 y1)))) ; relative height

(define (width-rect r)
  (abs (car (cdr r))))

(define (height-rect r)
  (abs (cdr (cdr r))))

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

4 Comments »

June 22nd, 2007

Solution to SICP Exercise 2.2

Structure and Interpretation of Computer Programs

Solution to Exercise 2.2:

(define (make-segment start end)
  (cons start end))

(define (start-segment s)
  (car s))

(define (end-segment s)
  (cdr s))

(define (make-point x y)
  (cons x y))

(define (x-point p)
  (car p))

(define (y-point p)
  (cdr p))

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

(define (midpoint-segment s)
  (let ((start (start-segment s))
        (end (end-segment s)))
    (make-point (avg (x-point start) (x-point end))
                (avg (y-point start) (y-point end)))))

Posted by Ken Dyck as Programming at 8:09 PM EDT

No Comments »

Solution to SICP Exercise 2.1

Structure and Interpretation of Computer Programs

Solution to Exercise 2.1:

(define (make-rat n d)
    (let ((new-n (if (negative? d) (- n) n))
          (new-d (abs d)))
      (let ((g (gcd new-n new-d)))
        (cons (/ new-n g) (/ new-d g)))))

Posted by Ken Dyck as Programming at 7:46 PM EDT

3 Comments »

Solution to SICP Exercise 1.46

Structure and Interpretation of Computer Programs

Solution to Exercise 1.46:

(define (iterative-improve good-enough? improve)
  (define (iter initial-guess)
    (if (good-enough? initial-guess)
        initial-guess
        (iter (improve initial-guess))))
  iter)

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

3 Comments »

June 4th, 2007

Solution to SICP Exercise 1.44

Structure and Interpretation of Computer Programs

Solution to Exercise 1.44:

(define (average-3 a b c)
  (/ (+ a b c) 3))

(define (smooth f dx)
  (lambda (x) (average-3 (f (- x dx)) 
                         (f x) 
                         (f (+ x dx)))))

(define (n-fold-smooth f dx n)
  (repeated (lambda (g) (smooth g dx)) n) f)

Posted by Ken Dyck as Programming at 7:17 PM EDT

3 Comments »

Solution to SICP Exercise 1.43

Structure and Interpretation of Computer Programs

Solution to Exercise 1.43:

(define (compose f g)
  (lambda (x) (f (g x))))
(define (inc i) (+ i 1))

(define (repeated f n)
  (define (iter i result)
    (if (= i n)
        result
        (iter (inc i) (compose f result))))
  (iter 1 f))

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

1 Comment »

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 »

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