SICP 2.1
August 1st 2008 09:55 pm
Section 2.1, I have kept most of the exercises I did for this section, but for some i got lazy and did not do. If you wish to compare answer for those exercises, please leave a comment and i would be happy to add them in. I will not add any comments since I did them a while ago, but i will just paste the code:
;; 2.1 (define (gcd x y) ;; we've implemented gcd in section 1.2.5. (if (= y 0) x (gcd y (remainder x y)))) (define (sign n) (if (= n 0) 0 (/ n (abs n)))) (define (make-rat n d) (let ((g (gcd n d)) (sign (sign n))) (cons (/ n (* g sign)) (/ d (* g sign))))) ;; 2.2 (define (make-point x y) (cons x y)) (define (x-point p) (car p)) (define (y-point p) (cdr p)) (define (make-segment p1 p2) (cons p1 p2)) (define (start-segment s) (car s)) (define (end-segment s) (cdr s)) (define (midpoint-segment s) (cons (/ (+ ;; 2.4 (define (cons x y) (lambda (m) (m x y))) (define (car z) (z (lambda (p q) p))) (car (cons 1 2)) (define (cdr z) (z (lambda (p q) q))) (cdr (cons 1 2)) ;; 2.7 (define (make-interval x y) (cons a b)) (define (upper-bound interval) (cdr interval)) (define (lower-bound interval) (car interval))