
Solution to Exercise 2.5:
(define (cons x y)
(* (expt 2 x)
(expt 3 y)))
(define (count-powers n d)
(define (iter i pow)
(if (zero? (remainder i d))
(iter (/ i d) (+ pow 1))
pow))
(iter n 0))
(define (car c)
(count-powers c 2))
(define (cdr c)
(count-powers c 3))
Posted by Ken Dyck as Programming at 7:39 PM MST
2 Comments »

Solution to Exercise 2.4:
Substituting through…
(car (cons x y))
(car (lambda (m) (m x y)))
((lambda (m) (m x y)) (lambda (p q) p))
((lambda(p q) p) x y)
x
The definition for cdr:
(define (cdr z)
(z (lambda (p q) q)))
Posted by Ken Dyck as Programming at 7:49 AM MST
No Comments »

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 MST
No Comments »

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 MST
3 Comments »

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 MST
No Comments »

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 MST
3 Comments »

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 MST
3 Comments »


Would you like to win an Apple iPod nano? Would you like to put an end to breast cancer? Now you can do both.
As part of my fundraising efforts for the Weekend to End Breast Cancer, I’m raffling off a 4GB Apple iPod nano.
Imagine all the hours of entertainment you could enjoy with a new iPod nano. Or, if you already own an iPod, how happy you could make a friend or family member by giving it as a gift. Or how much more money you could raise for breast cancer research if you donated it back for another raffle.
To enter the raffle, just sponsor my walk. One entry for a $5 donation or five for $20.
You can make your donation here: http://www.endcancer.ca/goto/kendyck
The winner will be drawn on September 10, 2007 or the day I reach my goal of raising $2500, whichever comes first.
If you win, you will be contacted by email so make sure you leave a valid email address when you make your donation.
As winner you will be able choose the colour in which you’d like the iPod (pink shown above) and where you’d like it delivered.
Those of you who have already sponsored my walk are already entered into the draw.
All the proceeds go directly to the Princess Margaret Hospital.
Thank you and good luck.
Posted by Ken Dyck as Charity at 10:33 AM MST
No Comments »

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 MST
2 Comments »

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 MST
1 Comment »