]> git.rkrishnan.org Git - sicp.git/blobdiff - src/sicp/ch2_1.clj
Lazy version of evaluator and tests.
[sicp.git] / src / sicp / ch2_1.clj
index 9063e9846d75aa8e4fed87a65fb90ea910eb16cd..df583a4f150ea8095283d61322cf410ca95180a3 100644 (file)
@@ -2,6 +2,8 @@
   (:use [sicp utils]
        [clojure.contrib test-is]))
 
+
+;; 2.1.1
 (declare make-rat numer denom)
 
 (defn add-rat [x y]
   (first (rest x)))
 
 (defn print-rat [x]
-  (println (numer x)"/"(denom x)))
\ No newline at end of file
+  (println (numer x)"/"(denom x)))
+
+;;; 2.1.3
+;; scheme/CL cons is nothing but a way to represent a pair.
+(defn s-cons [x y]
+  (fn [m] (cond (= m 0) x
+               (= m 1) y
+               :else (str "argument not 0 or 1 - s-cons"))))
+
+(defn car [z] (z 0))
+
+(defn cdr [z] (z 1))
+
+(def x (s-cons 2 3))
+(car x) ;;=> 2
+(cdr x) ;;=> 3
\ No newline at end of file