]> git.rkrishnan.org Git - sicp.git/blob - src/sicp/ex2_1.clj
Solutions to 4.27, 4.28 and 4.29.
[sicp.git] / src / sicp / ex2_1.clj
1 (ns sicp.ex2_1
2   (:use [sicp ch2_1 utils]
3         [clojure.test]))
4
5 ;; the gcd version of make-rat does this automatically for us. But assuming
6 ;; we don't use gcd, here is the solution
7 (defn new-make-rat [x y]
8   (cond (and (< x 0) (< y 0)) (list (- x) (- y))
9         (or (< x 0) (< y 0)) (if (< x 0) (list x y) (list (- x) (- y)))
10         :else (list x y)))
11
12 (deftest test-num-p-den-p
13   (are [x y] (= x y)
14        (numer (new-make-rat 2 3)) 2
15        (denom (new-make-rat 2 3)) 3))
16
17 (deftest test-num-p-den-n
18   (are [x y] (= x y)
19        (numer (new-make-rat 2 -3)) -2
20        (denom (new-make-rat 2 -3)) 3))
21
22 (deftest test-num-n-den-n
23   (are [x y] (= x y)
24        (numer (new-make-rat -2 -3)) 2
25        (denom (new-make-rat -2 -3)) 3))
26
27 (deftest test-num-n-den-p
28   (are [x y] (= x y)
29        (numer (new-make-rat -2 3)) -2
30        (denom (new-make-rat -2 3)) 3))