learning-sicp

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

exercise-5.scm (874B)


      1 ;; Exercise 2.5
      2 
      3 (define (power a b)
      4   (define (power' a b result)
      5     (cond
      6      ((= b 0) result)
      7      (else (power' a (1- b) (* a result)))))
      8   (power' a b 1))
      9 
     10 (test-begin "power")
     11 (test-equal 8 (power 2 3))
     12 (test-end "power")
     13 
     14 (define (exercise-2.5)
     15   (define (cons a b)
     16     (* (power 2 a)
     17        (power 3 b)))
     18 
     19   (define (car z)
     20     (define (car' z result)
     21       (cond
     22        ((odd? z) result)
     23        (else (car' (/ z 2)
     24                    (1+ result)))))
     25 
     26     (car' z 0))
     27 
     28   (define (cdr z)
     29     (define (cdr' z result)
     30       (cond
     31        ((not (= (remainder z 3) 0)) result)
     32        (else (cdr' (/ z 3)
     33                    (1+ result)))))
     34 
     35     (cdr' z 0))
     36 
     37   (test-begin "2.5")
     38   (do-ec (: a 1 10)
     39          (: b 1 10)
     40          (test-equal
     41              (list a b)
     42            (list (car (cons a b))
     43                  (cdr (cons a b)))))
     44   (test-end "2.5"))
     45 
     46 (exercise-2.5)