solution to 2.2 with tests
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Mon, 7 Jun 2010 12:08:35 +0000 (17:38 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Mon, 7 Jun 2010 12:08:35 +0000 (17:38 +0530)
src/sicp/ex2_2.clj [new file with mode: 0644]

diff --git a/src/sicp/ex2_2.clj b/src/sicp/ex2_2.clj
new file mode 100644 (file)
index 0000000..37d61c8
--- /dev/null
@@ -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