From: Ramakrishnan Muthukrishnan Date: Mon, 7 Jun 2010 12:08:35 +0000 (+0530) Subject: solution to 2.2 with tests X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/bcase/lohnkonto.html?a=commitdiff_plain;h=ec6c3e5d61f64e13ceaf6ad3c505fe97df193df3;p=sicp.git solution to 2.2 with tests --- diff --git a/src/sicp/ex2_2.clj b/src/sicp/ex2_2.clj new file mode 100644 index 0000000..37d61c8 --- /dev/null +++ b/src/sicp/ex2_2.clj @@ -0,0 +1,37 @@ +(ns sicp.ex2_2 + (:use [sicp utils] + [clojure.test])) + +(defn make-segment [x y] + (list x y)) + +(defn start-segment [seg] + (first seg)) + +(defn end-segment [seg] + (first (rest seg))) + +(defn make-point [x y] + (list x y)) + +(defn x-point [p] + (first p)) + +(defn y-point [p] + (first (rest p))) + +(defn midpoint-segment [seg] + (let [x1 (x-point (start-segment seg)) + y1 (y-point (start-segment seg)) + x2 (x-point (end-segment seg)) + y2 (y-point (end-segment seg))] + (make-point (average x1 x2) + (average y1 y2)))) + + +(deftest test-mid-point-1-1-3-3 + (let [m (midpoint-segment (make-segment (make-point 1 1) + (make-point 3 3)))] + (are [x y] [approx-equal x y] + (x-point m) 2.0 + (y-point m) 2.0))) \ No newline at end of file