From: Ramakrishnan Muthukrishnan Date: Mon, 2 Aug 2010 13:51:45 +0000 (+0530) Subject: added parts of section 2.2.3 X-Git-Url: https://git.rkrishnan.org/Site/Content/Exhibitors/%22news.html/%5B%5E?a=commitdiff_plain;h=8bf66a6f688a8af8f7c38156c50e7116bd9286e8;p=sicp.git added parts of section 2.2.3 --- diff --git a/src/sicp/ch1_2.clj b/src/sicp/ch1_2.clj index 1021953..bcf0dba 100644 --- a/src/sicp/ch1_2.clj +++ b/src/sicp/ch1_2.clj @@ -1,5 +1,5 @@ (ns sicp.ch1-2 - (:use [sicp utils] + (:use [sicp.utils :only (square)] [clojure.contrib.math :only (sqrt expt)] [clojure.contrib.trace :only (dotrace)])) @@ -166,19 +166,20 @@ (mygcd b (rem a b)))) ;;; section 1.2.6 Primality testing. -(defn prime? [n] - (= (smallest-divisor n) n)) - -(defn smallest-divisor [n] - (find-divisor n 2)) +(defn divides? [a b] + (= (rem b a) 0)) (defn find-divisor [n test-divisor] (cond (> (square test-divisor) n) n (divides? test-divisor n) test-divisor :else (find-divisor n (inc test-divisor)))) -(defn divides? [a b] - (= (rem b a) 0)) +(defn smallest-divisor [n] + (find-divisor n 2)) + +(defn prime? [n] + (= (smallest-divisor n) n)) + ;; fermat's little theorem (defn expmod [base exp m] @@ -188,12 +189,12 @@ :else (rem (* base (expmod base (dec exp) m)) m))) -(defn fermat-test [n] - (try-it (+ 1 (rand-int (- n 1))) n)) - (defn try-it [a n] (= a (expmod a n n))) +(defn fermat-test [n] + (try-it (+ 1 (rand-int (- n 1))) n)) + (defn fast-prime? [n times] (cond (= times 0) true (fermat-test n) (fast-prime? n (dec times)) diff --git a/src/sicp/ch2_2.clj b/src/sicp/ch2_2.clj index e0107f5..21d9f33 100644 --- a/src/sicp/ch2_2.clj +++ b/src/sicp/ch2_2.clj @@ -1,5 +1,6 @@ -(ns sicp.ch2_2 - (:refer-clojure :exclude (map))) +(ns sicp.ch2-2 + (:refer-clojure :exclude (map)) + (:use (sicp [ch1-2 :only (fib)]))) (cons 1 (cons 2 @@ -104,4 +105,54 @@ (if (seq? sub-tree) (scale-tree-with-map sub-tree factor) (* sub-tree factor))) - tree)) \ No newline at end of file + tree)) + +;;; 2.2.3 +(defn sum-odd-squares [tree] + (cond (nil? tree) 0 + (not (seq? tree)) (if (odd? tree) + ((fn [x] (* x x)) tree) + 0) + :else (+ (sum-odd-squares (first tree)) + (sum-odd-squares (next tree))))) + +(defn even-fibs [n] + (letfn [(next-fib [k] + (if (> k n) + nil + (let [f (fib k)] + (if (even? f) + (cons f (next-fib (+ k 1))) + (next-fib (+ k 1))))))] + (next-fib 0))) + +(map #(* % %) (list 1 2 3 4 5)) + +(defn myfilter-1 [pred? xs] + (cond (nil? xs) nil + (not (seq? xs)) (if (pred? xs) + (list xs) + ()) + :else (concat (myfilter-1 pred? (first xs)) + (myfilter-1 pred? (next xs))))) + +(defn myfilter-2 [pred? xs] + (cond (nil? xs) nil + (pred? (first xs)) (cons (first xs) + (myfilter-2 pred? (next xs))) + :else (myfilter-1 pred? (next xs)))) + +;; accumulate +(defn accumulate [op init xs] + (if (nil? xs) + init + (op (first xs) + (accumulate op init (next xs))))) + +(defn enumerate-interval + ([high] + (enumerate-interval 0 high)) + ([low high] + (if (> low high) + nil + (cons low (enumerate-interval (+ low 1) high))))) \ No newline at end of file