Solution to Exercise SICP 2.6

Structure and Interpretation of Computer Programs

One solution to Exercise 2.6:

(define zero (lambda (f) (lambda (x) x)))

(define (add-1 n)
(lambda (f) (lambda (x) (f ((n f) x)))))

; (add-1 zero)
; (lambda (f) (lambda (x) (f ((zero f) x))))
; (lambda (f) (lambda (x) (f (((lambda (g) (lambda (y) y)) f) x))))
; (lambda (f) (lambda (x) (f ((lambda (y) y) x))))
; (lambda (f) (lambda (x) (f x)))
(define one
(lambda (f) (lambda (x) (f x))))

; (add-1 one)
;(lambda (f) (lambda (x) (f ((one f) x))))
;(lambda (f) (lambda (x) (f (((lambda (g) (lambda (y) (g (y)))) f) x))))
;(lambda (f) (lambda (x) (f ((lambda (y) (f (y))) x))))
;(lambda (f) (lambda (x) (f (f (x)))))
(define two
(lambda (f) (lambda (x) (f (f x)))))

(define (add a b)
(lambda (f) (lambda (x) ((a f) ((b f) x)))))

; transform Church numerals to integers (for testing)
(define (to-integer n)
(define (inc x) (+ 1 x))
((n inc) 0))

Advertisements

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

End Breast Cancer – Win an iPod nano

Weekend to End Breast CanceriPod nano - pink

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.