From 20aa54a1e51de9f4e3746018e039578b41421fd3 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Mon, 7 Jun 2010 16:33:26 +0530 Subject: [PATCH] solution and tests for exercise 2.1 --- src/sicp/ex2_1.clj | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/sicp/ex2_1.clj 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 -- 2.45.2