From: Ramakrishnan Muthukrishnan Date: Mon, 7 Jun 2010 11:03:26 +0000 (+0530) Subject: solution and tests for exercise 2.1 X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/biz/flags/%3C?a=commitdiff_plain;h=20aa54a1e51de9f4e3746018e039578b41421fd3;p=sicp.git solution and tests for exercise 2.1 --- diff --git a/src/sicp/ex2_1.clj b/src/sicp/ex2_1.clj new file mode 100644 index 0000000..9020280 --- /dev/null +++ b/src/sicp/ex2_1.clj @@ -0,0 +1,30 @@ +(ns sicp.ex2_1 + (:use [sicp ch2_1 utils] + [clojure.test])) + +;; the gcd version of make-rat does this automatically for us. But assuming +;; we don't use gcd, here is the solution +(defn new-make-rat [x y] + (cond (and (< x 0) (< y 0)) (list (- x) (- y)) + (or (< x 0) (< y 0)) (if (< x 0) (list x y) (list (- x) (- y))) + :else (list x y))) + +(deftest test-num-p-den-p + (are [x y] (= x y) + (numer (new-make-rat 2 3)) 2 + (denom (new-make-rat 2 3)) 3)) + +(deftest test-num-p-den-n + (are [x y] (= x y) + (numer (new-make-rat 2 -3)) -2 + (denom (new-make-rat 2 -3)) 3)) + +(deftest test-num-n-den-n + (are [x y] (= x y) + (numer (new-make-rat -2 -3)) 2 + (denom (new-make-rat -2 -3)) 3)) + +(deftest test-num-n-den-p + (are [x y] (= x y) + (numer (new-make-rat -2 3)) -2 + (denom (new-make-rat -2 3)) 3)) \ No newline at end of file