learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-2.scm (702B)


      1 ;; Exercise 2.2
      2 
      3 (define (make-point x y)
      4   (cons x y))
      5 
      6 (define (x-point p) (car p))
      7 
      8 (define (y-point p) (cdr p))
      9 
     10 (define (make-segment p-start p-end)
     11   (cons p-start p-end))
     12 
     13 (define (start-segment s) (car s))
     14 
     15 (define (end-segment s) (cdr s))
     16 
     17 (define (midpoint-segment s)
     18   (define start (start-segment s))
     19   (define end (end-segment s))
     20 
     21   (make-point (/ (- (x-point end)
     22                     (x-point start))
     23                  2)
     24               (/ (- (y-point end)
     25                     (y-point start))
     26                  2)))
     27 
     28 (test-begin "2.2")
     29 (test-equal
     30     (make-point 5 2)
     31   (midpoint-segment (make-segment (make-point 0 0)
     32                                   (make-point 10 4)))
     33   )
     34 (test-end)