learning-sicp

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

exercise-1.scm (935B)


      1 ;; Exercise 2.1
      2 
      3 (define (make-rat n d)
      4   (define g (gcd n d))
      5   (define sign (if (or (and (< n 0)
      6                             (> d 0))
      7                        (and (> n 0)
      8                             (< d 0)))
      9                    -1
     10                    1))
     11   (cons (* sign
     12            (abs (/ n g)))
     13         (abs (/ d g))))
     14 
     15 (test-begin "2.1")
     16 (let ()
     17   (define (make-test)
     18     (define n-sign (1- (* 2
     19                           (random 2))))
     20     (define d-sign (1- (* 2
     21                           (random 2))))
     22     (define n (* n-sign
     23                  (random 100)))
     24     (define d (* d-sign
     25                  (1+ (random 100))))
     26 
     27     (define built-in-rational (/ n d))
     28     (define ratsui (cons (numerator built-in-rational)
     29                          (denominator built-in-rational)))
     30     (define matsui (make-rat n d))
     31     (test-equal (list n d ratsui) (list n d matsui)))
     32 
     33   (for-each (lambda (_) (make-test)) (iota 100)))
     34 (test-end "2.1")