diff --git a/.cljstyle b/.cljstyle new file mode 100644 index 0000000..6bc374b --- /dev/null +++ b/.cljstyle @@ -0,0 +1,99 @@ +{:files {:extensions #{"cljc" "cljs" "clj" "cljx"}, + :ignore #{".hg" ".git"}}, + :rules {:namespaces {:enabled? true, + :indent-size 2, + :break-libs? true, + :import-break-width 100}, + :whitespace {:enabled? true, + :remove-surrounding? true, + :remove-trailing? true, + :insert-missing? true}, + :comments {:enabled? true, + :inline-prefix " ", + :leading-prefix " "}, + :functions {:enabled? true}, + :eof-newline {:enabled? true, + :trailing-blanks? false}, + :types {:enabled? true, + :types? true, + :protocols? true, + :reifies? true, + :proxies? true}, + :blank-lines {:enabled? true, + :trim-consecutive? true, + :max-consecutive 1, + :insert-padding? true, + :padding-lines 1}, + :indentation {:enabled? true, + :list-indent 2, + :indents {are [[:block 2]], + when-first [[:block 1]], + cond->> [[:block 1]], + while [[:block 1]], + #"^def" [[:inner 0]], + try [[:block 0]], + bound-fn [[:inner 0]], + thrown-with-msg? [[:block 2]], + match [[:block 1]], + testing [[:block 1]], + if-not [[:block 1]], + doseq [[:block 1]], + finally [[:block 0]], + deftype [[:block 1] [:inner 1]], + when-let [[:block 1]], + go [[:block 0]], + if-some [[:block 1]], + with-precision [[:block 1]], + let [[:block 1]], + defstruct [[:block 1]], + doto [[:block 1]], + future [[:block 0]], + fn [[:inner 0]], + alt! [[:block 0]], + as-> [[:block 1]], + do [[:block 0]], + when-not [[:block 1]], + when [[:block 1]], + extend [[:block 1]], + go-loop [[:block 1]], + defn [[:inner 0]], + if [[:block 1]], + ns [[:block 1]], + deftest [[:inner 0]], + extend-type [[:block 1] [:inner 1]], + defmethod [[:inner 0]], + struct-map [[:block 1]], + extend-protocol [[:block 1] [:inner 1]], + cond-> [[:block 1]], + dotimes [[:block 1]], + reify [[:inner 0] [:inner 1]], + with-open [[:block 1]], + defonce [[:inner 0]], + defn- [[:inner 0]], + alt!! [[:block 0]], + defprotocol [[:block 1] [:inner 1]], + letfn [[:block 1] [:inner 2 0]], + use-fixtures [[:inner 0]], + loop [[:block 1]], + with-out-str [[:block 0]], + condp [[:block 2]], + cond [[:block 0]], + for [[:block 1]], + binding [[:block 1]], + with-local-vars [[:block 1]], + defmacro [[:inner 0]], + proxy [[:block 2] [:inner 1]], + with-redefs [[:block 1]], + locking [[:block 1]], + defmulti [[:inner 0]], + if-let [[:block 1]], + case [[:block 1]], + catch [[:block 2]], + thread [[:block 0]], + comment [[:block 0]], + #"^with-" [[:inner 0]], + defrecord [[:block 1] [:inner 1]], + thrown? [[:block 1]], + when-some [[:block 1]], + def [[:inner 0]]}}, + :vars {:enabled? true}}} diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bf715d0..c482801 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -77,9 +77,13 @@ jobs: cli: 'latest' bb: 'latest' cljfmt: 'latest' + cljstyle: 'latest' - name: 👍 cljfmt - run: bb lint:check + run: bb style:cljfmt + + - name: 👍 cljstyle + run: bb style:cljstyle experimental: diff --git a/bb.edn b/bb.edn index 509f98b..fa29eb7 100644 --- a/bb.edn +++ b/bb.edn @@ -1,11 +1,16 @@ -{:tasks {test (shell "clojure -X:test") - lint (do (run 'lint:check) - (run 'lint:eastwood) - (run 'lint:kondo)) - lint:fix (shell "cljfmt fix") - lint:check (shell "cljfmt check") - lint:eastwood (shell "clojure -M:test:eastwood") - lint:kondo (shell "clj-kondo --lint test src") - pre-push (do (run 'lint:fix) - (run 'test) - (run 'lint))}} +{:tasks {test (shell "clojure -X:test") + lint (do (run 'style:cljfmt) + (run 'style:cljstyle) + (run 'lint:eastwood) + (run 'lint:kondo)) + style:fix (do (run 'style:fix:cljfmt) + (run 'style:fix:cljstyle)) + style:cljfmt (shell "cljfmt check") + style:cljstyle (shell "cljstyle check") + style:fix:cljfmt (shell "cljfmt fix") + style:fix:cljstyle (shell "cljstyle fix") + lint:eastwood (shell "clojure -M:test:eastwood") + lint:kondo (shell "clj-kondo --lint test src") + pre-push (do (run 'style:fix) + (run 'test) + (run 'lint))}} diff --git a/src/sicp/chapter_1/part_1/book_1_1.clj b/src/sicp/chapter_1/part_1/book_1_1.clj index e22bfd7..8286632 100644 --- a/src/sicp/chapter_1/part_1/book_1_1.clj +++ b/src/sicp/chapter_1/part_1/book_1_1.clj @@ -1,7 +1,6 @@ (ns sicp.chapter-1.part-1.book-1-1) -(comment "1.1.1") -; Expressions -------------------------------------------------------------------------------------- +(comment "1.1.1 Expressions ----------------------------------------------------------------------") (+ 137 349) ; 486 (- 1000 334) ; 666 @@ -19,8 +18,7 @@ (+ (- 10 7) 6)) ; 57 -(comment "1.1.2") -; Naming and the Environment ----------------------------------------------------------------------- +(comment "1.1.2 Naming and the Environment -------------------------------------------------------") (def size 2) (* 5 size) ; 10 @@ -30,33 +28,33 @@ (* pi (* radius radius)) ; 314.159 (def circumference (* 2 pi radius)) ; 62.8318 -(comment "1.1.3") -; Evaluating Combinations -------------------------------------------------------------------------- +(comment "1.1.3 Evaluating Combinations ----------------------------------------------------------") (* (+ 2 (* 4 6)) (+ 3 5 7)) ; 390 -(comment "1.1.4") -; Compound Procedures ------------------------------------------------------------------------------ +(comment "1.1.4 Compound Procedures --------------------------------------------------------------") -(defn square [x] +(defn square + [x] (* x x)) (square 21) ; 441 (square (+ 2 5)) ; 49 (square (square 3)) ; 81 -(defn sum-of-squares [x y] +(defn sum-of-squares + [x y] (+ (square x) (square y))) (sum-of-squares 3 4) ; 25 -(defn f [a] +(defn f + [a] (sum-of-squares (+ a 1) (* a 2))) (f 5) ; 136 -(comment "1.1.5") -; The Substitution Model for Procedure Application ------------------------------------------------- +(comment "1.1.5 The Substitution Model for Procedure Application ---------------------------------") (f 5) ; 136 (sum-of-squares (+ 5 1) (* 5 2)) ; 136 @@ -70,8 +68,8 @@ (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2))) ; 136 -(comment "1.1.6") -; Conditional Expressions and Predicates ----------------------------------------------------------- +(comment "1.1.6 Conditional Expressions and Predicates -------------------------------------------") + ; Exercises: ; * 1.1 ; * 1.2 @@ -86,36 +84,43 @@ (= x 0) 0 (< x 0) (- x))) -(defn abs-2 [x] +(defn abs-2 + [x] (cond (< x 0) (- x) :else x)) -(defn abs-3 [x] +(defn abs-3 + [x] (if (< x 0) (- x) x)) ; Renamed due to Clojure conflit names -(defn >=-1 [x y] +(defn >=-1 + [x y] (or (> x y) (= x y))) -(defn >=-2 [x y] +(defn >=-2 + [x y] (not (< x y))) -(comment "1.1.7") -; Example: Square Roots by Newton’s Method --------------------------------------------------------- +(comment "1.1.7 Example: Square Roots by Newton’s Method -----------------------------------------") + ; Exercises: ; * 1.6 ; * 1.7 ; * 1.8 -(defn average [x y] +(defn average + [x y] (/ (+ x y) 2)) -(defn improve [guess x] +(defn improve + [guess x] (average guess (/ x guess))) -(defn good-enough? [guess x] +(defn good-enough? + [guess x] (< (abs (- (square guess) x)) 0.001)) (defn sqrt-iter @@ -124,19 +129,22 @@ guess (sqrt-iter (improve guess x) x))) -(defn sqrt [x] +(defn sqrt + [x] (sqrt-iter 1.0 x)) -(comment "1.1.8") -; Procedures as Black-Box Abstractions ------------------------------------------------------------- +(comment "1.1.8 Procedures as Black-Box Abstractions ---------------------------------------------") -(defn square-alt [x] +(defn square-alt + [x] (Math/exp (double (Math/log x)))) -(defn sqrt-alt [x] +(defn sqrt-alt + [x] (letfn [(good-enough? [guess x] (< (abs (- (* guess guess) x)) 0.001)) (improve [guess x] (/ (+ guess (/ x guess)) 2)) - (sqrt-iter [guess x] + (sqrt-iter + [guess x] (if (good-enough? guess x) guess (sqrt-iter (improve guess x) x)))] diff --git a/src/sicp/chapter_1/part_1/ex_1_05.clj b/src/sicp/chapter_1/part_1/ex_1_05.clj index c07167a..fd6f9e1 100644 --- a/src/sicp/chapter_1/part_1/ex_1_05.clj +++ b/src/sicp/chapter_1/part_1/ex_1_05.clj @@ -4,7 +4,10 @@ ; Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is ; using applicative-order evaluation or normal-order evaluation. He defines the following two procedures: -(defn p [] p) +(defn p + [] + p) + (defn test_1_5 [x y] (if (= x 0) 0 y)) diff --git a/src/sicp/chapter_1/part_1/ex_1_06.clj b/src/sicp/chapter_1/part_1/ex_1_06.clj index 90a991c..c2628ce 100644 --- a/src/sicp/chapter_1/part_1/ex_1_06.clj +++ b/src/sicp/chapter_1/part_1/ex_1_06.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-1.ex-1-06 - (:require [sicp.chapter-1.part-1.book-1-1 :as b11])) + (:require + [sicp.chapter-1.part-1.book-1-1 :as b11])) ; Exercise 1.6 ; Alyssa P. Hacker doesn’t see why if needs to be provided as a special form. diff --git a/src/sicp/chapter_1/part_1/ex_1_07.clj b/src/sicp/chapter_1/part_1/ex_1_07.clj index 54b58fc..e1a7487 100644 --- a/src/sicp/chapter_1/part_1/ex_1_07.clj +++ b/src/sicp/chapter_1/part_1/ex_1_07.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-1.ex-1-07 - (:require [sicp.chapter-1.part-1.book-1-1 :as b11])) + (:require + [sicp.chapter-1.part-1.book-1-1 :as b11])) ; Exercise 1.7 ; The `good-enough?` test used in computing square roots will not be very effective @@ -16,10 +17,12 @@ ; Does this work better for small and large numbers? ; Imporovments -(defn good-enough-v2? [guess1 guess2] +(defn good-enough-v2? + [guess1 guess2] (< (abs (- guess1 guess2)) 0.001)) -(defn sqrt-iter-v2 [guess x] +(defn sqrt-iter-v2 + [guess x] (if (good-enough-v2? guess (b11/improve guess x)) guess (sqrt-iter-v2 (b11/improve guess x) x))) diff --git a/src/sicp/chapter_1/part_1/ex_1_08.clj b/src/sicp/chapter_1/part_1/ex_1_08.clj index 147ae6f..2684aff 100644 --- a/src/sicp/chapter_1/part_1/ex_1_08.clj +++ b/src/sicp/chapter_1/part_1/ex_1_08.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-1.ex-1-08 - (:require [sicp.chapter-1.part-1.book-1-1 :as b11])) + (:require + [sicp.chapter-1.part-1.book-1-1 :as b11])) ; Exercise 1.8 ; Newton’s method for cube roots is based on the fact that if y diff --git a/src/sicp/chapter_1/part_2/book_1_2.clj b/src/sicp/chapter_1/part_2/book_1_2.clj index 723f834..57e4e89 100644 --- a/src/sicp/chapter_1/part_2/book_1_2.clj +++ b/src/sicp/chapter_1/part_2/book_1_2.clj @@ -1,115 +1,132 @@ (ns sicp.chapter-1.part-2.book-1-2 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) + +(comment "1.2.1 Linear Recursion and Iteration ---------------------------------------------------") -(comment "1.2.1") -; Linear Recursion and Iteration ------------------------------------------------------------------- ; Exercises: ; * 1.9 ; * 1.10 -(defn factorial [n] +(defn factorial + [n] (if (= n 1) 1 (* n (factorial (- n 1))))) -(defn fact-iter [product counter max-count] +(defn fact-iter + [product counter max-count] (if (> counter max-count) product (fact-iter (* counter product) (+ counter 1) max-count))) -(defn factorial-iter [n] +(defn factorial-iter + [n] (fact-iter 1 1 n)) -(comment "1.2.2") -; Tree Recursion ----------------------------------------------------------------------------------- +(comment "1.2.2 Tree Recursion -------------------------------------------------------------------") + ; Exercises: ; * 1.11 ; * 1.12 ; * 1.13 -(defn fib [n] +(defn fib + [n] (cond (= n 0) 0 (= n 1) 1 :else (+ (fib (- n 1)) (fib (- n 2))))) -(defn fib-iter [a b count] +(defn fib-iter + [a b count] (if (= count 0) b (fib-iter (+ a b) a (- count 1)))) -(defn fib-2 [n] +(defn fib-2 + [n] (fib-iter 1 0 n)) -(defn first-denomination [kinds-of-coins] +(defn first-denomination + [kinds-of-coins] (cond (= kinds-of-coins 1) 1 (= kinds-of-coins 2) 5 (= kinds-of-coins 3) 10 (= kinds-of-coins 4) 25 (= kinds-of-coins 5) 50)) -(defn cc [amount kinds-of-coins] +(defn cc + [amount kinds-of-coins] (cond (= amount 0) 1 (or (< amount 0) (= kinds-of-coins 0)) 0 :else (+ (cc amount (- kinds-of-coins 1)) (cc (- amount (first-denomination kinds-of-coins)) kinds-of-coins)))) -(defn count-change [amount] +(defn count-change + [amount] (cc amount 5)) -(comment "1.2.3") -; Orders of Growth --------------------------------------------------------------------------------- +(comment "1.2.3 Orders of Growth -----------------------------------------------------------------") + +; ; There are no code examples for this part in the book :( ; Exercises: ; * 1.14 ; * 1.15 -(comment "1.2.4") -; Exponentiation ----------------------------------------------------------------------------------- +(comment "1.2.4 Exponentiation -------------------------------------------------------------------") + ; Exercises: ; * 1.16 ; * 1.17 ; * 1.18 ; * 1.19 -(defn expt [b n] +(defn expt + [b n] (if (= n 0) 1 (* b (expt b (- n 1))))) -(defn expt-iter [b counter product] +(defn expt-iter + [b counter product] (if (= counter 0) product (expt-iter b (- counter 1) (* b product)))) -(defn expt-2 [b n] +(defn expt-2 + [b n] (expt-iter b n 1)) -(defn fast-expt [b n] +(defn fast-expt + [b n] (cond (= n 0) 1 (even? n) (m/square (fast-expt b (/ n 2))) :else (* b (fast-expt b (- n 1))))) -(defn even?-alt [n] +(defn even?-alt + [n] (= (rem n 2) 0)) -(comment "1.2.5") -; Greatest Common Divisors ------------------------------------------------------------------------- +(comment "1.2.5 Greatest Common Divisors ---------------------------------------------------------") + ; Exercises: ; * 1.20 -(defn gcd [a b] +(defn gcd + [a b] (if (= b 0) a (gcd b (rem a b)))) -(comment "1.2.6") -; Example: Testing for Primality ------------------------------------------------------------------- +(comment "1.2.6 Example: Testing for Primality ---------------------------------------------------") + ; Exercises: ; * 1.21 ; * 1.22 @@ -120,28 +137,34 @@ ; * 1.27 ; * 1.28 -(defn find-divisor [n test-divisor] +(defn find-divisor + [n test-divisor] (cond (> (m/square test-divisor) n) n (m/divides? test-divisor n) test-divisor :else (find-divisor n (+ test-divisor 1)))) -(defn smallest-divisor [n] +(defn smallest-divisor + [n] (find-divisor n 2)) -(defn prime? [n] +(defn prime? + [n] (= n (smallest-divisor n))) -(defn expmod [base exp m] +(defn expmod + [base exp m] (cond (= exp 0) 1 (even? exp) (rem (m/square (expmod base (/ exp 2) m)) m) :else (rem (* base (expmod base (- exp 1) m)) m))) -(defn fermat-test [n] +(defn fermat-test + [n] (let [try-it (fn [a] (= (expmod a n n) a)) a (rand-int (dec n))] (try-it (inc a)))) -(defn fast-prime? [n times] +(defn fast-prime? + [n times] (cond (= times 0) true (fermat-test n) (fast-prime? n (- times 1)) :else false)) diff --git a/src/sicp/chapter_1/part_2/ex_1_10.clj b/src/sicp/chapter_1/part_2/ex_1_10.clj index 287c113..dbde64e 100644 --- a/src/sicp/chapter_1/part_2/ex_1_10.clj +++ b/src/sicp/chapter_1/part_2/ex_1_10.clj @@ -16,16 +16,20 @@ (A 3 3) ; Consider the following procedures, where A is the procedure defined above: -(defn f [n] +(defn f + [n] (A 0 n)) -(defn g [n] +(defn g + [n] (A 1 n)) -(defn h [n] +(defn h + [n] (A 2 n)) -(defn k [n] +(defn k + [n] (* 5 n n)) ; Give concise mathematical definitions for the functions computed diff --git a/src/sicp/chapter_1/part_2/ex_1_14.clj b/src/sicp/chapter_1/part_2/ex_1_14.clj index e46bbc4..ea44706 100644 --- a/src/sicp/chapter_1/part_2/ex_1_14.clj +++ b/src/sicp/chapter_1/part_2/ex_1_14.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-2.ex-1-14 - (:require [sicp.chapter-1.part-2.book-1-2 :as b12])) + (:require + [sicp.chapter-1.part-2.book-1-2 :as b12])) ; Exercise 1.14 ; Draw the tree illustrating the process generated by the count-change procedure of 1.2.2 in making change for 11 cents. @@ -12,5 +13,6 @@ :else (+ (change amount (- type-of-coins 1)) (change (- amount (b12/first-denomination type-of-coins)) type-of-coins)))) -(defn money-change [amount] +(defn money-change + [amount] (change amount 5)) diff --git a/src/sicp/chapter_1/part_2/ex_1_15.clj b/src/sicp/chapter_1/part_2/ex_1_15.clj index ba4b929..16677cb 100644 --- a/src/sicp/chapter_1/part_2/ex_1_15.clj +++ b/src/sicp/chapter_1/part_2/ex_1_15.clj @@ -8,10 +8,12 @@ ; These ideas are incorporated in the following procedures: -(defn cube [x] +(defn cube + [x] (* x x x)) -(defn p [x] +(defn p + [x] (- (* 3 x) (* 4 (cube x)))) (defn sine diff --git a/src/sicp/chapter_1/part_2/ex_1_16.clj b/src/sicp/chapter_1/part_2/ex_1_16.clj index 8be598a..d3ce7f7 100644 --- a/src/sicp/chapter_1/part_2/ex_1_16.clj +++ b/src/sicp/chapter_1/part_2/ex_1_16.clj @@ -22,5 +22,6 @@ (even? n) (fast-expt (* b b) (/ n 2) product) :else (fast-expt b (- n 1) (* product b)))) -(defn expt [b n] +(defn expt + [b n] (fast-expt b n 1)) diff --git a/src/sicp/chapter_1/part_2/ex_1_18.clj b/src/sicp/chapter_1/part_2/ex_1_18.clj index a285b73..6d491c3 100644 --- a/src/sicp/chapter_1/part_2/ex_1_18.clj +++ b/src/sicp/chapter_1/part_2/ex_1_18.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-2.ex-1-18 - (:require [sicp.chapter-1.part-2.ex-1-16 :refer [expt]])) + (:require + [sicp.chapter-1.part-2.ex-1-16 :refer [expt]])) ; Exercise 1.18 ; Using the results of Exercise 1.16 and Exercise 1.17, devise a procedure that generates an iterative @@ -17,5 +18,6 @@ product (+ counter 1)))) -(defn mult [a b] +(defn mult + [a b] (fast-mult-v2 a b 0 0)) diff --git a/src/sicp/chapter_1/part_2/ex_1_19.clj b/src/sicp/chapter_1/part_2/ex_1_19.clj index 85a7c63..dddcdb1 100644 --- a/src/sicp/chapter_1/part_2/ex_1_19.clj +++ b/src/sicp/chapter_1/part_2/ex_1_19.clj @@ -33,11 +33,13 @@ q (- count 1)))) -(defn fib [n] +(defn fib + [n] (fib-iter 1 0 0 1 n)) ; Solution -(defn fib-iter-v2 [a b p q count] +(defn fib-iter-v2 + [a b p q count] (cond (= count 0) b (even? count) (fib-iter-v2 a b @@ -50,5 +52,6 @@ q (- count 1)))) -(defn fib-v2 [n] +(defn fib-v2 + [n] (fib-iter-v2 1 0 0 1 n)) diff --git a/src/sicp/chapter_1/part_2/ex_1_20.clj b/src/sicp/chapter_1/part_2/ex_1_20.clj index 510bdf3..10495dc 100644 --- a/src/sicp/chapter_1/part_2/ex_1_20.clj +++ b/src/sicp/chapter_1/part_2/ex_1_20.clj @@ -11,7 +11,8 @@ ; How many remainder operations are actually performed in the normal-order evaluation of (gcd 206 40)? ; In the applicative-order evaluation? -(defn gcd [a b] +(defn gcd + [a b] (if (= b 0) a (gcd b (mod a b)))) diff --git a/src/sicp/chapter_1/part_2/ex_1_21.clj b/src/sicp/chapter_1/part_2/ex_1_21.clj index f0892ea..43c60ae 100644 --- a/src/sicp/chapter_1/part_2/ex_1_21.clj +++ b/src/sicp/chapter_1/part_2/ex_1_21.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-2.ex-1-21 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 1.21 ; Use the smallest-divisor procedure to find the smallest divisor of each of the following numbers: @@ -11,5 +12,6 @@ (m/divides? test-divisor number) test-divisor :else (find-divisor number (+ test-divisor 1)))) -(defn smallest-divisor [n] +(defn smallest-divisor + [n] (find-divisor n 2)) diff --git a/src/sicp/chapter_1/part_2/ex_1_22.clj b/src/sicp/chapter_1/part_2/ex_1_22.clj index a7ab7e1..652508b 100644 --- a/src/sicp/chapter_1/part_2/ex_1_22.clj +++ b/src/sicp/chapter_1/part_2/ex_1_22.clj @@ -6,16 +6,16 @@ ; The following timed-prime-test procedure, when called with an integer n, prints n and checks to see if n is prime. ; If n is prime, the procedure prints three asterisks followed by the amount of time used in performing the test. -;(define (timed-prime-test n) +; (define (timed-prime-test n) ; (newline) ; (display n) ; (start-prime-test n (runtime))) ; -;(define (report-prime elapsed-time) +; (define (report-prime elapsed-time) ; (display " *** ") ; (display elapsed-time)) ; -;(define (start-prime-test n start-time) +; (define (start-prime-test n start-time) ; (if (prime? n) ; (report-prime (- (runtime) start-time)))) diff --git a/src/sicp/chapter_1/part_2/ex_1_23.clj b/src/sicp/chapter_1/part_2/ex_1_23.clj index 83aa06f..8afecfa 100644 --- a/src/sicp/chapter_1/part_2/ex_1_23.clj +++ b/src/sicp/chapter_1/part_2/ex_1_23.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-2.ex-1-23 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 1.23 ; The smallest-divisor procedure shown at the start of this section does lots of needless testing: @@ -21,7 +22,8 @@ ; Is this expectation confirmed? If not, what is the observed ratio of the speeds of the two algorithms, ; and how do you explain the fact that it is different from 2? -(defn next-odd [x] +(defn next-odd + [x] (if (= x 2) 3 (+ x 2))) (defn find-divisor @@ -30,8 +32,10 @@ (m/divides? test-divisor num) test-divisor :else (find-divisor num (next-odd test-divisor)))) -(defn smallest-divisor [num] +(defn smallest-divisor + [num] (find-divisor num 2)) -(defn prime? [n] +(defn prime? + [n] (= n (smallest-divisor n))) diff --git a/src/sicp/chapter_1/part_2/ex_1_24.clj b/src/sicp/chapter_1/part_2/ex_1_24.clj index 80d3fbd..6f98163 100644 --- a/src/sicp/chapter_1/part_2/ex_1_24.clj +++ b/src/sicp/chapter_1/part_2/ex_1_24.clj @@ -12,10 +12,12 @@ ; TODO: Improve benchmarking ; TODO: Improve add transparent solution -(defn expmod [base exp m] +(defn expmod + [base exp m] (mod (Math/pow base exp) m)) -(defn fermat-test [n] +(defn fermat-test + [n] (let [try-it (fn [a] (= (expmod a n n) a)) a (rand-int (dec n))] (try-it (inc a)))) diff --git a/src/sicp/chapter_1/part_2/ex_1_25.clj b/src/sicp/chapter_1/part_2/ex_1_25.clj index 143593b..e126bf1 100644 --- a/src/sicp/chapter_1/part_2/ex_1_25.clj +++ b/src/sicp/chapter_1/part_2/ex_1_25.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-2.ex-1-25 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 1.25 ; Alyssa P. Hacker complains that we went to a lot of extra work in writing expmod diff --git a/src/sicp/chapter_1/part_2/ex_1_26.clj b/src/sicp/chapter_1/part_2/ex_1_26.clj index c62ba81..ea85c96 100644 --- a/src/sicp/chapter_1/part_2/ex_1_26.clj +++ b/src/sicp/chapter_1/part_2/ex_1_26.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-2.ex-1-26 - (:require [sicp.misc :refer [square]])) + (:require + [sicp.misc :refer [square]])) ; Exercise 1.26 ; Louis Reasoner is having great difficulty doing Exercise 1.24. diff --git a/src/sicp/chapter_1/part_2/ex_1_27.clj b/src/sicp/chapter_1/part_2/ex_1_27.clj index 1af27dc..2897e6d 100644 --- a/src/sicp/chapter_1/part_2/ex_1_27.clj +++ b/src/sicp/chapter_1/part_2/ex_1_27.clj @@ -1,12 +1,14 @@ (ns sicp.chapter-1.part-2.ex-1-27 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 1.27 ; Demonstrate that the Carmichael numbers listed in Footnote 47 really do fool the Fermat test. ; That is, write a procedure that takes an integer n and tests whether an is congruent to a modulo n ; for every a a b) result (iter (next-fn a) (+ (term a) result))))] diff --git a/src/sicp/chapter_1/part_3/ex_1_31.clj b/src/sicp/chapter_1/part_3/ex_1_31.clj index da49eef..511af0f 100644 --- a/src/sicp/chapter_1/part_3/ex_1_31.clj +++ b/src/sicp/chapter_1/part_3/ex_1_31.clj @@ -39,7 +39,8 @@ ; 2. (defn product-iter [term a next-fn b] - (letfn [(iter [a result] + (letfn [(iter + [a result] (if (> a b) result (iter (next-fn a) (* (term a) result))))] diff --git a/src/sicp/chapter_1/part_3/ex_1_32.clj b/src/sicp/chapter_1/part_3/ex_1_32.clj index 7222fa5..02d9941 100644 --- a/src/sicp/chapter_1/part_3/ex_1_32.clj +++ b/src/sicp/chapter_1/part_3/ex_1_32.clj @@ -27,7 +27,8 @@ ; 2. --------- (defn accumulate-iter [combiner null-value term a next-fn b] - (letfn [(iter [a result] + (letfn [(iter + [a result] (if (> a b) result (iter (next-fn a) (combiner (term a) result))))] diff --git a/src/sicp/chapter_1/part_3/ex_1_33.clj b/src/sicp/chapter_1/part_3/ex_1_33.clj index bc43043..4a0b067 100644 --- a/src/sicp/chapter_1/part_3/ex_1_33.clj +++ b/src/sicp/chapter_1/part_3/ex_1_33.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-3.ex-1-33 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 1.33 ; @@ -29,7 +30,8 @@ (defn filtered-accumulate-iter [combiner null-value term a next-fn b filter?] - (letfn [(iter [a result] + (letfn [(iter + [a result] (if (> a b) result (iter (next-fn a) diff --git a/src/sicp/chapter_1/part_3/ex_1_34.clj b/src/sicp/chapter_1/part_3/ex_1_34.clj index ad502cc..0cfe07b 100644 --- a/src/sicp/chapter_1/part_3/ex_1_34.clj +++ b/src/sicp/chapter_1/part_3/ex_1_34.clj @@ -11,7 +11,8 @@ ; What happens if we (perversely) ask the interpreter to evaluate the combination (f f)? ; Explain. -(defn f [g] +(defn f + [g] (g 2)) ; (f f) diff --git a/src/sicp/chapter_1/part_3/ex_1_35.clj b/src/sicp/chapter_1/part_3/ex_1_35.clj index da98c42..9e08dde 100644 --- a/src/sicp/chapter_1/part_3/ex_1_35.clj +++ b/src/sicp/chapter_1/part_3/ex_1_35.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-3.ex-1-35 - (:require [sicp.chapter-1.part-3.book-1-3 :as b13])) + (:require + [sicp.chapter-1.part-3.book-1-3 :as b13])) ; Exercise 1.35 ; Show that the golden ratio φ (1.2.2) is a fixed point of the transformation x↦1+1/x, diff --git a/src/sicp/chapter_1/part_3/ex_1_36.clj b/src/sicp/chapter_1/part_3/ex_1_36.clj index b6063a9..b713789 100644 --- a/src/sicp/chapter_1/part_3/ex_1_36.clj +++ b/src/sicp/chapter_1/part_3/ex_1_36.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-3.ex-1-36 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 1.36 ; Modify fixed-point so that it prints the sequence of approximations it generates, @@ -12,7 +13,8 @@ (defn fixed-point-print [f first-guess tolerance print?] - (letfn [(try-fn [guess] + (letfn [(try-fn + [guess] (let [next (f guess)] (if (m/close-enough? guess next tolerance) diff --git a/src/sicp/chapter_1/part_3/ex_1_37.clj b/src/sicp/chapter_1/part_3/ex_1_37.clj index 2879d05..7153470 100644 --- a/src/sicp/chapter_1/part_3/ex_1_37.clj +++ b/src/sicp/chapter_1/part_3/ex_1_37.clj @@ -29,7 +29,8 @@ ; 1. Recursive process ------------------- (defn cont-frac [func-n func-d k] - (letfn [(recursive [i] + (letfn [(recursive + [i] (let [n (func-n i) d (func-d i)] (if (= i k) @@ -40,7 +41,8 @@ ; 2. Iterative process ------------------- (defn cont-frac-iter [func-n func-d k] - (letfn [(iterator [i result] + (letfn [(iterator + [i result] (let [n (func-n i) d (func-d i)] (if (= i 1) diff --git a/src/sicp/chapter_1/part_3/ex_1_38.clj b/src/sicp/chapter_1/part_3/ex_1_38.clj index 17f0437..8e7ee04 100644 --- a/src/sicp/chapter_1/part_3/ex_1_38.clj +++ b/src/sicp/chapter_1/part_3/ex_1_38.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-3.ex-1-38 - (:require [sicp.chapter-1.part-3.ex-1-37 :as ex-1-37])) + (:require + [sicp.chapter-1.part-3.ex-1-37 :as ex-1-37])) ; Exercise 1.38 ; In 1737, the Swiss mathematician Leonhard Euler published a memoir De Fractionibus Continuis, @@ -14,12 +15,14 @@ ; i = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ; SpikeN = 0 1 2 3 4 5 6 7 8 9 -(defn euler-fraction [k] +(defn euler-fraction + [k] (ex-1-37/cont-frac-iter (fn [_] 1.0) - (fn [i] (if (= i 1) - 1 - (if (> (rem (- i 2) 3) 0) - 1 ; Each second and third values - (* 2 (+ (/ (- i 2) 3) 1))))) ; 2 * ( ( (i-2)/3) + 1) + (fn [i] + (if (= i 1) + 1 + (if (> (rem (- i 2) 3) 0) + 1 ; Each second and third values + (* 2 (+ (/ (- i 2) 3) 1))))) ; 2 * ( ( (i-2)/3) + 1) k)) diff --git a/src/sicp/chapter_1/part_3/ex_1_39.clj b/src/sicp/chapter_1/part_3/ex_1_39.clj index eb352f5..c26f758 100644 --- a/src/sicp/chapter_1/part_3/ex_1_39.clj +++ b/src/sicp/chapter_1/part_3/ex_1_39.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-3.ex-1-39 - (:require [sicp.chapter-1.part-3.ex-1-37 :as ex-1-37])) + (:require + [sicp.chapter-1.part-3.ex-1-37 :as ex-1-37])) ; Exercise 1.39 ; A continued fraction representation of the tangent function was published in 1770 @@ -8,10 +9,12 @@ ; the tangent function based on Lambert’s formula. ; k specifies the number of terms to compute, as in Exercise 1.37. -(defn tan-cf [x k] +(defn tan-cf + [x k] (ex-1-37/cont-frac-iter - (fn [i] (if (= i 1) - x - (- (Math/pow x 2)))) + (fn [i] + (if (= i 1) + x + (- (Math/pow x 2)))) (fn [i] (- (* 2.0 i) 1)) k)) diff --git a/src/sicp/chapter_1/part_3/ex_1_40.clj b/src/sicp/chapter_1/part_3/ex_1_40.clj index 7f3bd29..986142c 100644 --- a/src/sicp/chapter_1/part_3/ex_1_40.clj +++ b/src/sicp/chapter_1/part_3/ex_1_40.clj @@ -9,7 +9,8 @@ (defn cubic [a b c] - (fn [x] (+ (* x x x) - (* a x x) - (* b x) - c))) + (fn [x] + (+ (* x x x) + (* a x x) + (* b x) + c))) diff --git a/src/sicp/chapter_1/part_3/ex_1_41.clj b/src/sicp/chapter_1/part_3/ex_1_41.clj index 8886505..95aff0d 100644 --- a/src/sicp/chapter_1/part_3/ex_1_41.clj +++ b/src/sicp/chapter_1/part_3/ex_1_41.clj @@ -9,5 +9,6 @@ ; ; (((double (double double)) inc) 5) -(defn double-fn [f] +(defn double-fn + [f] (fn [x] (f (f x)))) diff --git a/src/sicp/chapter_1/part_3/ex_1_42.clj b/src/sicp/chapter_1/part_3/ex_1_42.clj index a89f19e..a533ab6 100644 --- a/src/sicp/chapter_1/part_3/ex_1_42.clj +++ b/src/sicp/chapter_1/part_3/ex_1_42.clj @@ -8,5 +8,6 @@ ; ((compose square inc) 6) ; 49 -(defn compose [f g] +(defn compose + [f g] (fn [x] (f (g x)))) diff --git a/src/sicp/chapter_1/part_3/ex_1_43.clj b/src/sicp/chapter_1/part_3/ex_1_43.clj index 8415f75..4be7028 100644 --- a/src/sicp/chapter_1/part_3/ex_1_43.clj +++ b/src/sicp/chapter_1/part_3/ex_1_43.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-3.ex-1-43 - (:require [sicp.chapter-1.part-3.ex-1-42 :as ex-1-42])) + (:require + [sicp.chapter-1.part-3.ex-1-42 :as ex-1-42])) ; Exercise 1.43 ; @@ -22,7 +23,8 @@ (defn repeated [func times] (fn [x] - (letfn [(iter [i result] + (letfn [(iter + [i result] (if (= times i) result (iter (inc i) (func result))))] diff --git a/src/sicp/chapter_1/part_3/ex_1_44.clj b/src/sicp/chapter_1/part_3/ex_1_44.clj index 8bc910f..3119403 100644 --- a/src/sicp/chapter_1/part_3/ex_1_44.clj +++ b/src/sicp/chapter_1/part_3/ex_1_44.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-3.ex-1-44 - (:require [sicp.chapter-1.part-3.ex-1-43 :as ex-1-43])) + (:require + [sicp.chapter-1.part-3.ex-1-43 :as ex-1-43])) ; Exercise 1.44 ; The idea of smoothing a function is an important concept in signal processing. @@ -15,12 +16,15 @@ ; Show how to generate the n-fold smoothed function of any given function ; using smooth and repeated from Exercise 1.43. -(defn smooth [func] +(defn smooth + [func] (let [dx 0.001] - (fn [x] (/ (+ (func (- x dx)) - (func x) - (func (+ x dx))) - 3)))) + (fn [x] + (/ (+ (func (- x dx)) + (func x) + (func (+ x dx))) + 3)))) -(defn n-fold [func times] +(defn n-fold + [func times] ((ex-1-43/repeated smooth times) func)) diff --git a/src/sicp/chapter_1/part_3/ex_1_45.clj b/src/sicp/chapter_1/part_3/ex_1_45.clj index c8ef9ba..ba6264b 100644 --- a/src/sicp/chapter_1/part_3/ex_1_45.clj +++ b/src/sicp/chapter_1/part_3/ex_1_45.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-3.ex-1-45 - (:require [sicp.chapter-1.part-3.book-1-3 :as b13] - [sicp.chapter-1.part-3.ex-1-43 :as ex-1-43])) + (:require + [sicp.chapter-1.part-3.book-1-3 :as b13] + [sicp.chapter-1.part-3.ex-1-43 :as ex-1-43])) ; Exercise 1.45 ; @@ -22,7 +23,8 @@ ; Assume that any arithmetic operations you need are available as primitives. -(defn nth-root [x n damps] +(defn nth-root + [x n damps] (b13/fixed-point ((ex-1-43/repeated b13/average-damp damps) (fn [y] (/ x (Math/pow y (- n 1))))) @@ -30,8 +32,10 @@ ; The number of repetitions of the average moisture content ; can be calculated using the semi- and logarithmic basis 2 -(defn damp-number [n] +(defn damp-number + [n] (int (Math/floor (/ (Math/log n) (Math/log 2))))) -(defn nth-root-auto-damps [x n] +(defn nth-root-auto-damps + [x n] (nth-root x n (damp-number n))) diff --git a/src/sicp/chapter_1/part_3/ex_1_46.clj b/src/sicp/chapter_1/part_3/ex_1_46.clj index fcb80d6..a356c62 100644 --- a/src/sicp/chapter_1/part_3/ex_1_46.clj +++ b/src/sicp/chapter_1/part_3/ex_1_46.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-1.part-3.ex-1-46 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 1.46 @@ -25,12 +26,13 @@ guess ((iterative-improve enough? func-improver) (func-improver guess))))) -(defn sqrt-improved [x first-guess] +(defn sqrt-improved + [x first-guess] (let [is-enough? (fn [guess] (< (abs (- (* guess guess) x)) 0.001)) improver (fn [guess] (m/average guess (/ x guess)))] ((iterative-improve is-enough? improver) first-guess))) -;(defn fixed-point-iter +; (defn fixed-point-iter ; [func-improver guess] ; (letfn [(close-enough? [guess] ; (< (abs (- guess (func-improver guess))) 0.001)) diff --git a/src/sicp/chapter_2/part_1/book_2_1.clj b/src/sicp/chapter_2/part_1/book_2_1.clj index ce53c3a..eb1c0c2 100644 --- a/src/sicp/chapter_2/part_1/book_2_1.clj +++ b/src/sicp/chapter_2/part_1/book_2_1.clj @@ -1,94 +1,114 @@ (ns sicp.chapter-2.part-1.book-2-1 - (:require [sicp.chapter-2.part-1.ex-2-07 :as ex-2-07] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-1.ex-2-07 :as ex-2-07] + [sicp.misc :as m])) -(comment "2") -; Building Abstractions with Data ------------------------------------------------------------------ +(comment "2 Building Abstractions with Data ------------------------------------------------------") -(defn linear-combination [a b x y] +(defn linear-combination + [a b x y] (+ (* a x) (* b y))) ; (defn linear-combination [a b x y] ; (add (mul a x) (mul b y))) -(comment "2.1.1") -; Example: Arithmetic Operations for Rational Numbers ---------------------------------------------- +(comment "2.1.1 Example: Arithmetic Operations for Rational Numbers ------------------------------") + ; Exercises: ; * 2.1 ; (defn make-rat [n d] (m/pair n d)) -(defn make-rat [numer denom] +(defn make-rat + [numer denom] (let [g (m/gcd numer denom)] (m/pair (/ numer g) (/ denom g)))) -(defn numer [ratio] +(defn numer + [ratio] (m/car ratio)) -(defn denom [ratio] +(defn denom + [ratio] (m/cdr ratio)) -(defn add-rat [x y] +(defn add-rat + [x y] (make-rat (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) -(defn sub-rat [x y] +(defn sub-rat + [x y] (make-rat (- (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y)))) -(defn mul-rat [x y] +(defn mul-rat + [x y] (make-rat (* (numer x) (numer y)) (* (denom x) (denom y)))) -(defn div-rat [x y] +(defn div-rat + [x y] (make-rat (* (numer x) (denom y)) (* (denom x) (numer y)))) -(defn equal-rat? [x y] +(defn equal-rat? + [x y] (= (* (numer x) (denom y)) (* (numer y) (denom x)))) -(defn print-rat [ratio] +(defn print-rat + [ratio] (str (numer ratio) "/" (denom ratio))) -(comment "2.1.2") -; Abstraction Barriers ----------------------------------------------------------------------------- +(comment "2.1.2 Abstraction Barriers -------------------------------------------------------------") + ; Exercises: ; * 2.2 ; * 2.3 -(defn make-rat-alt [numer denom] +(defn make-rat-alt + [numer denom] (m/pair numer denom)) -(defn numer-alt [ratio] +(defn numer-alt + [ratio] (let [g (m/gcd (m/car ratio) (m/cdr ratio))] (/ (m/car ratio) g))) -(defn denom-alt [ratio] +(defn denom-alt + [ratio] (let [g (m/gcd (m/car ratio) (m/cdr ratio))] (/ (m/cdr ratio) g))) -(comment "2.1.3") -; What Is Meant by Data? --------------------------------------------------------------------------- +(comment "2.1.3 What Is Meant by Data? -----------------------------------------------------------") + ; Exercises: ; * 2.4 ; * 2.5 ; * 2.6 -(defn pair-alt [x y] - (let [dispatch (fn [m] (cond - (= m 0) x - (= m 1) y - :else (throw (Exception. "Argument not 0 or 1: CONS"))))] +(defn pair-alt + [x y] + (let [dispatch (fn [m] + (cond + (= m 0) x + (= m 1) y + :else (throw (Exception. "Argument not 0 or 1: CONS"))))] dispatch)) -(defn car-alt [z] (z 0)) -(defn cdr-alt [z] (z 1)) +(defn car-alt + [z] + (z 0)) + +(defn cdr-alt + [z] + (z 1)) + +(comment "2.1.4 Extended Exercise: Interval Arithmetic -------------------------------------------") -(comment "2.1.4") -; Extended Exercise: Interval Arithmetic ----------------------------------------------------------- ; Exercises: ; * 2.7 ; * 2.8 @@ -101,7 +121,8 @@ ; * 2.15 ; * 2.16 -(defn mul-interval [x y] +(defn mul-interval + [x y] (let [p1 (* (ex-2-07/lower-bound x) (ex-2-07/lower-bound y)) p2 (* (ex-2-07/lower-bound x) @@ -113,12 +134,14 @@ (ex-2-07/make-interval (min p1 p2 p3 p4) (max p1 p2 p3 p4)))) -(defn div-interval [x y] +(defn div-interval + [x y] (mul-interval x (ex-2-07/make-interval (/ 1.0 (ex-2-07/upper-bound y)) (/ 1.0 (ex-2-07/lower-bound y))))) -(defn add-interval [x y] +(defn add-interval + [x y] (ex-2-07/make-interval (+ (ex-2-07/lower-bound x) (ex-2-07/lower-bound y)) (+ (ex-2-07/upper-bound x) diff --git a/src/sicp/chapter_2/part_1/ex_2_01.clj b/src/sicp/chapter_2/part_1/ex_2_01.clj index 5d4fcd7..874a428 100644 --- a/src/sicp/chapter_2/part_1/ex_2_01.clj +++ b/src/sicp/chapter_2/part_1/ex_2_01.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-1.ex-2-01 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.1 ; Define a better version of make-rat that handles both positive and negative arguments. @@ -7,7 +8,8 @@ ; if the rational number is positive, both the numerator and denominator are positive ; and if the rational number is negative, only the numerator is negative. -(defn make-rat-2 [numer denom] +(defn make-rat-2 + [numer denom] (let [devider (abs (m/gcd numer denom)) signed-numer (cond (and (< denom 0) (< numer 0)) (abs numer) (and (< denom 0) (> numer 0)) (- numer) diff --git a/src/sicp/chapter_2/part_1/ex_2_02.clj b/src/sicp/chapter_2/part_1/ex_2_02.clj index 26b9f20..9c794db 100644 --- a/src/sicp/chapter_2/part_1/ex_2_02.clj +++ b/src/sicp/chapter_2/part_1/ex_2_02.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-1.ex-2-02 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.2 ; Consider the problem of representing line segments in a plane. @@ -25,28 +26,36 @@ ; (display (y-point p)) ; (display ")")) -(defn make-point [x y] +(defn make-point + [x y] (m/pair x y)) -(defn x-point [point] +(defn x-point + [point] (m/car point)) -(defn y-point [point] +(defn y-point + [point] (m/cdr point)) -(defn print-point [point] +(defn print-point + [point] (str "(" (x-point point) "," (y-point point) ")")) -(defn make-segment [start end] +(defn make-segment + [start end] (m/pair start end)) -(defn start-segment [segment] +(defn start-segment + [segment] (m/car segment)) -(defn end-segment [segment] +(defn end-segment + [segment] (m/cdr segment)) -(defn midpoint-segment [segment] +(defn midpoint-segment + [segment] (make-point (m/average (x-point (start-segment segment)) (x-point (end-segment segment))) (m/average (y-point (start-segment segment)) diff --git a/src/sicp/chapter_2/part_1/ex_2_03.clj b/src/sicp/chapter_2/part_1/ex_2_03.clj index 363bddc..7a02655 100644 --- a/src/sicp/chapter_2/part_1/ex_2_03.clj +++ b/src/sicp/chapter_2/part_1/ex_2_03.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-1.ex-2-03 - (:require [sicp.chapter-2.part-1.ex-2-02 :as ex-2-02] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-1.ex-2-02 :as ex-2-02] + [sicp.misc :as m])) ; Exercise 2.3 ; Implement a representation for rectangles in a plane. @@ -12,7 +13,8 @@ ; Can you design your system with suitable abstraction barriers, ; so that the same perimeter and area procedures will work using either representation? -(defn length-segment [segment] +(defn length-segment + [segment] (Math/sqrt (+ (Math/pow (- (ex-2-02/x-point (ex-2-02/start-segment segment)) (ex-2-02/x-point (ex-2-02/end-segment segment))) 2) @@ -20,19 +22,24 @@ (ex-2-02/y-point (ex-2-02/end-segment segment))) 2)))) -(defn make-rectangle [segment-width segment-height] +(defn make-rectangle + [segment-width segment-height] (m/pair segment-width segment-height)) -(defn side-width-rectangle [rectangle] +(defn side-width-rectangle + [rectangle] (m/car rectangle)) -(defn side-height-rectangle [rectangle] +(defn side-height-rectangle + [rectangle] (m/cdr rectangle)) -(defn area-rectangle [rectangle] +(defn area-rectangle + [rectangle] (* (length-segment (side-width-rectangle rectangle)) (length-segment (side-height-rectangle rectangle)))) -(defn perimeter-rectangle [rectangle] +(defn perimeter-rectangle + [rectangle] (* 2 (+ (length-segment (side-width-rectangle rectangle)) (length-segment (side-height-rectangle rectangle))))) diff --git a/src/sicp/chapter_2/part_1/ex_2_04.clj b/src/sicp/chapter_2/part_1/ex_2_04.clj index 287fcfc..186ddbb 100644 --- a/src/sicp/chapter_2/part_1/ex_2_04.clj +++ b/src/sicp/chapter_2/part_1/ex_2_04.clj @@ -13,11 +13,14 @@ ; What is the corresponding definition of cdr? ; (Hint: To verify that this works, make use of the substitution model of 1.1.5.) -(defn pair [x y] +(defn pair + [x y] (fn [m] (m x y))) -(defn car [z] +(defn car + [z] (z (fn [p _] p))) -(defn cdr [z] +(defn cdr + [z] (z (fn [_ q] q))) diff --git a/src/sicp/chapter_2/part_1/ex_2_05.clj b/src/sicp/chapter_2/part_1/ex_2_05.clj index c3d91d7..bc1bf6e 100644 --- a/src/sicp/chapter_2/part_1/ex_2_05.clj +++ b/src/sicp/chapter_2/part_1/ex_2_05.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-1.ex-2-05 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.5 ; @@ -9,17 +10,20 @@ ; ; Give the corresponding definitions of the procedures cons, car, and cdr. -(defn pair [x y] +(defn pair + [x y] (* (Math/pow 2 x) (Math/pow 3 y))) -(defn car [z] +(defn car + [z] (loop [x z count 0 divisor 2] (if (m/divides? divisor x) (recur (/ x divisor) (inc count) divisor) count))) -(defn cdr [z] +(defn cdr + [z] (loop [x z count 0 divisor 3] (if (m/divides? divisor x) (recur (/ x divisor) (inc count) divisor) diff --git a/src/sicp/chapter_2/part_1/ex_2_06.clj b/src/sicp/chapter_2/part_1/ex_2_06.clj index 2a01f07..56c8c06 100644 --- a/src/sicp/chapter_2/part_1/ex_2_06.clj +++ b/src/sicp/chapter_2/part_1/ex_2_06.clj @@ -25,9 +25,13 @@ (def one (fn [f] (fn [x] (f x)))) (def two (fn [f] (fn [x] (f (f x))))) -(defn add-1 [next-item-fn] +(defn add-1 + [next-item-fn] (fn [f] (fn [x] (f ((next-item-fn f) x))))) -(defn add [num-1 num-2] - (fn [f] (fn [x] ((num-1 f) - ((num-2 f) x))))) +(defn add + [num-1 num-2] + (fn [f] + (fn [x] + ((num-1 f) + ((num-2 f) x))))) diff --git a/src/sicp/chapter_2/part_1/ex_2_07.clj b/src/sicp/chapter_2/part_1/ex_2_07.clj index d30afba..aa04ca5 100644 --- a/src/sicp/chapter_2/part_1/ex_2_07.clj +++ b/src/sicp/chapter_2/part_1/ex_2_07.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-1.ex-2-07 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.7 ; Alyssa’s program is incomplete because she has not specified @@ -8,11 +9,14 @@ ; (define (make-interval a b) (cons a b)) ; Define selectors upper-bound and lower-bound to complete the implementation. -(defn make-interval [low high] +(defn make-interval + [low high] (m/pair low high)) -(defn lower-bound [interval] +(defn lower-bound + [interval] (m/car interval)) -(defn upper-bound [interval] +(defn upper-bound + [interval] (m/cdr interval)) diff --git a/src/sicp/chapter_2/part_1/ex_2_08.clj b/src/sicp/chapter_2/part_1/ex_2_08.clj index 3b97f46..e447917 100644 --- a/src/sicp/chapter_2/part_1/ex_2_08.clj +++ b/src/sicp/chapter_2/part_1/ex_2_08.clj @@ -1,11 +1,13 @@ (ns sicp.chapter-2.part-1.ex-2-08 - (:require [sicp.chapter-2.part-1.ex-2-07 :as ex-2-07])) + (:require + [sicp.chapter-2.part-1.ex-2-07 :as ex-2-07])) ; Exercise 2.8 ; Using reasoning analogous to Alyssa’s, describe how the difference of two intervals may be computed. ; Define a corresponding subtraction procedure, called sub-interval. -(defn sub-interval [interval-1 interval-2] +(defn sub-interval + [interval-1 interval-2] (ex-2-07/make-interval (- (ex-2-07/lower-bound interval-1) (ex-2-07/lower-bound interval-2)) (- (ex-2-07/upper-bound interval-1) diff --git a/src/sicp/chapter_2/part_1/ex_2_09.clj b/src/sicp/chapter_2/part_1/ex_2_09.clj index fdba341..300a70e 100644 --- a/src/sicp/chapter_2/part_1/ex_2_09.clj +++ b/src/sicp/chapter_2/part_1/ex_2_09.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-1.ex-2-09 - (:require [sicp.chapter-2.part-1.ex-2-07 :as ex-2-07])) + (:require + [sicp.chapter-2.part-1.ex-2-07 :as ex-2-07])) ; Exercise 2.9 ; The width of an interval is half of the difference between its upper and lower bounds. @@ -11,6 +12,7 @@ ; only of the widths of the intervals being added (or subtracted). ; Give examples to show that this is not true for multiplication or division. -(defn radius [interval] +(defn radius + [interval] (/ (- (ex-2-07/upper-bound interval) (ex-2-07/lower-bound interval)) 2)) diff --git a/src/sicp/chapter_2/part_1/ex_2_10.clj b/src/sicp/chapter_2/part_1/ex_2_10.clj index 365762e..c36637c 100644 --- a/src/sicp/chapter_2/part_1/ex_2_10.clj +++ b/src/sicp/chapter_2/part_1/ex_2_10.clj @@ -1,12 +1,14 @@ (ns sicp.chapter-2.part-1.ex-2-10 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.10 ; Ben Bitdiddle, an expert systems programmer, looks over Alyssa’s shoulder ; and comments that it is not clear what it means to divide by an interval that spans zero. ; Modify Alyssa’s code to check for this condition and to signal an error if it occurs. -(defn div-interval [interval-1 interval-2] +(defn div-interval + [interval-1 interval-2] (if (< (* (m/lower-bound interval-2) (m/upper-bound interval-2)) 0) diff --git a/src/sicp/chapter_2/part_1/ex_2_11.clj b/src/sicp/chapter_2/part_1/ex_2_11.clj index 92ba19b..7527a9e 100644 --- a/src/sicp/chapter_2/part_1/ex_2_11.clj +++ b/src/sicp/chapter_2/part_1/ex_2_11.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-1.ex-2-11 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.11 ; In passing, Ben also cryptically comments: “By testing the signs of the endpoints @@ -34,23 +35,28 @@ ; Engineers usually specify percentage tolerances on the parameters of devices, ; as in the resistor specifications given earlier. -(defn make-center-width [c w] +(defn make-center-width + [c w] (m/make-interval (- c w) (+ c w))) -(defn center [i] +(defn center + [i] (/ (+ (m/lower-bound i) (m/upper-bound i)) 2.0)) -(defn width [i] +(defn width + [i] (/ (- (m/upper-bound i) (m/lower-bound i)) 2.0)) ; Solution -(defn interval-case [low high] +(defn interval-case + [low high] (cond (and (< low 0) (< high 0)) -1 (and (< low 0) (> high 0)) 0 :else 1)) -(defn mul-interval [i1 i2] +(defn mul-interval + [i1 i2] (let [i1-lo (m/lower-bound i1) i1-hi (m/upper-bound i1) i2-lo (m/lower-bound i2) diff --git a/src/sicp/chapter_2/part_1/ex_2_12.clj b/src/sicp/chapter_2/part_1/ex_2_12.clj index c213821..f0c6604 100644 --- a/src/sicp/chapter_2/part_1/ex_2_12.clj +++ b/src/sicp/chapter_2/part_1/ex_2_12.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-1.ex-2-12 - (:require [sicp.chapter-2.part-1.ex-2-11 :as ex-2-11] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-1.ex-2-11 :as ex-2-11] + [sicp.misc :as m])) ; Exercise 2.12 ; Define a constructor make-center-percent that takes a center and a percentage tolerance @@ -9,12 +10,14 @@ ; for a given interval. ; The center selector is the same as the one shown above. -(defn make-center-percent [average percent] +(defn make-center-percent + [average percent] (let [diff (* average (/ percent 100.0)) low (- average diff) high (+ average diff)] (m/make-interval low high))) -(defn percent [interval] +(defn percent + [interval] (* 100.0 (/ (- (m/upper-bound interval) (ex-2-11/center interval)) (ex-2-11/center interval)))) diff --git a/src/sicp/chapter_2/part_1/ex_2_13.clj b/src/sicp/chapter_2/part_1/ex_2_13.clj index 0bbc57a..158cb92 100644 --- a/src/sicp/chapter_2/part_1/ex_2_13.clj +++ b/src/sicp/chapter_2/part_1/ex_2_13.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-1.ex-2-13 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.13 ; Show that under the assumption of small percentage tolerances there is a simple formula for @@ -34,11 +35,13 @@ ; Lem complains that Alyssa’s program gives different answers for the two ways of computing. ; This is a serious complaint. -(defn par1 [r1 r2] +(defn par1 + [r1 r2] (m/div-interval (m/mul-interval r1 r2) (m/add-interval r1 r2))) -(defn par2 [r1 r2] +(defn par2 + [r1 r2] (let [one (m/make-interval 1 1)] (m/div-interval one (m/add-interval (m/div-interval one r1) diff --git a/src/sicp/chapter_2/part_1/ex_2_14.clj b/src/sicp/chapter_2/part_1/ex_2_14.clj index 7a4597d..ff5a2f0 100644 --- a/src/sicp/chapter_2/part_1/ex_2_14.clj +++ b/src/sicp/chapter_2/part_1/ex_2_14.clj @@ -8,4 +8,3 @@ ; You will get the most insight by using intervals whose width is a small percentage ; of the center value. Examine the results of the computation in center-percent form ; (see Exercise 2.12). - diff --git a/src/sicp/chapter_2/part_2/book_2_2.clj b/src/sicp/chapter_2/part_2/book_2_2.clj index 1820834..86c9ffc 100644 --- a/src/sicp/chapter_2/part_2/book_2_2.clj +++ b/src/sicp/chapter_2/part_2/book_2_2.clj @@ -1,19 +1,19 @@ (ns sicp.chapter-2.part-2.book-2-2 - (:require [sicp.chapter-1.part-2.book-1-2 :refer [fib]] - [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46] - [sicp.chapter-2.part-2.ex-2-47 :as ex-2-47] - [sicp.chapter-2.part-2.ex-2-48 :as ex-2-48] - [sicp.misc :as m])) + (:require + [sicp.chapter-1.part-2.book-1-2 :refer [fib]] + [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46] + [sicp.chapter-2.part-2.ex-2-47 :as ex-2-47] + [sicp.chapter-2.part-2.ex-2-48 :as ex-2-48] + [sicp.misc :as m])) -(comment "2.2") -; Hierarchical Data and the Closure Property ------------------------------------------------------- +(comment "2.2 Hierarchical Data and the Closure Property -----------------------------------------") (comment (m/pair (m/pair 1 2) (m/pair 3 4)) ; [[1 2] [3 4]] (m/pair (m/pair 1 (m/pair 2 3)) 4)) ; [[1 [2 3]] 4] -(comment "2.2.1") -; Representing Sequences --------------------------------------------------------------------------- +(comment "2.2.1 Representing Sequences -----------------------------------------------------------") + ; Exercises: ; * 2.17 ; * 2.18 @@ -30,7 +30,8 @@ (m/cdr '(1 2 3 4)) ; 2 (m/cdr '(1 (2 (3 4))))) ; (2 (3 4)) -(defn list-ref [lst index] +(defn list-ref + [lst index] (cond (< index 0) nil (= index 0) (first lst) @@ -40,7 +41,8 @@ (def squares '(1 4 9 16 25)) (list-ref squares 3)) ; 16 -(defn length-recursice [items] +(defn length-recursice + [items] (if (m/list-empty? items) 0 (+ 1 (length-recursice (m/cdr items))))) @@ -48,7 +50,8 @@ (comment (length-recursice (list 1 3 5 7))) ; 4 -(defn length [list] +(defn length + [list] (if (seq list) (+ 1 (length (rest list))) 0)) @@ -56,7 +59,8 @@ (comment (length (list 1 3 5 7))) ; 4 -(defn append [list1 list2] +(defn append + [list1 list2] (if (empty? list1) (if (empty? list2) '() list2) (cons (first list1) (append (rest list1) list2)))) @@ -65,7 +69,8 @@ (append '(1 4 9 16 25) '(1 3 5 7)) ; (1 4 9 16 25 1 3 5 7) (append '(1 3 5 7) '(1 4 9 16 25))) ; (1 3 5 7 1 4 9 16 25) -(defn scale-list [items factor] +(defn scale-list + [items factor] (if (m/list-empty? items) nil (cons (* (m/car items) factor) @@ -75,7 +80,8 @@ (comment (scale-list (list 1 2 3 4 5) 10)) ; (10 20 30 40 50) -(defn my-map [proc items] +(defn my-map + [proc items] (if (m/list-empty? items) nil (cons (proc (m/car items)) @@ -85,14 +91,15 @@ (my-map abs (list -10 2.5 -11.6 17)) ; (10 2.5 11.6 17) (my-map #(* % %) (list 1 2 3 4))) ; (1 4 9 16) -(defn scale-list-2 [items factor] +(defn scale-list-2 + [items factor] (my-map #(* % factor) items)) (comment (scale-list-2 (list 1 2 3 4 5) 10)) ; (10 20 30 40 50) -(comment "2.2.2") -; Hierarchical Structures -------------------------------------------------------------------------- +(comment "2.2.2 Hierarchical Structures ----------------------------------------------------------") + ; Exercises: ; * 2.24 ; * 2.25 @@ -104,31 +111,35 @@ ; * 2.31 ; * 2.32 -(defn length-tree [tree] +(defn length-tree + [tree] (reduce (fn [acc _] (inc acc)) 0 tree)) -(defn count-leaves [tree] +(defn count-leaves + [tree] (cond (number? tree) 1 (or (empty? tree) (nil? tree)) 0 :else (+ (count-leaves (m/car tree)) (count-leaves (m/cdr tree))))) -(defn scale-tree-v0 [tree factor] +(defn scale-tree-v0 + [tree factor] (cond (m/list-empty? tree) nil (m/leaf? tree) (* tree factor) :else (cons (scale-tree-v0 (first tree) factor) (scale-tree-v0 (rest tree) factor)))) -(defn scale-tree [tree factor] +(defn scale-tree + [tree factor] (map (fn [sub-tree] (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree factor))) tree)) -(comment "2.2.3") -; Sequences as Conventional Interfaces ------------------------------------------------------------- +(comment "2.2.3 Sequences as Conventional Interfaces ---------------------------------------------") + ; Exercises: ; * 2.33 ; * 2.34 @@ -142,14 +153,17 @@ ; * 2.42 ; * 2.43 -(defn sum-odd-squares [tree] +(defn sum-odd-squares + [tree] (cond (m/list-empty? tree) 0 (m/leaf? tree) (if (odd? tree) (m/square tree) 0) :else (+ (sum-odd-squares (first tree)) (sum-odd-squares (rest tree))))) -(defn even-fibs [n] - (letfn [(next [k] +(defn even-fibs + [n] + (letfn [(next + [k] (if (> k n) nil (let [f (fib k)] @@ -158,31 +172,36 @@ (next (inc k))))))] (next 0))) -(defn my-filter [predicate sequence] +(defn my-filter + [predicate sequence] (cond (m/list-empty? sequence) nil (predicate (m/car sequence)) (cons (m/car sequence) (my-filter predicate (m/cdr sequence))) :else (my-filter predicate (m/cdr sequence)))) -(defn accumulate [op initial sequence] +(defn accumulate + [op initial sequence] (if (m/list-empty? sequence) initial (op (m/car sequence) (accumulate op initial (m/cdr sequence))))) -(defn enumerate-interval [low high] +(defn enumerate-interval + [low high] (if (> low high) nil (cons low (enumerate-interval (+ low 1) high)))) -(defn enumerate-tree [tree] +(defn enumerate-tree + [tree] (cond (m/list-empty? tree) nil (m/leaf? tree) (list tree) :else (m/append (enumerate-tree (m/car tree)) (enumerate-tree (m/cdr tree))))) -(defn sum-odd-squares-v2 [tree] +(defn sum-odd-squares-v2 + [tree] (accumulate + 0 @@ -190,7 +209,8 @@ (filter odd? (enumerate-tree tree))))) -(defn even-fibs-v2 [n] +(defn even-fibs-v2 + [n] (accumulate cons nil @@ -198,7 +218,8 @@ (map fib (enumerate-interval 0 n))))) -(defn list-fib-squares [n] +(defn list-fib-squares + [n] (accumulate cons nil @@ -206,7 +227,8 @@ (map fib (enumerate-interval 0 n))))) -(defn product-of-squares-of-odd-elements [sequence] +(defn product-of-squares-of-odd-elements + [sequence] (accumulate * 1 @@ -227,18 +249,22 @@ (enumerate-interval 1 6)))) ; (1 2 3 4 5 6) ; ((2 1) (3 1) (3 2) (4 1) (4 2) (4 3) (5 1) (5 2) (5 3) (5 4) (6 1) (6 2) (6 3) (6 4) (6 5)) -(defn flatmap [proc seq] +(defn flatmap + [proc seq] (accumulate append nil (map proc seq))) -(defn prime-sum? [pair] +(defn prime-sum? + [pair] (m/prime? (+ (m/car pair) (m/cdr pair)))) -(defn make-pair-sum [pair] +(defn make-pair-sum + [pair] (list (m/car pair) (m/cdr pair) (+ (m/car pair) (m/cdr pair)))) -(defn prime-sum-pairs [n] +(defn prime-sum-pairs + [n] (map make-pair-sum (filter prime-sum? (flatmap (fn [i] @@ -246,10 +272,12 @@ (enumerate-interval 1 (- i 1)))) (enumerate-interval 1 n))))) -(defn my-remove [item sequence] +(defn my-remove + [item sequence] (filter (fn [x] (not (= x item))) sequence)) -(defn permutations [s] +(defn permutations + [s] (if (m/list-empty? s) ; empty set? (list nil) ; sequence containing empty set (flatmap (fn [x] @@ -257,8 +285,8 @@ (permutations (my-remove x s)))) s))) -(comment "2.2.4") -; Example: A Picture Language ---------------------------------------------------------------------- +(comment "2.2.4 Example: A Picture Language ------------------------------------------------------") + ; Exercises: ; * 2.44 ; * 2.45 @@ -271,12 +299,24 @@ ; * 2.52 ; The part of book has fake functions -(defn below [wave-1 wave-2] (comment wave-1 wave-2)) -(defn flip-horiz [painter] (comment painter)) -(defn rotate180 [wave] (comment wave)) -(defn draw-line [param1 param2] (comment param1 param2)) +(defn below + [wave-1 wave-2] + (comment wave-1 wave-2)) + +(defn flip-horiz + [painter] + (comment painter)) + +(defn rotate180 + [wave] + (comment wave)) + +(defn draw-line + [param1 param2] + (comment param1 param2)) -(defn frame-coord-map [frame] +(defn frame-coord-map + [frame] (fn [v] (ex-2-46/add-vect (ex-2-47/origin-frame frame) @@ -288,7 +328,8 @@ ; ((frame-coord-map a-frame) (ex-2-46/make-vect 0 0)) ; (origin-frame a-frame) -(defn segments->painter [segment-list] +(defn segments->painter + [segment-list] (fn [frame] (doseq [segment segment-list] (let [start (ex-2-48/start-segment segment) @@ -296,7 +337,8 @@ frame-coord-map-int (frame-coord-map frame)] (draw-line (frame-coord-map-int start) (frame-coord-map-int end)))))) -(defn transform-painter [painter origin corner1 corner2] +(defn transform-painter + [painter origin corner1 corner2] (fn [frame] (let [m (frame-coord-map frame) new-origin (m origin)] @@ -305,26 +347,30 @@ (ex-2-46/sub-vect (m corner1) new-origin) (ex-2-46/sub-vect (m corner2) new-origin)))))) -(defn flip-vert [painter] +(defn flip-vert + [painter] (transform-painter painter (ex-2-46/make-vect 0.0 1.0) ; new origin (ex-2-46/make-vect 1.0 1.0) ; new end of edge1 (ex-2-46/make-vect 0.0 0.0))) ; new end of edge2 -(defn rotate90 [painter] +(defn rotate90 + [painter] (transform-painter painter (ex-2-46/make-vect 1.0 0.0) (ex-2-46/make-vect 1.0 1.0) (ex-2-46/make-vect 0.0 0.0))) -(defn squash-inwards [painter] +(defn squash-inwards + [painter] (transform-painter painter (ex-2-46/make-vect 0.0 0.0) (ex-2-46/make-vect 0.65 0.35) (ex-2-46/make-vect 0.35 0.65))) -(defn beside [painter1 painter2] +(defn beside + [painter1 painter2] (let [split-point (ex-2-46/make-vect 0.5 0.0) paint-left (transform-painter painter1 @@ -340,7 +386,8 @@ (paint-left frame) (paint-right frame)))) -(defn corner-split [painter n] +(defn corner-split + [painter n] (if (= n 0) painter (let [up (corner-split painter (dec n)) @@ -351,16 +398,19 @@ (beside (below painter top-left) (below bottom-right corner))))) -(defn square-of-four [tl tr bl br] +(defn square-of-four + [tl tr bl br] (fn [painter] (let [top (beside (tl painter) (tr painter)) bottom (beside (bl painter) (br painter))] (below top bottom)))) -(defn flipped-pairs [painter] +(defn flipped-pairs + [painter] (let [combine4 (fn [p] ((square-of-four identity flip-vert identity flip-vert) p))] (combine4 painter))) -(defn square-limit [painter n] +(defn square-limit + [painter n] (let [combine4 (fn [p] ((square-of-four flip-horiz identity rotate180 flip-vert) p))] (combine4 (corner-split painter n)))) diff --git a/src/sicp/chapter_2/part_2/ex_2_17.clj b/src/sicp/chapter_2/part_2/ex_2_17.clj index 08f4ca1..f58ffcc 100644 --- a/src/sicp/chapter_2/part_2/ex_2_17.clj +++ b/src/sicp/chapter_2/part_2/ex_2_17.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-17 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.17 ; Define a procedure last-pair that returns the list that contains only the last element of @@ -8,8 +9,10 @@ ; (last-pair (list 23 72 149 34)) ; (34) -(defn last-pair [items] - (letfn [(iter [list-current result] +(defn last-pair + [items] + (letfn [(iter + [list-current result] (if (m/list-empty? list-current) result (iter diff --git a/src/sicp/chapter_2/part_2/ex_2_18.clj b/src/sicp/chapter_2/part_2/ex_2_18.clj index 059d0b4..288758f 100644 --- a/src/sicp/chapter_2/part_2/ex_2_18.clj +++ b/src/sicp/chapter_2/part_2/ex_2_18.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-18 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.misc :as m])) ; Exercise 2.18 ; Define a procedure reverse that takes a list as argument and returns a list of the @@ -9,7 +10,8 @@ ; (reverse (list 1 4 9 16 25)) ; (25 16 9 4 1) -(defn list-reverse [items] +(defn list-reverse + [items] (if (m/list-empty? items) '() (b22/append (list-reverse (m/cdr items)) (list (m/car items))))) diff --git a/src/sicp/chapter_2/part_2/ex_2_19.clj b/src/sicp/chapter_2/part_2/ex_2_19.clj index f93961b..3799de2 100644 --- a/src/sicp/chapter_2/part_2/ex_2_19.clj +++ b/src/sicp/chapter_2/part_2/ex_2_19.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-19 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.19 ; Consider the change-counting program of 1.2.2. @@ -27,7 +28,7 @@ ; To do this will require changing the program cc somewhat. ; It will still have the same form, but it will access its second argument differently, as follows: ; -;(define (cc amount coin-values) +; (define (cc amount coin-values) ; (cond ((= amount 0) ; 1) ; ((or (< amount 0) @@ -47,16 +48,20 @@ ; in terms of primitive operations on list structures. ; Does the order of the list coin-values affect the answer produced by cc? Why or why not? -(defn no-more? [coin-values] +(defn no-more? + [coin-values] (m/list-empty? coin-values)) -(defn except-first-denomination [coin-values] +(defn except-first-denomination + [coin-values] (m/cdr coin-values)) -(defn first-denomination [coin-values] +(defn first-denomination + [coin-values] (m/car coin-values)) -(defn cc [amount coin-values] +(defn cc + [amount coin-values] (cond (= amount 0) 1 (or (< amount 0) (no-more? coin-values)) 0 diff --git a/src/sicp/chapter_2/part_2/ex_2_20.clj b/src/sicp/chapter_2/part_2/ex_2_20.clj index 37b9e79..fcfc17c 100644 --- a/src/sicp/chapter_2/part_2/ex_2_20.clj +++ b/src/sicp/chapter_2/part_2/ex_2_20.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-20 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.misc :as m])) ; Exercise 2.20 ; @@ -37,8 +38,10 @@ ; (same-parity 2 3 4 5 6 7) ; (2 4 6) -(defn same-parity [base & check-list] - (letfn [(iter [rest result] +(defn same-parity + [base & check-list] + (letfn [(iter + [rest result] (if (m/list-empty? rest) result (if (= (mod (m/car rest) 2) (mod base 2)) diff --git a/src/sicp/chapter_2/part_2/ex_2_21.clj b/src/sicp/chapter_2/part_2/ex_2_21.clj index 53ca36c..5f8cd0b 100644 --- a/src/sicp/chapter_2/part_2/ex_2_21.clj +++ b/src/sicp/chapter_2/part_2/ex_2_21.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-21 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.21 ; @@ -19,11 +20,13 @@ ; (define (square-list items) ; (map ⟨??⟩ ⟨??⟩)) -(defn square-list-1 [items] +(defn square-list-1 + [items] (if (m/list-empty? items) nil (cons (* (m/car items) (m/car items)) (square-list-1 (m/cdr items))))) -(defn square-list-2 [items] +(defn square-list-2 + [items] (map #(* % %) items)) diff --git a/src/sicp/chapter_2/part_2/ex_2_22.clj b/src/sicp/chapter_2/part_2/ex_2_22.clj index 04c73f6..e29a8e0 100644 --- a/src/sicp/chapter_2/part_2/ex_2_22.clj +++ b/src/sicp/chapter_2/part_2/ex_2_22.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-22 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.22 ; @@ -32,8 +33,10 @@ ; ; This doesn’t work either. Explain. -(defn square-list-1 [items] - (letfn [(iter [things answer] +(defn square-list-1 + [items] + (letfn [(iter + [things answer] (if (m/list-empty? things) answer (iter (m/cdr things) diff --git a/src/sicp/chapter_2/part_2/ex_2_23.clj b/src/sicp/chapter_2/part_2/ex_2_23.clj index ac34724..2c86503 100644 --- a/src/sicp/chapter_2/part_2/ex_2_23.clj +++ b/src/sicp/chapter_2/part_2/ex_2_23.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-23 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.23 ; @@ -20,7 +21,8 @@ ; The value returned by the call to for-each (not illustrated above) can ; be something arbitrary, such as true. Give an implementation of for-each. -(defn for-each [proc items] +(defn for-each + [proc items] (if (m/list-empty? items) true (do diff --git a/src/sicp/chapter_2/part_2/ex_2_27.clj b/src/sicp/chapter_2/part_2/ex_2_27.clj index 86c59ff..e579671 100644 --- a/src/sicp/chapter_2/part_2/ex_2_27.clj +++ b/src/sicp/chapter_2/part_2/ex_2_27.clj @@ -12,7 +12,8 @@ ; (deep-reverse x) ; ((4 3) (2 1)) ; -(defn deep-reverse [coll] +(defn deep-reverse + [coll] (if (list? coll) (map deep-reverse (reverse coll)) coll)) diff --git a/src/sicp/chapter_2/part_2/ex_2_28.clj b/src/sicp/chapter_2/part_2/ex_2_28.clj index dd6765d..08fb196 100644 --- a/src/sicp/chapter_2/part_2/ex_2_28.clj +++ b/src/sicp/chapter_2/part_2/ex_2_28.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-28 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.28 ; @@ -11,7 +12,8 @@ ; (fringe x) => (1 2 3 4) ; (fringe (list x x)) => (1 2 3 4 1 2 3 4) -(defn fringe [lst] +(defn fringe + [lst] (cond (nil? lst) nil (m/leaf? lst) (list lst) diff --git a/src/sicp/chapter_2/part_2/ex_2_29.clj b/src/sicp/chapter_2/part_2/ex_2_29.clj index 41f092f..4df0c7a 100644 --- a/src/sicp/chapter_2/part_2/ex_2_29.clj +++ b/src/sicp/chapter_2/part_2/ex_2_29.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-29 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.29 ; @@ -38,27 +39,50 @@ ; How much do you need to change your programs to convert to the new representation? ; Solution -(defn make-mobile [left right] (m/pair left right)) -(defn make-branch [length structure] (m/pair length structure)) -(defn left-branch [mobile] (m/car mobile)) -(defn right-branch [mobile] (m/cdr mobile)) -(defn branch-length [branch] (m/car branch)) -(defn branch-structure [branch] (m/cdr branch)) -(defn branch-has-structure? [branch] (m/pair? (branch-structure branch))) +(defn make-mobile + [left right] + (m/pair left right)) -(defn total-weight [mobile] +(defn make-branch + [length structure] + (m/pair length structure)) + +(defn left-branch + [mobile] + (m/car mobile)) + +(defn right-branch + [mobile] + (m/cdr mobile)) + +(defn branch-length + [branch] + (m/car branch)) + +(defn branch-structure + [branch] + (m/cdr branch)) + +(defn branch-has-structure? + [branch] + (m/pair? (branch-structure branch))) + +(defn total-weight + [mobile] (cond (m/leaf? mobile) 0 (m/leaf? (branch-structure mobile)) (branch-structure mobile) :else (+ (total-weight (left-branch mobile)) (total-weight (right-branch mobile))))) -(defn branch-moment [branch] +(defn branch-moment + [branch] (if (branch-has-structure? branch) (* (branch-length branch) (total-weight (branch-structure branch))) (* (branch-length branch) (branch-structure branch)))) -(defn mobile-balanced? [mobile] +(defn mobile-balanced? + [mobile] (let [left (left-branch mobile) right (right-branch mobile)] (and (= (branch-moment left) (branch-moment right)) diff --git a/src/sicp/chapter_2/part_2/ex_2_30.clj b/src/sicp/chapter_2/part_2/ex_2_30.clj index 18329cb..022db8a 100644 --- a/src/sicp/chapter_2/part_2/ex_2_30.clj +++ b/src/sicp/chapter_2/part_2/ex_2_30.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-30 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.30 ; @@ -16,13 +17,15 @@ ; Define square-tree both directly (i.e., without using any higher-order procedures) ; and also by using map and recursion. -(defn square-tree [tree] +(defn square-tree + [tree] (cond (m/list-empty? tree) nil (m/leaf? tree) (m/square tree) :else (cons (square-tree (first tree)) (square-tree (rest tree))))) -(defn square-tree-map [tree] +(defn square-tree-map + [tree] (map (fn [sub-tree] (if (list? sub-tree) (square-tree-map sub-tree) diff --git a/src/sicp/chapter_2/part_2/ex_2_31.clj b/src/sicp/chapter_2/part_2/ex_2_31.clj index 10057de..337ae4a 100644 --- a/src/sicp/chapter_2/part_2/ex_2_31.clj +++ b/src/sicp/chapter_2/part_2/ex_2_31.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-31 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.31 ; @@ -9,12 +10,14 @@ ; (define (square-tree tree) ; (tree-map square tree)) -(defn tree-map [term tree] +(defn tree-map + [term tree] (map (fn [sub-tree] (if (list? sub-tree) (tree-map term sub-tree) (term sub-tree))) tree)) -(defn square-tree [tree] +(defn square-tree + [tree] (tree-map m/square tree)) diff --git a/src/sicp/chapter_2/part_2/ex_2_32.clj b/src/sicp/chapter_2/part_2/ex_2_32.clj index 5bb7cd5..f0999c2 100644 --- a/src/sicp/chapter_2/part_2/ex_2_32.clj +++ b/src/sicp/chapter_2/part_2/ex_2_32.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-32 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.32 ; @@ -18,7 +19,8 @@ ; (let ((rest (subsets (cdr s)))) ; (append rest (map ⟨??⟩ rest))))) -(defn subsets [s] +(defn subsets + [s] (if (empty? s) (list '()) (let [rest (subsets (rest s))] diff --git a/src/sicp/chapter_2/part_2/ex_2_33.clj b/src/sicp/chapter_2/part_2/ex_2_33.clj index 5bc8077..d1e5e79 100644 --- a/src/sicp/chapter_2/part_2/ex_2_33.clj +++ b/src/sicp/chapter_2/part_2/ex_2_33.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-33 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22])) ; Exercise 2.33 ; @@ -16,11 +17,14 @@ ; (define (length sequence) ; (accumulate ⟨??⟩ 0 sequence)) -(defn my-map [p sequence] +(defn my-map + [p sequence] (b22/accumulate (fn [x y] (cons (p x) y)) nil sequence)) -(defn my-append [seq1 seq2] +(defn my-append + [seq1 seq2] (b22/accumulate cons seq1 seq2)) -(defn my-length [sequence] +(defn my-length + [sequence] (b22/accumulate (fn [_ y] (inc y)) 0 sequence)) diff --git a/src/sicp/chapter_2/part_2/ex_2_34.clj b/src/sicp/chapter_2/part_2/ex_2_34.clj index 985fad1..5249a10 100644 --- a/src/sicp/chapter_2/part_2/ex_2_34.clj +++ b/src/sicp/chapter_2/part_2/ex_2_34.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-34 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22])) ; Exercise 2.34 ; @@ -27,7 +28,8 @@ ; ; (horner-eval 2 (list 1 3 0 5 0 1)) -(defn horner-eval [x coefficient-sequence] +(defn horner-eval + [x coefficient-sequence] (b22/accumulate (fn [this-coeff higher-terms] (+ (* higher-terms x) this-coeff)) 0 diff --git a/src/sicp/chapter_2/part_2/ex_2_35.clj b/src/sicp/chapter_2/part_2/ex_2_35.clj index 930fe10..6965e49 100644 --- a/src/sicp/chapter_2/part_2/ex_2_35.clj +++ b/src/sicp/chapter_2/part_2/ex_2_35.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-35 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.misc :as m])) ; Exercise 2.35 ; Redefine count-leaves from 2.2.2 as an accumulation: @@ -8,7 +9,8 @@ ; (define (count-leaves t) ; (accumulate ⟨??⟩ ⟨??⟩ (map ⟨??⟩ ⟨??⟩))) -(defn count-leaves [tree] +(defn count-leaves + [tree] (b22/accumulate + 0 (map (fn [sub-tree] (if (m/pair? sub-tree) (count-leaves sub-tree) diff --git a/src/sicp/chapter_2/part_2/ex_2_36.clj b/src/sicp/chapter_2/part_2/ex_2_36.clj index d581d8b..11b96ca 100644 --- a/src/sicp/chapter_2/part_2/ex_2_36.clj +++ b/src/sicp/chapter_2/part_2/ex_2_36.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-36 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.misc :as m])) ; Exercise 2.36 ; @@ -21,7 +22,8 @@ ; (accumulate-n op init ⟨??⟩)))) ; -(defn accumulate-n [op init seqs] +(defn accumulate-n + [op init seqs] (if (nil? (m/car seqs)) nil (cons (b22/accumulate op init (map m/car seqs)) diff --git a/src/sicp/chapter_2/part_2/ex_2_37.clj b/src/sicp/chapter_2/part_2/ex_2_37.clj index 09192b6..781aa23 100644 --- a/src/sicp/chapter_2/part_2/ex_2_37.clj +++ b/src/sicp/chapter_2/part_2/ex_2_37.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-37 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.chapter-2.part-2.ex-2-36 :as ex-2-36])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.chapter-2.part-2.ex-2-36 :as ex-2-36])) ; Exercise 2.37 ; @@ -40,15 +41,19 @@ ; (map ⟨??⟩ m))) ; -(defn dot-product [v w] +(defn dot-product + [v w] (b22/accumulate + 0 (map * v w))) -(defn matrix-*-vector [m v] +(defn matrix-*-vector + [m v] (map (fn [i] (dot-product i v)) m)) -(defn transpose [mat] +(defn transpose + [mat] (ex-2-36/accumulate-n cons nil mat)) -(defn matrix-*-matrix [m n] +(defn matrix-*-matrix + [m n] (let [cols (transpose n)] (map (fn [rows] (matrix-*-vector cols rows)) m))) diff --git a/src/sicp/chapter_2/part_2/ex_2_38.clj b/src/sicp/chapter_2/part_2/ex_2_38.clj index 0d5ff5a..030ce5c 100644 --- a/src/sicp/chapter_2/part_2/ex_2_38.clj +++ b/src/sicp/chapter_2/part_2/ex_2_38.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-38 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.misc :as m])) ; Exercise 2.38 ; @@ -28,11 +29,14 @@ ; Give a property that op should satisfy to guarantee that fold-right and fold-left ; will produce the same values for any sequence. -(defn fold-right [op initial sequence] +(defn fold-right + [op initial sequence] (b22/accumulate op initial sequence)) -(defn fold-left [op initial sequence] - (letfn [(iter [result rest] +(defn fold-left + [op initial sequence] + (letfn [(iter + [result rest] (if (nil? rest) result (iter (op result (m/car rest)) diff --git a/src/sicp/chapter_2/part_2/ex_2_39.clj b/src/sicp/chapter_2/part_2/ex_2_39.clj index 2fe3426..4016195 100644 --- a/src/sicp/chapter_2/part_2/ex_2_39.clj +++ b/src/sicp/chapter_2/part_2/ex_2_39.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-39 - (:require [sicp.chapter-2.part-2.ex-2-38 :as ex-2-38] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-2.ex-2-38 :as ex-2-38] + [sicp.misc :as m])) ; Exercise 2.39 ; @@ -15,11 +16,12 @@ ; (fold-left ; (lambda (x y) ⟨??⟩) nil sequence)) -(defn reverse-right [sequence] +(defn reverse-right + [sequence] (ex-2-38/fold-right (fn [x y] (m/append y (list x))) nil sequence)) -(defn reverse-left [sequence] +(defn reverse-left + [sequence] (ex-2-38/fold-left (fn [x y] (cons y x)) nil sequence)) -;(reverse-right '(1 2 3)) - +; (reverse-right '(1 2 3)) diff --git a/src/sicp/chapter_2/part_2/ex_2_40.clj b/src/sicp/chapter_2/part_2/ex_2_40.clj index b0dbd77..607122b 100644 --- a/src/sicp/chapter_2/part_2/ex_2_40.clj +++ b/src/sicp/chapter_2/part_2/ex_2_40.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-40 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22])) ; Exercise 2.40 ; @@ -8,14 +9,16 @@ ; ; Use unique-pairs to simplify the definition of prime-sum-pairs given above. -(defn unique-pairs [n] +(defn unique-pairs + [n] (b22/flatmap (fn [i] (map (fn [j] (list i j)) (b22/enumerate-interval 1 (- i 1)))) (b22/enumerate-interval 1 n))) -(defn prime-sum-pairs [n] +(defn prime-sum-pairs + [n] (map b22/make-pair-sum (filter b22/prime-sum? (unique-pairs n)))) diff --git a/src/sicp/chapter_2/part_2/ex_2_41.clj b/src/sicp/chapter_2/part_2/ex_2_41.clj index ecf3f27..0a9d538 100644 --- a/src/sicp/chapter_2/part_2/ex_2_41.clj +++ b/src/sicp/chapter_2/part_2/ex_2_41.clj @@ -1,32 +1,40 @@ (ns sicp.chapter-2.part-2.ex-2-41 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22])) ; Exercise 2.41 ; ; Write a procedure to find all ordered triples of distinct positive integers i, j, and k ; less than or equal to a given integer n that sum to a given integer s. -(defn duplets [n] - (b22/flatmap (fn [i] (map (fn [j] (list i j)) - (b22/enumerate-interval 1 n))) +(defn duplets + [n] + (b22/flatmap (fn [i] + (map (fn [j] (list i j)) + (b22/enumerate-interval 1 n))) (b22/enumerate-interval 1 n))) -(defn triplets [n] - (b22/flatmap (fn [i] (map (fn [j] (cons i j)) - (duplets n))) +(defn triplets + [n] + (b22/flatmap (fn [i] + (map (fn [j] (cons i j)) + (duplets n))) (b22/enumerate-interval 1 n))) -(defn check-triplet? [triplet] +(defn check-triplet? + [triplet] (and (< (b22/list-ref triplet 0) (b22/list-ref triplet 1)) (< (b22/list-ref triplet 1) (b22/list-ref triplet 2)))) -(defn check-triplet-sum? [triplet sum] +(defn check-triplet-sum? + [triplet sum] (= sum (+ (b22/list-ref triplet 0) (b22/list-ref triplet 1) (b22/list-ref triplet 2)))) -(defn find-triplets [n s] +(defn find-triplets + [n s] (filter (fn [i] (check-triplet-sum? i s)) (filter check-triplet? (triplets n)))) diff --git a/src/sicp/chapter_2/part_2/ex_2_42.clj b/src/sicp/chapter_2/part_2/ex_2_42.clj index ce3d048..65624a3 100644 --- a/src/sicp/chapter_2/part_2/ex_2_42.clj +++ b/src/sicp/chapter_2/part_2/ex_2_42.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-42 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.misc :as m])) ; Exercise 2.42 ; @@ -59,14 +60,24 @@ ; guaranteed safe with respect to each other.) ; Some utility functions -(defn make-pos [row column] (m/pair row column)) -(defn pos-row [position] (m/car position)) -(defn pos-col [position] (m/cdr position)) +(defn make-pos + [row column] + (m/pair row column)) -(defn adjoin-position [row column queens] +(defn pos-row + [position] + (m/car position)) + +(defn pos-col + [position] + (m/cdr position)) + +(defn adjoin-position + [row column queens] (m/append queens (list (make-pos row column)))) ; Important to add coords to the end of the list -(defn attacks? [position-1 position-2] +(defn attacks? + [position-1 position-2] (let [row-1 (pos-row position-1) col-1 (pos-col position-1) row-2 (pos-row position-2) @@ -79,12 +90,15 @@ (abs (- row-1 row-2))) true :else false))) -(defn safe? [k positions] +(defn safe? + [k positions] (let [kth-pos (nth positions (dec k))] (nil? (some (fn [pos] (attacks? kth-pos pos)) positions)))) -(defn queens [board-size] - (letfn [(queen-cols [k] +(defn queens + [board-size] + (letfn [(queen-cols + [k] (if (= 0 k) (list nil) ; It's `empty-board` (filter (fn [positions] (safe? k positions)) diff --git a/src/sicp/chapter_2/part_2/ex_2_43.clj b/src/sicp/chapter_2/part_2/ex_2_43.clj index 93f73d6..f7bec61 100644 --- a/src/sicp/chapter_2/part_2/ex_2_43.clj +++ b/src/sicp/chapter_2/part_2/ex_2_43.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-43 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.chapter-2.part-2.ex-2-42 :as ex-2-42])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.chapter-2.part-2.ex-2-42 :as ex-2-42])) ; Exercise 2.43 ; @@ -22,8 +23,10 @@ ; Estimate how long it will take Louis’s program to solve the eight-queens puzzle, ; assuming that the program in Exercise 2.42 solves the puzzle in time T. -(defn queens-2-42 [board-size] - (letfn [(queen-cols [k] +(defn queens-2-42 + [board-size] + (letfn [(queen-cols + [k] (if (= 0 k) (list nil) (filter (fn [positions] (ex-2-42/safe? k positions)) @@ -34,13 +37,17 @@ (queen-cols (- k 1))))))] (queen-cols board-size))) -(defn queens-2-43 [board-size] - (letfn [(queen-cols [k] +(defn queens-2-43 + [board-size] + (letfn [(queen-cols + [k] (if (= 0 k) (list nil) (filter (fn [positions] (ex-2-42/safe? k positions)) - (mapcat (fn [new-row] ; diff 1 - (map (fn [rest-of-queens] ; diff 1 + (mapcat (fn [new-row] + ; diff 1 + (map (fn [rest-of-queens] + ; diff 1 (ex-2-42/adjoin-position new-row k rest-of-queens)) (queen-cols (- k 1)))) ; diff 2 (b22/enumerate-interval 1 board-size)))))] ; diff 2 diff --git a/src/sicp/chapter_2/part_2/ex_2_44.clj b/src/sicp/chapter_2/part_2/ex_2_44.clj index 521f47c..a59d97c 100644 --- a/src/sicp/chapter_2/part_2/ex_2_44.clj +++ b/src/sicp/chapter_2/part_2/ex_2_44.clj @@ -1,12 +1,15 @@ (ns sicp.chapter-2.part-2.ex-2-44 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22])) ; Exercise 2.44 ; ; Define the procedure up-split used by corner-split. It is similar to right-split, ; except that it switches the roles of below and beside. -(defn up-split [painter n] ; Just a potential solution... +(defn up-split + [painter n] + ; Just a potential solution... (if (= n 0) painter (let [down (up-split painter (dec n)) diff --git a/src/sicp/chapter_2/part_2/ex_2_45.clj b/src/sicp/chapter_2/part_2/ex_2_45.clj index aae6537..0d04ad1 100644 --- a/src/sicp/chapter_2/part_2/ex_2_45.clj +++ b/src/sicp/chapter_2/part_2/ex_2_45.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-45 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22])) ; Exercise 2.45 ; @@ -11,7 +12,8 @@ ; ; produces procedures right-split and up-split with the same behaviors as the ones already defined. -(defn split [primary secondary] +(defn split + [primary secondary] (fn [painter n] (if (= n 0) painter diff --git a/src/sicp/chapter_2/part_2/ex_2_46.clj b/src/sicp/chapter_2/part_2/ex_2_46.clj index 5d03eae..1627ad4 100644 --- a/src/sicp/chapter_2/part_2/ex_2_46.clj +++ b/src/sicp/chapter_2/part_2/ex_2_46.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-46 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.46 ; @@ -15,27 +16,33 @@ ; (x1, y1) - (x2, y2) = (x1 - x2, y1 - y2) ; s*(x, y) = (s*x, s*y) -(defn make-vect [x y] +(defn make-vect + [x y] (m/pair x y)) -(defn xcor-vect [vect] +(defn xcor-vect + [vect] (m/car vect)) -(defn ycor-vect [vect] +(defn ycor-vect + [vect] (m/cdr vect)) -(defn add-vect [v1 v2] +(defn add-vect + [v1 v2] (make-vect (+ (xcor-vect v1) (xcor-vect v2)) (+ (ycor-vect v1) (ycor-vect v2)))) -(defn sub-vect [v1 v2] +(defn sub-vect + [v1 v2] (make-vect (- (xcor-vect v1) (xcor-vect v2)) (- (ycor-vect v1) (ycor-vect v2)))) -(defn scale-vect [s v] +(defn scale-vect + [s v] (make-vect (* s (xcor-vect v)) (* s (ycor-vect v)))) diff --git a/src/sicp/chapter_2/part_2/ex_2_47.clj b/src/sicp/chapter_2/part_2/ex_2_47.clj index fe7ba96..acec0a3 100644 --- a/src/sicp/chapter_2/part_2/ex_2_47.clj +++ b/src/sicp/chapter_2/part_2/ex_2_47.clj @@ -12,13 +12,26 @@ ; ; For each constructor supply the appropriate selectors to produce an implementation for frames. -(defn make-frame [origin edge1 edge2] +(defn make-frame + [origin edge1 edge2] (list origin edge1 edge2)) -(defn make-frame-2 [origin edge1 edge2] +(defn make-frame-2 + [origin edge1 edge2] (cons origin (cons edge1 edge2))) -(defn origin-frame [frame] (first frame)) -(defn edge1-frame [frame] (second frame)) -(defn edge2-frame [frame] (nth frame 2)) -(defn edge2-frame-2 [frame] (list (nth frame 2) (nth frame 3))) +(defn origin-frame + [frame] + (first frame)) + +(defn edge1-frame + [frame] + (second frame)) + +(defn edge2-frame + [frame] + (nth frame 2)) + +(defn edge2-frame-2 + [frame] + (list (nth frame 2) (nth frame 3))) diff --git a/src/sicp/chapter_2/part_2/ex_2_48.clj b/src/sicp/chapter_2/part_2/ex_2_48.clj index 0a947d1..6b91fc1 100644 --- a/src/sicp/chapter_2/part_2/ex_2_48.clj +++ b/src/sicp/chapter_2/part_2/ex_2_48.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-48 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.48 ; @@ -10,11 +11,14 @@ ; Use your vector representation from Exercise 2.46 to define a representation for segments ; with a constructor make-segment and selectors start-segment and end-segment. -(defn make-segment [x y] +(defn make-segment + [x y] (m/pair x y)) -(defn start-segment [vect] +(defn start-segment + [vect] (m/car vect)) -(defn end-segment [vect] +(defn end-segment + [vect] (m/cdr vect)) diff --git a/src/sicp/chapter_2/part_2/ex_2_49.clj b/src/sicp/chapter_2/part_2/ex_2_49.clj index 463d179..9ad8103 100644 --- a/src/sicp/chapter_2/part_2/ex_2_49.clj +++ b/src/sicp/chapter_2/part_2/ex_2_49.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-2.ex-2-49 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46] - [sicp.chapter-2.part-2.ex-2-48 :as ex-2-48])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46] + [sicp.chapter-2.part-2.ex-2-48 :as ex-2-48])) ; Exercise 2.49 ; @@ -12,17 +13,20 @@ ; 3. The painter that draws a diamond shape by connecting the midpoints of the sides of the frame. ; 4. The wave painter. -(defn outline-painter [] +(defn outline-painter + [] (b22/segments->painter [(ex-2-48/make-segment (ex-2-46/make-vect 0 0) (ex-2-46/make-vect 1 0)) (ex-2-48/make-segment (ex-2-46/make-vect 1 0) (ex-2-46/make-vect 1 1)) (ex-2-48/make-segment (ex-2-46/make-vect 1 1) (ex-2-46/make-vect 0 1)) (ex-2-48/make-segment (ex-2-46/make-vect 0 1) (ex-2-46/make-vect 0 0))])) -(defn x-painter [] +(defn x-painter + [] (b22/segments->painter [(ex-2-48/make-segment (ex-2-46/make-vect 0 0) (ex-2-46/make-vect 1 1)) (ex-2-48/make-segment (ex-2-46/make-vect 0 1) (ex-2-46/make-vect 1 0))])) -(defn diamond-painter [] +(defn diamond-painter + [] (b22/segments->painter [(ex-2-48/make-segment (ex-2-46/make-vect 0.5 0) (ex-2-46/make-vect 1 0.5)) (ex-2-48/make-segment (ex-2-46/make-vect 1 0.5) (ex-2-46/make-vect 0.5 1)) (ex-2-48/make-segment (ex-2-46/make-vect 0.5 1) (ex-2-46/make-vect 0 0.5)) diff --git a/src/sicp/chapter_2/part_2/ex_2_50.clj b/src/sicp/chapter_2/part_2/ex_2_50.clj index 08809d8..100acf6 100644 --- a/src/sicp/chapter_2/part_2/ex_2_50.clj +++ b/src/sicp/chapter_2/part_2/ex_2_50.clj @@ -1,25 +1,29 @@ (ns sicp.chapter-2.part-2.ex-2-50 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46])) ; Exercise 2.50 ; ; Define the transformation flip-horiz, which flips painters horizontally, ; and transformations that rotate painters counterclockwise by 180 degrees and 270 degrees. -(defn flip-horiz [painter] +(defn flip-horiz + [painter] (b22/transform-painter painter (ex-2-46/make-vect 1.0 0.0) (ex-2-46/make-vect 0.0 0.0) (ex-2-46/make-vect 1.0 1.0))) -(defn rotate-180-cc [painter] +(defn rotate-180-cc + [painter] (b22/transform-painter painter (ex-2-46/make-vect 1.0 1.0) (ex-2-46/make-vect 1.0 0.0) (ex-2-46/make-vect 0.0 1.0))) -(defn rotate-270-cc [painter] +(defn rotate-270-cc + [painter] (b22/transform-painter painter (ex-2-46/make-vect 0.0 1.0) (ex-2-46/make-vect 1.0 1.0) diff --git a/src/sicp/chapter_2/part_2/ex_2_51.clj b/src/sicp/chapter_2/part_2/ex_2_51.clj index cbac323..7cbbfcd 100644 --- a/src/sicp/chapter_2/part_2/ex_2_51.clj +++ b/src/sicp/chapter_2/part_2/ex_2_51.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-2.ex-2-51 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46] - [sicp.chapter-2.part-2.ex-2-50 :as ex-2-50])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46] + [sicp.chapter-2.part-2.ex-2-50 :as ex-2-50])) ; Exercise 2.51 ; @@ -11,7 +12,8 @@ ; writing a procedure that is analogous to the beside procedure given above, and again in terms ; of beside and suitable rotation operations (from Exercise 2.50). -(defn below [painter1 painter2] +(defn below + [painter1 painter2] (let [split-point (ex-2-46/make-vect 0.0 0.5) paint-bottom (b22/transform-painter painter1 (ex-2-46/make-vect 0.0 0.0) @@ -25,7 +27,8 @@ (paint-bottom frame) (paint-top frame)))) -(defn below-v2 [painter1 painter2] +(defn below-v2 + [painter1 painter2] (ex-2-50/rotate-270-cc (ex-2-50/rotate-180-cc (b22/beside (ex-2-50/rotate-270-cc painter1) (ex-2-50/rotate-270-cc painter2))))) diff --git a/src/sicp/chapter_2/part_2/ex_2_52.clj b/src/sicp/chapter_2/part_2/ex_2_52.clj index f46d5f9..51c3bcd 100644 --- a/src/sicp/chapter_2/part_2/ex_2_52.clj +++ b/src/sicp/chapter_2/part_2/ex_2_52.clj @@ -1,9 +1,10 @@ (ns sicp.chapter-2.part-2.ex-2-52 - (:require [sicp.chapter-2.part-2.book-2-2 :as b22] - [sicp.chapter-2.part-2.ex-2-44 :as ex-2-44] - [sicp.chapter-2.part-2.ex-2-45 :as ex-2-45] - [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46] - [sicp.chapter-2.part-2.ex-2-48 :as ex-2-48])) + (:require + [sicp.chapter-2.part-2.book-2-2 :as b22] + [sicp.chapter-2.part-2.ex-2-44 :as ex-2-44] + [sicp.chapter-2.part-2.ex-2-45 :as ex-2-45] + [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46] + [sicp.chapter-2.part-2.ex-2-48 :as ex-2-48])) ; Exercise 2.52 ; @@ -18,7 +19,8 @@ ; in a different pattern. (For example, you might make the big Mr. Rogers look ; outward from each corner of the square.) -(defn corner-split [painter n] +(defn corner-split + [painter n] (if (zero? n) painter (let [up (ex-2-44/up-split painter (dec n)) diff --git a/src/sicp/chapter_2/part_3/book_2_3.clj b/src/sicp/chapter_2/part_3/book_2_3.clj index 91d6bc1..a1b556a 100644 --- a/src/sicp/chapter_2/part_3/book_2_3.clj +++ b/src/sicp/chapter_2/part_3/book_2_3.clj @@ -1,52 +1,81 @@ (ns sicp.chapter-2.part-3.book-2-3) -(comment "2.3") -; Symbolic Data ------------------------------------------------------------------------------------ +(comment "2.3 Symbolic Data ----------------------------------------------------------------------") + +(comment "2.3.1 Quotation ------------------------------------------------------------------------") -(comment "2.3.1") -; Quotation ---------------------------------------------------------------------------------------- ; Exercises: ; * 2.53 ; * 2.54 ; * 2.55 -(defn memq [item x] +(defn memq + [item x] (cond (empty? x) false (= item (first x)) x :else (memq item (rest x)))) -(comment "2.3.2") -; Example: Symbolic Differentiation ---------------------------------------------------------------- +(comment "2.3.2 Example: Symbolic Differentiation ------------------------------------------------") + ; Exercises: ; * 2.56 ; * 2.57 ; * 2.58 -(defn =number? [exp num] (and (number? exp) (= exp num))) -(defn variable? [x] (symbol? x)) -(defn same-variable? [v1 v2] (and (variable? v1) (variable? v2) (= v1 v2))) +(defn =number? + [exp num] + (and (number? exp) (= exp num))) + +(defn variable? + [x] + (symbol? x)) + +(defn same-variable? + [v1 v2] + (and (variable? v1) (variable? v2) (= v1 v2))) + +(defn sum? + [x] + (and (sequential? x) (= (first x) '+))) -(defn sum? [x] (and (sequential? x) (= (first x) '+))) -(defn addend [s] (second s)) -(defn augend [s] (nth s 2)) -(defn make-sum [a1 a2] +(defn addend + [s] + (second s)) + +(defn augend + [s] + (nth s 2)) + +(defn make-sum + [a1 a2] (cond (= a1 0) a2 (= a2 0) a1 (and (number? a1) (number? a2)) (+ a1 a2) :else (list '+ a1 a2))) -(defn product? [x] (and (sequential? x) (= (first x) '*))) -(defn multiplier [p] (second p)) -(defn multiplicand [p] (nth p 2)) -(defn make-product [m1 m2] +(defn product? + [x] + (and (sequential? x) (= (first x) '*))) + +(defn multiplier + [p] + (second p)) + +(defn multiplicand + [p] + (nth p 2)) + +(defn make-product + [m1 m2] (cond (or (=number? m1 0) (=number? m2 0)) 0 (=number? m1 1) m2 (=number? m2 1) m1 (and (number? m1) (number? m2)) (* m1 m2) :else (list '* m1 m2))) -(defn deriv [exp var] +(defn deriv + [exp var] (cond (number? exp) 0 (variable? exp) (if (same-variable? exp var) 1 0) @@ -61,8 +90,8 @@ (multiplicand exp))) :else (throw (Exception. (str "unknown expression type: DERIV " exp))))) -(comment "2.3.3") -; Example: Representing Sets ----------------------------------------------------------------------- +(comment "2.3.3 Example: Representing Sets -------------------------------------------------------") + ; Exercises: ; * 2.59 ; * 2.60 @@ -73,18 +102,21 @@ ; * 2.65 ; * 2.66 -(defn element-of-set? [x set] +(defn element-of-set? + [x set] (cond (empty? set) false (= x (first set)) true :else (element-of-set? x (rest set)))) -(defn adjoin-set [x set] +(defn adjoin-set + [x set] (if (element-of-set? x set) set (cons x set))) -(defn intersection-set [set1 set2] +(defn intersection-set + [set1 set2] (cond (or (empty? set1) (empty? set2)) '() (element-of-set? (first set1) set2) @@ -92,13 +124,15 @@ :else (intersection-set (rest set1) set2))) ; Sorted list for optimization -(defn element-of-set-sorted? [x set] +(defn element-of-set-sorted? + [x set] (cond (empty? set) false (= x (first set)) true (< x (first set)) false :else (element-of-set-sorted? x (rest set)))) -(defn intersection-set-sorted [set1 set2] +(defn intersection-set-sorted + [set1 set2] (cond (or (empty? set1) (empty? set2)) '() :else (let [x1 (first set1) x2 (first set2)] (cond (= x1 x2) (cons x1 (intersection-set-sorted (rest set1) (rest set2))) @@ -106,27 +140,35 @@ :else (intersection-set-sorted set1 (rest set2)))))) ; List as tree -(defn entry [tree] (first tree)) +(defn entry + [tree] + (first tree)) -(defn left-branch [tree] +(defn left-branch + [tree] (if (< 1 (count tree)) (nth tree 1) '())) -(defn right-branch [tree] +(defn right-branch + [tree] (if (< 2 (count tree)) (nth tree 2) '())) -(defn make-tree [entry left right] (list entry left right)) +(defn make-tree + [entry left right] + (list entry left right)) -(defn element-of-set-tree? [x set] +(defn element-of-set-tree? + [x set] (cond (empty? set) false (= x (entry set)) true (< x (entry set)) (element-of-set-tree? x (left-branch set)) :else (element-of-set-tree? x (right-branch set)))) -(defn adjoin-set-tree [x set] +(defn adjoin-set-tree + [x set] (cond (empty? set) (make-tree x '() '()) (= x (entry set)) set (< x (entry set)) (make-tree (entry set) @@ -136,14 +178,15 @@ (left-branch set) (adjoin-set-tree x (right-branch set))))) -(defn lookup [given-key set-of-records] +(defn lookup + [given-key set-of-records] (cond (empty? set-of-records) false (= given-key (:key (first set-of-records))) (first set-of-records) :else (lookup given-key (rest set-of-records)))) -(comment "2.3.4") -; Example: Huffman Encoding Trees ------------------------------------------------------------------ +(comment "2.3.4 Example: Huffman Encoding Trees --------------------------------------------------") + ; Exercises: ; * 2.67 ; * 2.68 @@ -152,38 +195,61 @@ ; * 2.71 ; * 2.72 -(defn make-leaf [symbol weight] [:leaf symbol weight]) -(defn leaf? [object] +(defn make-leaf + [symbol weight] + [:leaf symbol weight]) + +(defn leaf? + [object] (and (or (list? object) (vector? object)) (= (first object) :leaf))) -(defn symbol-leaf [x] (second x)) -(defn weight-leaf [x] (nth x 2)) -(defn left-branch-h [tree] (first tree)) -(defn right-branch-h [tree] (second tree)) -(defn symbols [tree] + +(defn symbol-leaf + [x] + (second x)) + +(defn weight-leaf + [x] + (nth x 2)) + +(defn left-branch-h + [tree] + (first tree)) + +(defn right-branch-h + [tree] + (second tree)) + +(defn symbols + [tree] (if (leaf? tree) (list (symbol-leaf tree)) (nth tree 2))) -(defn weight [tree] +(defn weight + [tree] (if (leaf? tree) (weight-leaf tree) (nth tree 3))) -(defn make-code-tree [left right] +(defn make-code-tree + [left right] [left right (concat (symbols left) (symbols right)) (+ (weight left) (weight right))]) -(defn choose-branch [bit branch] +(defn choose-branch + [bit branch] (cond (= bit 0) (left-branch-h branch) (= bit 1) (right-branch-h branch) :else (throw (Exception. (str "bad bit: CHOOSE-BRANCH " bit))))) -(defn decode [bits tree] - (letfn [(decode-1 [bits current-branch] +(defn decode + [bits tree] + (letfn [(decode-1 + [bits current-branch] (if (empty? bits) '() (let [next-branch (choose-branch (first bits) current-branch)] @@ -193,14 +259,16 @@ (decode-1 (rest bits) next-branch)))))] (decode-1 bits tree))) -(defn adjoin-set-h [x set] +(defn adjoin-set-h + [x set] (cond (empty? set) (list x) (< (weight x) (weight (first set))) (cons x set) :else (cons (first set) (adjoin-set-h x (rest set))))) -(defn make-leaf-set [pairs] +(defn make-leaf-set + [pairs] (if (empty? pairs) '() (let [pair (first pairs)] @@ -230,13 +298,14 @@ (:B :D :C) 4] (:A :B :D :C) 8]) -(def huffman-message-decoded '(0 ; A - 1 1 0 ; D - 0 ; A - 1 0 ; B - 1 0 ; B - 1 1 1 ; C - 0)) ; A +(def huffman-message-decoded + '(0 ; A + 1 1 0 ; D + 0 ; A + 1 0 ; B + 1 0 ; B + 1 1 1 ; C + 0)) ; A (def huffman-message-encoded '(:A :D :A :B :B :C :A)) (def huffman-A '(0)) diff --git a/src/sicp/chapter_2/part_3/ex_2_53.clj b/src/sicp/chapter_2/part_3/ex_2_53.clj index e594ce3..3ec5143 100644 --- a/src/sicp/chapter_2/part_3/ex_2_53.clj +++ b/src/sicp/chapter_2/part_3/ex_2_53.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-3.ex-2-53 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23] + [sicp.misc :as m])) ; Exercise 2.53 ; @@ -14,7 +15,8 @@ ; (memq 'red '((red shoes) (blue socks))) ; (memq 'red '(red shoes blue socks)) -(defn print-vars [] +(defn print-vars + [] [(list 'a 'b 'c) ; (a b c) (list (list 'george)) ; ((george)) (m/cdr '((x1 x2) (y1 y2))) ; (y1 y2) diff --git a/src/sicp/chapter_2/part_3/ex_2_54.clj b/src/sicp/chapter_2/part_3/ex_2_54.clj index b6c83cb..61d1316 100644 --- a/src/sicp/chapter_2/part_3/ex_2_54.clj +++ b/src/sicp/chapter_2/part_3/ex_2_54.clj @@ -19,7 +19,8 @@ ; symbols are eq?, or if they are both lists such that (car a) is equal? to (car b) and (cdr a) ; is equal? to (cdr b). Using this idea, implement equal? as a procedure. -(defn equal? [a b] +(defn equal? + [a b] (cond (and (symbol? a) (symbol? b)) (= a b) (and (list? a) (list? b)) diff --git a/src/sicp/chapter_2/part_3/ex_2_55.clj b/src/sicp/chapter_2/part_3/ex_2_55.clj index 3fd614b..8d3a7bd 100644 --- a/src/sicp/chapter_2/part_3/ex_2_55.clj +++ b/src/sicp/chapter_2/part_3/ex_2_55.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-3.ex-2-55 - (:require [sicp.misc :as m])) + (:require + [sicp.misc :as m])) ; Exercise 2.55 ; @@ -13,5 +14,6 @@ (m/car (quote (quote abasdfa))) ; quote (m/car '(quote abasdfa)) ; quote -(defn print-test [] +(defn print-test + [] (m/car ''abracadabra)) diff --git a/src/sicp/chapter_2/part_3/ex_2_56.clj b/src/sicp/chapter_2/part_3/ex_2_56.clj index e649edf..7507f23 100644 --- a/src/sicp/chapter_2/part_3/ex_2_56.clj +++ b/src/sicp/chapter_2/part_3/ex_2_56.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-3.ex-2-56 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.56 ; @@ -12,17 +13,28 @@ ; Build in the rules that anything raised to the power 0 is 1 and anything raised to ; the power 1 is the thing itself. -(defn exponentiation? [x] (and (sequential? x) (= (first x) '**))) -(defn base [s] (second s)) -(defn exponent [s] (nth s 2)) -(defn make-exponentiation [base exponent] +(defn exponentiation? + [x] + (and (sequential? x) (= (first x) '**))) + +(defn base + [s] + (second s)) + +(defn exponent + [s] + (nth s 2)) + +(defn make-exponentiation + [base exponent] (cond (= exponent 0) 1.0 (and (= exponent 1) (number? base)) (float base) (and (number? base) (number? exponent)) (Math/pow base exponent) :else (list '** base exponent))) -(defn deriv [exp var] +(defn deriv + [exp var] (cond (number? exp) 0 (b23/variable? exp) (if (b23/same-variable? exp var) 1 0) @@ -30,7 +42,7 @@ (deriv (b23/augend exp) var)) (exponentiation? exp) (b23/make-product (b23/make-product (exponent exp) - (make-exponentiation (base exp) (dec (exponent exp)))) + (make-exponentiation (base exp) (dec (exponent exp)))) (deriv (base exp) var)) (b23/product? exp) (b23/make-sum (b23/make-product diff --git a/src/sicp/chapter_2/part_3/ex_2_57.clj b/src/sicp/chapter_2/part_3/ex_2_57.clj index b70c9c2..a488403 100644 --- a/src/sicp/chapter_2/part_3/ex_2_57.clj +++ b/src/sicp/chapter_2/part_3/ex_2_57.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-3.ex-2-57 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.57 ; @@ -14,27 +15,34 @@ ; For example, the addend of a sum would be the first term, and the augend would be the sum ; of the rest of the terms. -(defn augend [s] (reduce + (drop 2 s))) -(defn make-sum [& args] +(defn augend + [s] + (reduce + (drop 2 s))) + +(defn make-sum + [& args] (cond (empty? args) 0 (= (count args) 1) (first args) (every? number? args) (reduce + args) :else (cons '+ args))) -(defn multiplicand [s] +(defn multiplicand + [s] (let [last-items (drop 2 s)] (cond (every? number? last-items) (reduce * last-items) :else (cons '* last-items)))) -(defn make-product [& args] +(defn make-product + [& args] (cond (empty? args) 0 (= (count args) 1) (first args) (every? number? args) (reduce * args) :else (cons '* args))) -(defn deriv [exp var] +(defn deriv + [exp var] (cond (number? exp) 0 (b23/variable? exp) (if (b23/same-variable? exp var) 1 0) diff --git a/src/sicp/chapter_2/part_3/ex_2_58.clj b/src/sicp/chapter_2/part_3/ex_2_58.clj index 2cb3088..92c9aa9 100644 --- a/src/sicp/chapter_2/part_3/ex_2_58.clj +++ b/src/sicp/chapter_2/part_3/ex_2_58.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-3.ex-2-58 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.58 ; @@ -19,35 +20,52 @@ ; is done before addition. Can you design appropriate predicates, selectors, and constructors ; for this notation such that our derivative program still works? -(defn sum? [x] (and (sequential? x) (= (second x) '+))) -(defn addend [s] (first s)) -(defn augend [e] +(defn sum? + [x] + (and (sequential? x) (= (second x) '+))) + +(defn addend + [s] + (first s)) + +(defn augend + [e] (if (not (nil? (nth e 3 nil))) (drop 2 e) (nth e 2))) -(defn make-sum [a1 a2] +(defn make-sum + [a1 a2] (cond (= a1 0) a2 (= a2 0) a1 (and (number? a1) (number? a2)) (+ a1 a2) :else (list a1 '+ a2))) -(defn product? [x] (and (sequential? x) (= (second x) '*))) -(defn multiplier [p] (first p)) -(defn multiplicand [e] +(defn product? + [x] + (and (sequential? x) (= (second x) '*))) + +(defn multiplier + [p] + (first p)) + +(defn multiplicand + [e] (if (not (nil? (nth e 3 nil))) (drop 2 e) (nth e 2))) -(defn make-product [m1 m2] +(defn make-product + [m1 m2] (cond (or (b23/=number? m1 0) (b23/=number? m2 0)) 0 (b23/=number? m1 1) m2 (b23/=number? m2 1) m1 (and (number? m1) (number? m2)) (* m1 m2) :else (list m1 '* m2))) -(defn deriv [exp var] +(defn deriv + [exp var] (cond (number? exp) 0 (b23/variable? exp) (if (b23/same-variable? exp var) 1 0) diff --git a/src/sicp/chapter_2/part_3/ex_2_59.clj b/src/sicp/chapter_2/part_3/ex_2_59.clj index f0528e8..d56337d 100644 --- a/src/sicp/chapter_2/part_3/ex_2_59.clj +++ b/src/sicp/chapter_2/part_3/ex_2_59.clj @@ -1,11 +1,13 @@ (ns sicp.chapter-2.part-3.ex-2-59 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.59 ; ; Implement the union-set operation for the unordered-list representation of sets. -(defn union-set [set1 set2] +(defn union-set + [set1 set2] (cond (empty? set1) set2 (empty? set2) set1 diff --git a/src/sicp/chapter_2/part_3/ex_2_60.clj b/src/sicp/chapter_2/part_3/ex_2_60.clj index 89d92bc..36f5a65 100644 --- a/src/sicp/chapter_2/part_3/ex_2_60.clj +++ b/src/sicp/chapter_2/part_3/ex_2_60.clj @@ -14,18 +14,21 @@ ; you would use this representation in preference to the non-duplicate one?) ; No change to element-of-set? -(defn element-of-set? [x set] +(defn element-of-set? + [x set] (cond (empty? set) false (= x (first set)) true :else (element-of-set? x (rest set)))) ; No checks for duplicates in adjoin-set. It has complexity O(n) -(defn adjoin-set [x set] +(defn adjoin-set + [x set] (cons x set)) ; No checks for duplicates in intersection-set. No visible reasons -(defn intersection-set [set1 set2] +(defn intersection-set + [set1 set2] (cond (or (empty? set1) (empty? set2)) '() (element-of-set? (first set1) set2) @@ -34,5 +37,6 @@ ; No checks for duplicates in union-set ; Complexity is O(n) instede of O(n^2) -(defn union-set [set1 set2] +(defn union-set + [set1 set2] (concat set1 set2)) diff --git a/src/sicp/chapter_2/part_3/ex_2_61.clj b/src/sicp/chapter_2/part_3/ex_2_61.clj index 126b4a9..c93ff93 100644 --- a/src/sicp/chapter_2/part_3/ex_2_61.clj +++ b/src/sicp/chapter_2/part_3/ex_2_61.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-3.ex-2-61 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.61 ; @@ -7,7 +8,8 @@ ; By analogy with element-of-set? show how to take advantage of the ordering to produce ; a procedure that requires on the average about half as many steps as with the unordered representation. -(defn adjoin-set-sorted [x set] +(defn adjoin-set-sorted + [x set] (if (b23/element-of-set-sorted? x set) set (cons x set))) diff --git a/src/sicp/chapter_2/part_3/ex_2_62.clj b/src/sicp/chapter_2/part_3/ex_2_62.clj index 836c2eb..1641a6a 100644 --- a/src/sicp/chapter_2/part_3/ex_2_62.clj +++ b/src/sicp/chapter_2/part_3/ex_2_62.clj @@ -4,7 +4,8 @@ ; ; Give a O(n) implementation of union-set for sets represented as ordered lists. -(defn union-set-sorted [set1 set2] +(defn union-set-sorted + [set1 set2] (cond (empty? set1) set2 (empty? set2) set1 :else (let [x1 (first set1) x2 (first set2)] diff --git a/src/sicp/chapter_2/part_3/ex_2_63.clj b/src/sicp/chapter_2/part_3/ex_2_63.clj index b404cd9..436a7c4 100644 --- a/src/sicp/chapter_2/part_3/ex_2_63.clj +++ b/src/sicp/chapter_2/part_3/ex_2_63.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-3.ex-2-63 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.63 ; @@ -27,7 +28,8 @@ ; result-list))))) ; (copy-to-list tree '())) -(defn tree->list-1 [tree] +(defn tree->list-1 + [tree] (if (empty? tree) '() (concat @@ -35,8 +37,10 @@ (cons (b23/entry tree) (tree->list-1 (b23/right-branch tree)))))) -(defn tree->list-2 [tree] - (letfn [(copy-to-list [tree result-list] +(defn tree->list-2 + [tree] + (letfn [(copy-to-list + [tree result-list] (if (empty? tree) result-list (copy-to-list diff --git a/src/sicp/chapter_2/part_3/ex_2_64.clj b/src/sicp/chapter_2/part_3/ex_2_64.clj index d140ab7..56ea9ad 100644 --- a/src/sicp/chapter_2/part_3/ex_2_64.clj +++ b/src/sicp/chapter_2/part_3/ex_2_64.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-3.ex-2-64 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.64 ; @@ -42,7 +43,8 @@ ; right-tree) ; remaining-elts)))))))) -(defn partial-tree [elts n] +(defn partial-tree + [elts n] (if (= n 0) (list '() elts) (let [left-size (quot (- n 1) 2) @@ -56,7 +58,8 @@ remaining-elts (second right-result)] (list (b23/make-tree this-entry left-tree right-tree) remaining-elts)))) -(defn list->tree [elements] +(defn list->tree + [elements] (first (partial-tree elements (count elements)))) ; 1. Write a short paragraph explaining as clearly as you can how partial-tree works. diff --git a/src/sicp/chapter_2/part_3/ex_2_65.clj b/src/sicp/chapter_2/part_3/ex_2_65.clj index ffce292..1e131a1 100644 --- a/src/sicp/chapter_2/part_3/ex_2_65.clj +++ b/src/sicp/chapter_2/part_3/ex_2_65.clj @@ -1,12 +1,14 @@ (ns sicp.chapter-2.part-3.ex-2-65 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.65 ; ; Use the results of Exercise 2.63 and Exercise 2.64 to give Θ(n) ; implementations of union-set and intersection-set for sets implemented as (balanced) binary trees. -(defn union-set-tree [tree1 tree2] +(defn union-set-tree + [tree1 tree2] (cond (empty? tree1) tree2 (empty? tree2) tree1 @@ -27,7 +29,8 @@ (union-set-tree tree1-left tree2) tree1-right))))) -(defn intersection-set-tree [tree1 tree2] +(defn intersection-set-tree + [tree1 tree2] (cond (empty? tree1) '() (empty? tree2) '() diff --git a/src/sicp/chapter_2/part_3/ex_2_66.clj b/src/sicp/chapter_2/part_3/ex_2_66.clj index bd9fb75..93cf717 100644 --- a/src/sicp/chapter_2/part_3/ex_2_66.clj +++ b/src/sicp/chapter_2/part_3/ex_2_66.clj @@ -1,12 +1,14 @@ (ns sicp.chapter-2.part-3.ex-2-66 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.66 ; ; Implement the lookup procedure for the case where the set of records is structured as ; a binary tree, ordered by the numerical values of the keys. -(defn lookup-tree [given-key set-of-records] +(defn lookup-tree + [given-key set-of-records] (if (empty? set-of-records) false (let [parent (b23/entry set-of-records)] diff --git a/src/sicp/chapter_2/part_3/ex_2_67.clj b/src/sicp/chapter_2/part_3/ex_2_67.clj index 3f576b8..58ceb39 100644 --- a/src/sicp/chapter_2/part_3/ex_2_67.clj +++ b/src/sicp/chapter_2/part_3/ex_2_67.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-3.ex-2-67 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.67 ; @@ -17,5 +18,6 @@ ; (define sample-message ; '(0 1 1 0 0 1 0 1 0 1 1 1 0)) -(defn decode-message [message tree] +(defn decode-message + [message tree] (b23/decode message tree)) diff --git a/src/sicp/chapter_2/part_3/ex_2_68.clj b/src/sicp/chapter_2/part_3/ex_2_68.clj index 833bc25..89799c6 100644 --- a/src/sicp/chapter_2/part_3/ex_2_68.clj +++ b/src/sicp/chapter_2/part_3/ex_2_68.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-3.ex-2-68 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.68 ; @@ -20,7 +21,8 @@ ; you obtained in Exercise 2.67 with the sample tree and seeing whether it is the same as the ; original sample message. -(defn encode-symbol [symbol tree] +(defn encode-symbol + [symbol tree] (cond (empty? tree) '() (b23/leaf? tree) '() (= symbol (b23/symbols tree)) '() @@ -33,7 +35,8 @@ (cons 1 (encode-symbol symbol right-branch))) (throw (Exception. (str "Symbol not found in tree " symbol))))))) -(defn encode [message tree] +(defn encode + [message tree] (if (empty? message) '() (concat diff --git a/src/sicp/chapter_2/part_3/ex_2_69.clj b/src/sicp/chapter_2/part_3/ex_2_69.clj index 21e905f..0a8dae3 100644 --- a/src/sicp/chapter_2/part_3/ex_2_69.clj +++ b/src/sicp/chapter_2/part_3/ex_2_69.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-3.ex-2-69 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.69 ; @@ -21,7 +22,8 @@ ; ; You can take significant advantage of the fact that we are using an ordered set representation.) -(defn successive-merge [leaf-set] +(defn successive-merge + [leaf-set] (if (= (count leaf-set) 1) (first leaf-set) (let [first (first leaf-set) @@ -31,5 +33,6 @@ (b23/make-code-tree first second) rest))))) -(defn generate-huffman-tree [pairs] +(defn generate-huffman-tree + [pairs] (successive-merge (b23/make-leaf-set pairs))) diff --git a/src/sicp/chapter_2/part_3/ex_2_70.clj b/src/sicp/chapter_2/part_3/ex_2_70.clj index 97d213a..105d12b 100644 --- a/src/sicp/chapter_2/part_3/ex_2_70.clj +++ b/src/sicp/chapter_2/part_3/ex_2_70.clj @@ -27,15 +27,17 @@ ; How many bits are required for the encoding? What is the smallest number of bits that would be ; needed to encode this song if we used a fixed-length code for the eight-symbol alphabet? -(def abc '((:a 2) (:na 16) +(def abc + '((:a 2) (:na 16) (:boom 1) (:Sha 3) (:Get 2) (:yip 9) (:job 2) (:Wah 1))) -(def song '(:Get :a :job - :Sha :na :na :na :na :na :na :na :na - :Get :a :job - :Sha :na :na :na :na :na :na :na :na - :Wah :yip :yip :yip :yip - :yip :yip :yip :yip :yip - :Sha :boom)) +(def song + '(:Get :a :job + :Sha :na :na :na :na :na :na :na :na + :Get :a :job + :Sha :na :na :na :na :na :na :na :na + :Wah :yip :yip :yip :yip + :yip :yip :yip :yip :yip + :Sha :boom)) diff --git a/src/sicp/chapter_2/part_4/book_2_4.clj b/src/sicp/chapter_2/part_4/book_2_4.clj index 672b430..19ab9b6 100644 --- a/src/sicp/chapter_2/part_4/book_2_4.clj +++ b/src/sicp/chapter_2/part_4/book_2_4.clj @@ -1,17 +1,26 @@ (ns sicp.chapter-2.part-4.book-2-4) -(comment "2.4") -; Multiple Representations for Abstract Data ------------------------------------------------------- +(comment "2.4 Multiple Representations for Abstract Data -----------------------------------------") -(comment "2.4.1") -; Representations for Complex Numbers -------------------------------------------------------------- +(comment "2.4.1 Representations for Complex Numbers ----------------------------------------------") ; Ben's way -(defn real-part-Ben [z] (first z)) -(defn imag-part-Ben [z] (second z)) -(defn magnitude-Ben [z] (Math/sqrt (+ (Math/pow (real-part-Ben z) 2) - (Math/pow (imag-part-Ben z) 2)))) -(defn angle-v1 [z] (Math/atan2 (imag-part-Ben z) (real-part-Ben z))) +(defn real-part-Ben + [z] + (first z)) + +(defn imag-part-Ben + [z] + (second z)) + +(defn magnitude-Ben + [z] + (Math/sqrt (+ (Math/pow (real-part-Ben z) 2) + (Math/pow (imag-part-Ben z) 2)))) + +(defn angle-v1 + [z] + (Math/atan2 (imag-part-Ben z) (real-part-Ben z))) (defn make-from-real-imag-Ben ([x y] [x y]) @@ -21,139 +30,192 @@ ([r a] [(* r (Math/cos a)) (* r (Math/sin a))]) ([z] [(magnitude-Ben z) (angle-v1 z)])) -(defn add-complex-Ben [z1 z2] +(defn add-complex-Ben + [z1 z2] (make-from-real-imag-Ben (+ (real-part-Ben z1) (real-part-Ben z2)) (+ (imag-part-Ben z1) (imag-part-Ben z2)))) -(defn sub-complex-Ben [z1 z2] +(defn sub-complex-Ben + [z1 z2] (make-from-real-imag-Ben (- (real-part-Ben z1) (real-part-Ben z2)) (- (imag-part-Ben z1) (imag-part-Ben z2)))) -(defn mul-complex-Ben [z1 z2] +(defn mul-complex-Ben + [z1 z2] (make-from-mag-ang-Ben (* (magnitude-Ben z1) (magnitude-Ben z2)) (+ (angle-v1 z1) (angle-v1 z2)))) -(defn div-complex-Ben [z1 z2] +(defn div-complex-Ben + [z1 z2] (make-from-mag-ang-Ben (/ (magnitude-Ben z1) (magnitude-Ben z2)) (- (angle-v1 z1) (angle-v1 z2)))) ; Alyssa's way -(defn magnitude-Alyssa [z] (first z)) -(defn angle-Alyssa [z] (second z)) -(defn real-part-Alyssa [z] (* (magnitude-Alyssa z) (Math/cos (angle-Alyssa z)))) -(defn imag-part-Alyssa [z] (* (magnitude-Alyssa z) (Math/sin (angle-Alyssa z)))) -(defn make-from-real-imag-Alyssa [x y] [(Math/sqrt (+ (Math/pow x 2) (Math/pow y 2))) - (Math/atan2 y x)]) -(defn make-from-mag-ang-Alyssa [r a] [r a]) +(defn magnitude-Alyssa + [z] + (first z)) + +(defn angle-Alyssa + [z] + (second z)) + +(defn real-part-Alyssa + [z] + (* (magnitude-Alyssa z) (Math/cos (angle-Alyssa z)))) + +(defn imag-part-Alyssa + [z] + (* (magnitude-Alyssa z) (Math/sin (angle-Alyssa z)))) + +(defn make-from-real-imag-Alyssa + [x y] + [(Math/sqrt (+ (Math/pow x 2) (Math/pow y 2))) + (Math/atan2 y x)]) + +(defn make-from-mag-ang-Alyssa + [r a] + [r a]) (comment "2.4.2") + ; Tagged data -------------------------------------------------------------------------------------- ; Types/tags of data -(defn attach-tag [type-tag contents] +(defn attach-tag + [type-tag contents] (cons type-tag contents)) -(defn type-tag [datum] +(defn type-tag + [datum] (if (sequential? datum) (first datum) (throw (Exception. (str "Bad tagged datum: TYPE-TAG " datum))))) -(defn contents [datum] +(defn contents + [datum] (if (sequential? datum) (rest datum) (throw (Exception. (str "Bad tagged datum: CONTENTS " datum))))) -(defn rectangular? [z] +(defn rectangular? + [z] (= (type-tag z) :rectangular)) -(defn polar? [z] +(defn polar? + [z] (= (type-tag z) :polar)) ; Rectangular -(defn real-part-rectangular [z] (first z)) -(defn imag-part-rectangular [z] (second z)) +(defn real-part-rectangular + [z] + (first z)) + +(defn imag-part-rectangular + [z] + (second z)) -(defn magnitude-rectangular [z] +(defn magnitude-rectangular + [z] (Math/sqrt (+ (Math/pow (real-part-rectangular z) 2) (Math/pow (imag-part-rectangular z) 2)))) -(defn angle-rectangular [z] +(defn angle-rectangular + [z] (Math/atan2 (imag-part-rectangular z) (real-part-rectangular z))) -(defn make-from-real-imag-rectangular [x y] +(defn make-from-real-imag-rectangular + [x y] (attach-tag :rectangular [x y])) -(defn make-from-mag-ang-rectangular [r a] +(defn make-from-mag-ang-rectangular + [r a] (attach-tag :rectangular [(* r (Math/cos a)) (* r (Math/sin a))])) ; Polar -(defn magnitude-polar [z] (first z)) -(defn angle-polar [z] (second z)) +(defn magnitude-polar + [z] + (first z)) -(defn real-part-polar [z] +(defn angle-polar + [z] + (second z)) + +(defn real-part-polar + [z] (* (magnitude-polar z) (Math/cos (angle-polar z)))) -(defn imag-part-polar [z] +(defn imag-part-polar + [z] (* (magnitude-polar z) (Math/sin (angle-polar z)))) -(defn make-from-real-imag-polar [x y] +(defn make-from-real-imag-polar + [x y] (attach-tag :polar [(Math/sqrt (+ (Math/pow x 2) (Math/pow y 2))) (Math/atan2 y x)])) -(defn make-from-mag-ang-polar [r a] +(defn make-from-mag-ang-polar + [r a] (attach-tag :polar [r a])) ; Compounding of Polar and Rectangular ways -(defn real-part [z] +(defn real-part + [z] (cond (rectangular? z) (real-part-rectangular (contents z)) (polar? z) (real-part-polar (contents z)) :else (throw (Exception. (str "Unknown type: REAL-PART " z))))) -(defn imag-part [z] +(defn imag-part + [z] (cond (rectangular? z) (imag-part-rectangular (contents z)) (polar? z) (imag-part-polar (contents z)) :else (throw (Exception. (str "Unknown type: IMAG-PART " z))))) -(defn magnitude [z] +(defn magnitude + [z] (cond (rectangular? z) (magnitude-rectangular (contents z)) (polar? z) (magnitude-polar (contents z)) :else (throw (Exception. (str "Unknown type: MAGNITUDE " z))))) -(defn angle [z] +(defn angle + [z] (cond (rectangular? z) (angle-rectangular (contents z)) (polar? z) (angle-polar (contents z)) :else (throw (Exception. (str "Unknown type: ANGLE " z))))) -(defn make-from-real-imag-v2 [x y] +(defn make-from-real-imag-v2 + [x y] (make-from-real-imag-rectangular x y)) -(defn add-complex [z1 z2] +(defn add-complex + [z1 z2] (make-from-real-imag-v2 (+ (real-part z1) (real-part z2)) (+ (imag-part z1) (imag-part z2)))) -(defn make-from-mag-ang-v2 [r a] +(defn make-from-mag-ang-v2 + [r a] (make-from-mag-ang-polar r a)) -(comment "2.4.3") -; Data-Directed Programming and Additivity --------------------------------------------------------- +(comment "2.4.3 Data-Directed Programming and Additivity -----------------------------------------") + ; Exercises: ; * 2.73 ; * 2.74 ; * 2.75 ; * 2.76 -(defn apply-generic [op & args] +(defn apply-generic + [op & args] (let [type-tags (map type-tag args) proc (get op type-tags)] (if proc @@ -161,7 +223,8 @@ (throw (Exception. (str "No method for these types: APPLY-GENERIC " (list op type-tags))))))) -(defn make-from-real-imag [x y] +(defn make-from-real-imag + [x y] (fn [op] (cond (= op :real-part) x @@ -170,5 +233,6 @@ (= op :angle) (Math/atan2 y x) :else (throw (Exception. (str "Unknown op: MAKE-FROM-REAL-IMAG " op)))))) -(defn apply-generic-v2 [op arg] (arg op)) - +(defn apply-generic-v2 + [op arg] + (arg op)) diff --git a/src/sicp/chapter_2/part_4/ex_2_73.clj b/src/sicp/chapter_2/part_4/ex_2_73.clj index f4b427b..ded4a07 100644 --- a/src/sicp/chapter_2/part_4/ex_2_73.clj +++ b/src/sicp/chapter_2/part_4/ex_2_73.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-4.ex-2-73 - (:require [sicp.chapter-2.part-3.book-2-3 :as b23])) + (:require + [sicp.chapter-2.part-3.book-2-3 :as b23])) ; Exercise 2.73 ; @@ -58,7 +59,8 @@ ; ((get (operator exp) 'deriv) ; (operands exp) var) -(defn deriv [exp var] +(defn deriv + [exp var] (cond (number? exp) 0 (b23/variable? exp) (if (b23/same-variable? exp var) 1 0) @@ -72,10 +74,16 @@ ; ⟨more rules can be added here⟩ :else (throw (Exception. (str "unknown expression type: DERIV " exp))))) -(defn operator [exp] (first exp)) -(defn operands [exp] (rest exp)) +(defn operator + [exp] + (first exp)) -(defn deriv-v2 [exp var] +(defn operands + [exp] + (rest exp)) + +(defn deriv-v2 + [exp var] (cond (number? exp) 0 (b23/variable? exp) (if (b23/same-variable? exp var) 1 0) @@ -88,29 +96,36 @@ ; Sorry, I'm lazy and took examples of code here and rewrite it to Clojure ; https://github.com/ivanjovanovic/sicp/blob/master/2.4/e-2.73.scm -(defn put [param1 param2 deriver] ; Just for linter +(defn put + [param1 param2 deriver] + ; Just for linter (println param1 param2 deriver)) (defn make-sum ([a b] (list '+ a b)) ([a b c] (list '+ a b c))) -(defn make-product [m1 m2] +(defn make-product + [m1 m2] (list '* m1 m2)) -(defn install-sum-derivation [] +(defn install-sum-derivation + [] (letfn [(addend [operands] (first operands)) (augend [operands] (second operands)) - (derive-sum [operands var] + (derive-sum + [operands var] (make-sum (deriv (addend operands) var) (deriv (augend operands) var)))] ; and methods for putting the thing in the table (put '+ 'deriv derive-sum))) -(defn install-product-derivation [] +(defn install-product-derivation + [] (letfn [(multiplier [operands] (first operands)) (multiplicand [operands] (second operands)) - (derive-product [operands var] + (derive-product + [operands var] (make-sum (make-product (multiplier operands) (deriv (multiplicand operands) var)) @@ -120,11 +135,13 @@ ; put that into table (put '* 'deriv derive-product))) -(defn install-exponent-derivation [] +(defn install-exponent-derivation + [] (letfn [(power [operands] (second operands)) (base [operands] (first operands)) (make-exponent [b p] (list '** b p)) - (derive-exponent [operands var] + (derive-exponent + [operands var] (make-product (make-product (power operands) (make-exponent (base operands) (dec (power operands)))) diff --git a/src/sicp/chapter_2/part_4/ex_2_74.clj b/src/sicp/chapter_2/part_4/ex_2_74.clj index 38f4f5b..551ea44 100644 --- a/src/sicp/chapter_2/part_4/ex_2_74.clj +++ b/src/sicp/chapter_2/part_4/ex_2_74.clj @@ -26,16 +26,20 @@ ; Explain how the individual divisions’ files should be structured. ; In particular, what type information must be supplied? -(defn make-generic-employee-file [division employee-file] +(defn make-generic-employee-file + [division employee-file] (cons division employee-file)) -(defn division [generic-employee-file] +(defn division + [generic-employee-file] (first generic-employee-file)) -(defn employee-file [generic-employee-file] +(defn employee-file + [generic-employee-file] (rest generic-employee-file)) -(defn get-record [employee-name generic-employee-file] +(defn get-record + [employee-name generic-employee-file] ((get (division generic-employee-file) :get-record) employee-name (employee-file generic-employee-file))) @@ -44,10 +48,12 @@ ; given employee’s record from any division’s personnel file. How should the record be structured ; in order to make this operation work? -(defn employee-record [generic-employee-record] +(defn employee-record + [generic-employee-record] (println "Debug" generic-employee-record)) -(defn get-salary [generic-employee-record] +(defn get-salary + [generic-employee-record] ((get (division generic-employee-record) :get-salary) (employee-record generic-employee-record))) @@ -55,7 +61,8 @@ ; divisions’ files for the record of a given employee and return the record. Assume that this ; procedure takes as arguments an employee’s name and a list of all the divisions’ files. -(defn find-employee-record [employee-name generic-files] +(defn find-employee-record + [employee-name generic-files] (if (empty? generic-files) (throw (Exception. "Record not found")) (let [record (get-record employee-name (first generic-files))] diff --git a/src/sicp/chapter_2/part_4/ex_2_75.clj b/src/sicp/chapter_2/part_4/ex_2_75.clj index ca678dc..d116e99 100644 --- a/src/sicp/chapter_2/part_4/ex_2_75.clj +++ b/src/sicp/chapter_2/part_4/ex_2_75.clj @@ -5,7 +5,8 @@ ; Implement the constructor make-from-mag-ang in message-passing style. ; This procedure should be analogous to the make-from-real-imag procedure given above. -(defn make-from-mag-ang [r a] +(defn make-from-mag-ang + [r a] (fn [op] (cond (= op :real-part) (* r (Math/cos a)) diff --git a/src/sicp/chapter_2/part_5/book_2_5.clj b/src/sicp/chapter_2/part_5/book_2_5.clj index 7dcc915..4dfbcd5 100644 --- a/src/sicp/chapter_2/part_5/book_2_5.clj +++ b/src/sicp/chapter_2/part_5/book_2_5.clj @@ -1,27 +1,30 @@ (ns sicp.chapter-2.part-5.book-2-5 - (:require [sicp.chapter-2.part-4.book-2-4 :as b24] - [sicp.misc :as m])) + (:require + [sicp.chapter-2.part-4.book-2-4 :as b24] + [sicp.misc :as m])) -(comment "2.5") -; Systems with Generic Operations ------------------------------------------------------------------ +(comment "2.5 Systems with Generic Operations ----------------------------------------------------") -(comment "2.5.1") -; Generic Arithmetic Operations -------------------------------------------------------------------- +(comment "2.5.1 Generic Arithmetic Operations ----------------------------------------------------") ; Custom helper functions. Made myself, just for testing ------------------------------------------- (def operation-table (atom {})) -(defn attach-tag [tag value] +(defn attach-tag + [tag value] {:tag tag, :contents value}) -(defn put-op [operation types procedure] +(defn put-op + [operation types procedure] (swap! operation-table assoc-in [operation types] procedure)) -(defn get-op [operation types] +(defn get-op + [operation types] (get-in @operation-table [operation types])) -(defn apply-generic [op & args] +(defn apply-generic + [op & args] (let [type-tags (map #(-> % :tag) args) operation (get-op op type-tags)] (if operation @@ -30,12 +33,24 @@ ; -------------------------------------------------------------------------------------------------- -(defn add [x y] (apply-generic :add x y)) -(defn sub [x y] (apply-generic :sub x y)) -(defn mul [x y] (apply-generic :mul x y)) -(defn div [x y] (apply-generic :div x y)) +(defn add + [x y] + (apply-generic :add x y)) -(defn install-scheme-number-package [] +(defn sub + [x y] + (apply-generic :sub x y)) + +(defn mul + [x y] + (apply-generic :mul x y)) + +(defn div + [x y] + (apply-generic :div x y)) + +(defn install-scheme-number-package + [] (letfn [(tag [x] (attach-tag :scheme-number x))] (put-op :add [:scheme-number :scheme-number] (fn [x y] (tag (+ x y)))) (put-op :sub [:scheme-number :scheme-number] (fn [x y] (tag (- x y)))) @@ -44,22 +59,33 @@ (put-op :make :scheme-number (fn [x] (tag x))) :done)) -(defn install-rational-package [] +(defn install-rational-package + [] ; Internal procedures (letfn [(numer [x] (first x)) (denom [x] (second x)) - (make-rat [n d] (let [g (m/gcd n d)] - (m/pair (/ n g) (/ d g)))) - (add-rat [x y] (make-rat (+ (* (numer x) (denom y)) - (* (numer y) (denom x))) - (* (denom x) (denom y)))) - (sub-rat [x y] (make-rat (- (* (numer x) (denom y)) - (* (numer y) (denom x))) - (* (denom x) (denom y)))) - (mul-rat [x y] (make-rat (* (numer x) (numer y)) - (* (denom x) (denom y)))) - (div-rat [x y] (make-rat (* (numer x) (denom y)) - (* (denom x) (numer y)))) + (make-rat + [n d] + (let [g (m/gcd n d)] + (m/pair (/ n g) (/ d g)))) + (add-rat + [x y] + (make-rat (+ (* (numer x) (denom y)) + (* (numer y) (denom x))) + (* (denom x) (denom y)))) + (sub-rat + [x y] + (make-rat (- (* (numer x) (denom y)) + (* (numer y) (denom x))) + (* (denom x) (denom y)))) + (mul-rat + [x y] + (make-rat (* (numer x) (numer y)) + (* (denom x) (denom y)))) + (div-rat + [x y] + (make-rat (* (numer x) (denom y)) + (* (denom x) (numer y)))) ; Interface to rest of the system (tag [x] (attach-tag :rational x))] (put-op :add [:rational :rational] (fn [x y] (tag (add-rat x y)))) @@ -69,23 +95,32 @@ (put-op :make :rational (fn [n d] (tag (make-rat n d)))) :done)) -(defn install-complex-package [] +(defn install-complex-package + [] ; Imported procedures from rectangular and polar packages (letfn [(make-from-real-imag [x y] ((get-op :make-from-real-imag :rectangular) x y)) (make-from-mag-ang [r a] ((get-op :make-from-mag-ang :polar) r a)) ; Internal procedures - (add-complex [z1 z2] (make-from-real-imag - (+ (b24/real-part z1) (b24/real-part z2)) - (+ (b24/imag-part z1) (b24/imag-part z2)))) - (sub-complex [z1 z2] (make-from-real-imag - (- (b24/real-part z1) (b24/real-part z2)) - (- (b24/imag-part z1) (b24/imag-part z2)))) - (mul-complex [z1 z2] (make-from-mag-ang - (* (b24/magnitude z1) (b24/magnitude z2)) - (+ (b24/angle z1) (b24/angle z2)))) - (div-complex [z1 z2] (make-from-mag-ang - (/ (b24/magnitude z1) (b24/magnitude z2)) - (- (b24/angle z1) (b24/angle z2)))) + (add-complex + [z1 z2] + (make-from-real-imag + (+ (b24/real-part z1) (b24/real-part z2)) + (+ (b24/imag-part z1) (b24/imag-part z2)))) + (sub-complex + [z1 z2] + (make-from-real-imag + (- (b24/real-part z1) (b24/real-part z2)) + (- (b24/imag-part z1) (b24/imag-part z2)))) + (mul-complex + [z1 z2] + (make-from-mag-ang + (* (b24/magnitude z1) (b24/magnitude z2)) + (+ (b24/angle z1) (b24/angle z2)))) + (div-complex + [z1 z2] + (make-from-mag-ang + (/ (b24/magnitude z1) (b24/magnitude z2)) + (- (b24/angle z1) (b24/angle z2)))) ; Interface to rest of the system (tag [z] (attach-tag :complex z))] ; Putting functions in a map or registry @@ -97,14 +132,18 @@ (put-op :make-from-mag-ang :complex (fn [r a] (tag (make-from-mag-ang r a)))) :done)) -(defn make-scheme-number [n] +(defn make-scheme-number + [n] ((get-op :make :scheme-number) n)) -(defn make-rational [n d] +(defn make-rational + [n d] ((get-op :make :rational) n d)) -(defn make-complex-from-real-imag [x y] +(defn make-complex-from-real-imag + [x y] ((get-op :make-from-real-imag :complex) x y)) -(defn make-complex-from-mag-ang [r a] +(defn make-complex-from-mag-ang + [r a] ((get-op :make-from-mag-ang :complex) r a)) diff --git a/src/sicp/misc.clj b/src/sicp/misc.clj index c91ee18..7d7ff18 100644 --- a/src/sicp/misc.clj +++ b/src/sicp/misc.clj @@ -1,58 +1,69 @@ (ns sicp.misc) -(comment "Chapter #1") -; The Elements of Programming ---------------------------------------------------------------------- +(comment "Chapter #1 The Elements of Programming -------------------------------------------------") -(defn d [vars] +(defn d + [vars] (println vars) vars) -(defn error [error-message] +(defn error + [error-message] (throw (Exception. (str error-message)))) -(defn square [x] +(defn square + [x] (* x x)) -(defn cube [x] +(defn cube + [x] (* x x x)) -(defn divides? [a b] +(defn divides? + [a b] (zero? (rem b a))) -(defn find-divisor [n test-divisor] +(defn find-divisor + [n test-divisor] (cond (> (square test-divisor) n) n (divides? test-divisor n) test-divisor :else (recur n (inc test-divisor)))) -(defn smallest-divisor [n] +(defn smallest-divisor + [n] (find-divisor n 2)) -(defn prime? [n] +(defn prime? + [n] (= n (smallest-divisor n))) (defn close-enough? ([x y] (< (abs (- x y)) 0.001)) ([x y tolerance] (< (abs (- x y)) tolerance))) -(defn average [a b] +(defn average + [a b] (/ (+ a b) 2)) -(defn positive? [x] +(defn positive? + [x] (> x 0)) -(defn negative? [x] +(defn negative? + [x] (< x 0)) -(defn gcd [a b] +(defn gcd + [a b] (if (zero? b) a (recur b (mod a b)))) -(defn round-to-dec [n decimals] +(defn round-to-dec + [n decimals] (let [factor (Math/pow 10 decimals)] (/ (Math/round (* n factor)) factor))) -(comment "Chapter #2") -; Introduction to Data Abstraction ----------------------------------------------------------------- +(comment "Chapter #2 Introduction to Data Abstraction --------------------------------------------") (comment (def x [1 2]) ; (define x (cons 1 2)) @@ -65,27 +76,33 @@ ([] (list nil nil)) ([head tail] (list head tail))) -(defn pair? [pair] +(defn pair? + [pair] (and (list? pair) (= 2 (count pair)))) -(defn leaf? [item] +(defn leaf? + [item] (not (list? item))) -(defn pair-empty? [pair] +(defn pair-empty? + [pair] (or (nil? pair) (= '() pair) (= '(nil nil) pair))) -(defn list-empty? [l] +(defn list-empty? + [l] (or (= '() l) (nil? l))) -(defn car [pair] +(defn car + [pair] (if (seq? pair) (first pair) pair)) -(defn cdr [pair] +(defn cdr + [pair] (cond (pair-empty? pair) nil (pair? pair) (last pair) @@ -93,19 +110,24 @@ (instance? clojure.lang.Sequential pair) (rest pair) :else nil)) -(defn cadr [pair] +(defn cadr + [pair] (second pair)) -(defn make-interval [low high] +(defn make-interval + [low high] (pair low high)) -(defn lower-bound [interval] +(defn lower-bound + [interval] (car interval)) -(defn upper-bound [interval] +(defn upper-bound + [interval] (cdr interval)) -(defn mul-interval [interval-1 interval-2] +(defn mul-interval + [interval-1 interval-2] (let [p1 (* (lower-bound interval-1) (lower-bound interval-2)) p2 (* (lower-bound interval-1) @@ -117,30 +139,36 @@ (make-interval (min p1 p2 p3 p4) (max p1 p2 p3 p4)))) -(defn div-interval [interval-1 interval-2] +(defn div-interval + [interval-1 interval-2] (mul-interval interval-1 (make-interval (/ 1.0 (upper-bound interval-2)) (/ 1.0 (lower-bound interval-2))))) -(defn add-interval [interval-1 interval-2] +(defn add-interval + [interval-1 interval-2] (make-interval (+ (lower-bound interval-1) (lower-bound interval-2)) (+ (upper-bound interval-1) (upper-bound interval-2)))) -(defn sub-interval [interval-1 interval-2] +(defn sub-interval + [interval-1 interval-2] (make-interval (- (lower-bound interval-1) (lower-bound interval-2)) (- (upper-bound interval-1) (upper-bound interval-2)))) -(defn make-center-width [c w] +(defn make-center-width + [c w] (make-interval (- c w) (+ c w))) -(defn center-interval [interval] +(defn center-interval + [interval] (/ (+ (lower-bound interval) (upper-bound interval)) 2.0)) -(defn width-interval [interval] +(defn width-interval + [interval] (/ (- (upper-bound interval) (lower-bound interval)) 2.0)) (defn print-interval @@ -152,12 +180,14 @@ "] / " (round-to-dec (center-interval interval) precision) "±" (round-to-dec (width-interval interval) precision)))) -(defn length [lst] +(defn length + [lst] (if (seq lst) (+ 1 (length (rest lst))) 0)) -(defn append [list1 list2] +(defn append + [list1 list2] (if (empty? list1) (if (empty? list2) '() list2) (cons (first list1) (append (rest list1) list2)))) diff --git a/test/sicp/chapter_1/part_1/book_1_1_test.clj b/test/sicp/chapter_1/part_1/book_1_1_test.clj index 82f2bfd..d776fae 100644 --- a/test/sicp/chapter_1/part_1/book_1_1_test.clj +++ b/test/sicp/chapter_1/part_1/book_1_1_test.clj @@ -1,23 +1,21 @@ (ns sicp.chapter-1.part-1.book-1-1-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-1.book-1-1 :as b11])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-1.book-1-1 :as b11])) -(comment "1.1.4") -; Compound Procedures ------------------------------------------------------------------------------ +(comment "1.1.4 Compound Procedures --------------------------------------------------------------") (deftest square-test (is (= 4 (b11/square 2))) (is (= 16 (b11/square 4)))) -(comment "1.1.5") -; The Substitution Model for Procedure Application ------------------------------------------------- +(comment "1.1.5 The Substitution Model for Procedure Application ---------------------------------") (deftest sum-of-squares-test (is (= 25 (b11/sum-of-squares 3 4))) (is (= 41 (b11/sum-of-squares 4 5)))) -(comment "1.1.6") -; Conditional Expressions and Predicates ----------------------------------------------------------- +(comment "1.1.6 Conditional Expressions and Predicates -------------------------------------------") (deftest abs-1-test (is (= 1 (b11/abs-1 1))) @@ -44,8 +42,7 @@ (is (= true (b11/>=-2 1 1))) (is (= true (b11/>=-2 2 1)))) -(comment "1.1.7") -; Example: Square Roots by Newton’s Method --------------------------------------------------------- +(comment "1.1.7 Example: Square Roots by Newton’s Method -----------------------------------------") (deftest sqrt-test (is (= 3.00009155413138 (b11/sqrt 9))) @@ -53,8 +50,7 @@ (is (= 1.7739279023207892 (b11/sqrt (+ (b11/sqrt 2) (b11/sqrt 3))))) (is (= 1000.000369924366 (b11/square (b11/sqrt 1000))))) -(comment "1.1.8") -; Procedures as Black-Box Abstractions ------------------------------------------------------------- +(comment "1.1.8 Procedures as Black-Box Abstractions ---------------------------------------------") (deftest square-alt-test (is (= 1.0 (b11/square-alt 1))) diff --git a/test/sicp/chapter_1/part_1/ex_1_3_test.clj b/test/sicp/chapter_1/part_1/ex_1_3_test.clj index ea60016..9682fd5 100644 --- a/test/sicp/chapter_1/part_1/ex_1_3_test.clj +++ b/test/sicp/chapter_1/part_1/ex_1_3_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-1.ex-1-3-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-1.ex-1-03 :refer [sum-larger-numbers-square]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-1.ex-1-03 :refer [sum-larger-numbers-square]])) (deftest sum-larger-numbers-square-test (is (= 2 (sum-larger-numbers-square 1 1 1))) diff --git a/test/sicp/chapter_1/part_1/ex_1_4_test.clj b/test/sicp/chapter_1/part_1/ex_1_4_test.clj index 5ba8083..5689278 100644 --- a/test/sicp/chapter_1/part_1/ex_1_4_test.clj +++ b/test/sicp/chapter_1/part_1/ex_1_4_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-1.ex-1-4-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-1.ex-1-04 :refer [a-plus-abs-b]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-1.ex-1-04 :refer [a-plus-abs-b]])) (deftest a-plus-abs-b-test (is (= 2 (a-plus-abs-b 1 1))) diff --git a/test/sicp/chapter_1/part_1/ex_1_5_test.clj b/test/sicp/chapter_1/part_1/ex_1_5_test.clj index 64a2950..6d3302a 100644 --- a/test/sicp/chapter_1/part_1/ex_1_5_test.clj +++ b/test/sicp/chapter_1/part_1/ex_1_5_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-1.ex-1-5-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-1.ex-1-05 :refer [p test_1_5]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-1.ex-1-05 :refer [p test_1_5]])) (deftest test_1_5-test ; Normal: Operand "p" will not be evaluated until it's needed by some primitive operation. So result is 0. diff --git a/test/sicp/chapter_1/part_1/ex_1_6_test.clj b/test/sicp/chapter_1/part_1/ex_1_6_test.clj index 974ffb8..3c2e7ac 100644 --- a/test/sicp/chapter_1/part_1/ex_1_6_test.clj +++ b/test/sicp/chapter_1/part_1/ex_1_6_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-1.part-1.ex-1-6-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-1.book-1-1 :as b11] - [sicp.chapter-1.part-1.ex-1-06])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-1.book-1-1 :as b11] + [sicp.chapter-1.part-1.ex-1-06])) (deftest sqrt-iter-test ; Expected diff --git a/test/sicp/chapter_1/part_1/ex_1_7_test.clj b/test/sicp/chapter_1/part_1/ex_1_7_test.clj index 9337d4f..3067dad 100644 --- a/test/sicp/chapter_1/part_1/ex_1_7_test.clj +++ b/test/sicp/chapter_1/part_1/ex_1_7_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-1.part-1.ex-1-7-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-1.book-1-1 :as b11] - [sicp.chapter-1.part-1.ex-1-07 :refer [sqrt-iter-v2]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-1.book-1-1 :as b11] + [sicp.chapter-1.part-1.ex-1-07 :refer [sqrt-iter-v2]])) (deftest sqrt-iter-test ; 1.0 diff --git a/test/sicp/chapter_1/part_1/ex_1_8_test.clj b/test/sicp/chapter_1/part_1/ex_1_8_test.clj index cb93ec1..40aa998 100644 --- a/test/sicp/chapter_1/part_1/ex_1_8_test.clj +++ b/test/sicp/chapter_1/part_1/ex_1_8_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-1.ex-1-8-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-1.ex-1-08 :refer [cube-root-iter]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-1.ex-1-08 :refer [cube-root-iter]])) (deftest cube-root-iter-test ; 1.0 diff --git a/test/sicp/chapter_1/part_2/book_1_2_test.clj b/test/sicp/chapter_1/part_2/book_1_2_test.clj index 1260252..b6a389e 100644 --- a/test/sicp/chapter_1/part_2/book_1_2_test.clj +++ b/test/sicp/chapter_1/part_2/book_1_2_test.clj @@ -1,9 +1,9 @@ (ns sicp.chapter-1.part-2.book-1-2-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.book-1-2 :as b12])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.book-1-2 :as b12])) -(comment "1.2.1") -; Linear Recursion and Iteration ------------------------------------------------------------------- +(comment "1.2.1 Linear Recursion and Iteration ---------------------------------------------------") (deftest factorial-test (is (= 1 (b12/factorial 1))) @@ -21,8 +21,7 @@ (is (= 120 (b12/factorial-iter 5))) (is (= 720 (b12/factorial-iter 6)))) -(comment "1.2.2") -; Tree Recursion ----------------------------------------------------------------------------------- +(comment "1.2.2 Tree Recursion -------------------------------------------------------------------") (deftest fib-test (is (= 5 (b12/fib 5))) @@ -40,12 +39,11 @@ (is (= 50 (b12/count-change 50))) (is (= 292 (b12/count-change 100)))) -(comment "1.2.3") -; Orders of Growth --------------------------------------------------------------------------------- +(comment "1.2.3 Orders of Growth -----------------------------------------------------------------") + ; There are no code examples for this part in the book :( -(comment "1.2.4") -; Exponentiation ----------------------------------------------------------------------------------- +(comment "1.2.4 Exponentiation -------------------------------------------------------------------") (deftest expt-test (is (= 1 (b12/expt 1 2))) @@ -65,8 +63,7 @@ (is (= false (b12/even?-alt 3))) (is (= true (b12/even?-alt 4)))) -(comment "1.2.5") -; Greatest Common Divisors ------------------------------------------------------------------------- +(comment "1.2.5 Greatest Common Divisors ---------------------------------------------------------") (deftest gcd-test (is (= 8 (b12/gcd 56 48))) @@ -78,8 +75,7 @@ (is (= 21 (b12/gcd 21 21))) (is (= 1 (b12/gcd 17 19)))) -(comment "1.2.6") -; Example: Testing for Primality ------------------------------------------------------------------- +(comment "1.2.6 Example: Testing for Primality ---------------------------------------------------") (deftest smallest-divisor-test (is (= 1 (b12/smallest-divisor 1))) diff --git a/test/sicp/chapter_1/part_2/ex_1_10_test.clj b/test/sicp/chapter_1/part_2/ex_1_10_test.clj index dcf94d3..bcb39dc 100644 --- a/test/sicp/chapter_1/part_2/ex_1_10_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_10_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-10-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-10 :refer [A f g h k]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-10 :refer [A f g h k]])) (deftest A-test (is (= 1024 (A 1 10))) diff --git a/test/sicp/chapter_1/part_2/ex_1_11_test.clj b/test/sicp/chapter_1/part_2/ex_1_11_test.clj index add696c..3d4d376 100644 --- a/test/sicp/chapter_1/part_2/ex_1_11_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_11_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-11-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-11 :refer [f f-v2]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-11 :refer [f f-v2]])) (deftest f-test ; ~ O(3^n) @@ -16,8 +17,7 @@ (is (= (f 10) 1892)) ; "Elapsed time: 2.025039 msecs" (is (= (f 20) 10771211)) ; "Elapsed time: 32.969995 msecs (is (= (f 25) 812934961)) ; "Elapsed time: 77.825729 msecs - (is (= (f 30) 61354575194)) ; "Elapsed time: 470.359975 msecs" -) + (is (= (f 30) 61354575194))) ; "Elapsed time: 470.359975 msecs" (deftest f-v2-test ; ~ O(n) @@ -33,5 +33,4 @@ (is (= (f-v2 10) 1892)) ; "Elapsed time: 3.245716 msecs" (is (= (f-v2 20) 10771211)) ; "Elapsed time: 3.275056 msecs" (is (= (f-v2 25) 812934961)) ; "Elapsed time: 1.989168 msecs" - (is (= (f-v2 30) 61354575194)) ; "Elapsed time: 2.462203 msecs" -) + (is (= (f-v2 30) 61354575194))) ; "Elapsed time: 2.462203 msecs" diff --git a/test/sicp/chapter_1/part_2/ex_1_12_test.clj b/test/sicp/chapter_1/part_2/ex_1_12_test.clj index a95bdb4..9148165 100644 --- a/test/sicp/chapter_1/part_2/ex_1_12_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_12_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-12-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-12 :refer [pascal]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-12 :refer [pascal]])) (deftest pascal-test (is (= 1 (pascal 1 1))) diff --git a/test/sicp/chapter_1/part_2/ex_1_14_test.clj b/test/sicp/chapter_1/part_2/ex_1_14_test.clj index 6818a1c..26077c7 100644 --- a/test/sicp/chapter_1/part_2/ex_1_14_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_14_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-14-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-14 :refer [money-change]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-14 :refer [money-change]])) (deftest count-change-test (is (= 1 (money-change 1))) diff --git a/test/sicp/chapter_1/part_2/ex_1_15_test.clj b/test/sicp/chapter_1/part_2/ex_1_15_test.clj index ae0e8a3..292b895 100644 --- a/test/sicp/chapter_1/part_2/ex_1_15_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_15_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-15-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-15 :refer [sine]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-15 :refer [sine]])) (deftest sine-test (is (= -0.39980345741334 (sine 12.15)))) ; 5 times, O(log3(n)) diff --git a/test/sicp/chapter_1/part_2/ex_1_16_test.clj b/test/sicp/chapter_1/part_2/ex_1_16_test.clj index 87461f8..bc89e46 100644 --- a/test/sicp/chapter_1/part_2/ex_1_16_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_16_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-16-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-16 :refer [expt]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-16 :refer [expt]])) (deftest expt-test (is (= 1 (expt 1 2))) diff --git a/test/sicp/chapter_1/part_2/ex_1_17_test.clj b/test/sicp/chapter_1/part_2/ex_1_17_test.clj index 96fdf84..096ff41 100644 --- a/test/sicp/chapter_1/part_2/ex_1_17_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_17_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-17-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-17 :refer [multi multi-fast]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-17 :refer [multi multi-fast]])) (deftest multi-test (is (= 2 (multi 1 2))) ; "Elapsed time: 2.383128 msecs" diff --git a/test/sicp/chapter_1/part_2/ex_1_18_test.clj b/test/sicp/chapter_1/part_2/ex_1_18_test.clj index 3aa642d..cd35374 100644 --- a/test/sicp/chapter_1/part_2/ex_1_18_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_18_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-18-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-18 :refer [mult]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-18 :refer [mult]])) (deftest mult-test (is (= 0 (mult 0 19)))) diff --git a/test/sicp/chapter_1/part_2/ex_1_19_test.clj b/test/sicp/chapter_1/part_2/ex_1_19_test.clj index cba9ee2..e7e47ec 100644 --- a/test/sicp/chapter_1/part_2/ex_1_19_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_19_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-19-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-19 :refer [fib fib-v2]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-19 :refer [fib fib-v2]])) (deftest fib-test (is (= 1 (fib 1))) diff --git a/test/sicp/chapter_1/part_2/ex_1_20_test.clj b/test/sicp/chapter_1/part_2/ex_1_20_test.clj index a7be97b..3b7ab3c 100644 --- a/test/sicp/chapter_1/part_2/ex_1_20_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_20_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-20-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-20 :refer [gcd]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-20 :refer [gcd]])) (deftest gcd-test (is (= 2 (gcd 206 40)))) ; 4 times diff --git a/test/sicp/chapter_1/part_2/ex_1_21_test.clj b/test/sicp/chapter_1/part_2/ex_1_21_test.clj index 96d59ab..11e9786 100644 --- a/test/sicp/chapter_1/part_2/ex_1_21_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_21_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-21-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-21 :refer [smallest-divisor]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-21 :refer [smallest-divisor]])) (deftest smallest-divisor-test (is (= 199 (smallest-divisor 199))) diff --git a/test/sicp/chapter_1/part_2/ex_1_22_test.clj b/test/sicp/chapter_1/part_2/ex_1_22_test.clj index ee8c289..a359961 100644 --- a/test/sicp/chapter_1/part_2/ex_1_22_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_22_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-22-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-22 :refer [find-primes]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-22 :refer [find-primes]])) (deftest find-primes-test (is (= 5 (find-primes 1 5 false)))) ; Change arguments and see the output diff --git a/test/sicp/chapter_1/part_2/ex_1_23_test.clj b/test/sicp/chapter_1/part_2/ex_1_23_test.clj index 48043f9..775b06f 100644 --- a/test/sicp/chapter_1/part_2/ex_1_23_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_23_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-23-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-23 :refer [find-divisor next-odd prime? smallest-divisor]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-23 :refer [find-divisor next-odd prime? smallest-divisor]])) (deftest next-odd-test (is (= 3 (next-odd 1))) diff --git a/test/sicp/chapter_1/part_2/ex_1_24_test.clj b/test/sicp/chapter_1/part_2/ex_1_24_test.clj index fb85ae7..dc438db 100644 --- a/test/sicp/chapter_1/part_2/ex_1_24_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_24_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-24-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-24 :refer [fast-prime? fermat-test]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-24 :refer [fast-prime? fermat-test]])) (deftest fast-prime?-test (is (= false (fast-prime? 100 100)))) diff --git a/test/sicp/chapter_1/part_2/ex_1_25_test.clj b/test/sicp/chapter_1/part_2/ex_1_25_test.clj index c3e093c..ea015dc 100644 --- a/test/sicp/chapter_1/part_2/ex_1_25_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_25_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-25-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-25 :refer [expmod expmod-alyssa]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-25 :refer [expmod expmod-alyssa]])) (deftest expmod-test (is (= 1 (expmod 2 0 3))) @@ -8,4 +9,5 @@ (deftest expmod-alyssa-test (is (= 1 (expmod-alyssa 2 0 3)))) - ; (is (= 3 (expmod-alyssa 123 44 55))) ; It makes too many iterations => time & memory consuming + +; (is (= 3 (expmod-alyssa 123 44 55))) ; It makes too many iterations => time & memory consuming diff --git a/test/sicp/chapter_1/part_2/ex_1_26_test.clj b/test/sicp/chapter_1/part_2/ex_1_26_test.clj index edbd76d..9fdfdde 100644 --- a/test/sicp/chapter_1/part_2/ex_1_26_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_26_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-26-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-26 :refer [expmod]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-26 :refer [expmod]])) (deftest expmod-test (is (= 1 (expmod 2 0 3)))) diff --git a/test/sicp/chapter_1/part_2/ex_1_27_test.clj b/test/sicp/chapter_1/part_2/ex_1_27_test.clj index d40cafc..431f468 100644 --- a/test/sicp/chapter_1/part_2/ex_1_27_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_27_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-27-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-27 :refer [fermat-test]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-27 :refer [fermat-test]])) (deftest fermat-test-test (is (= true (fermat-test 561))) diff --git a/test/sicp/chapter_1/part_2/ex_1_9_test.clj b/test/sicp/chapter_1/part_2/ex_1_9_test.clj index 0697d48..507e3e7 100644 --- a/test/sicp/chapter_1/part_2/ex_1_9_test.clj +++ b/test/sicp/chapter_1/part_2/ex_1_9_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-2.ex-1-9-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-2.ex-1-09 :refer [plus plus-v2]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-2.ex-1-09 :refer [plus plus-v2]])) (deftest plus-test (is (= 3 (plus 1 2)))) diff --git a/test/sicp/chapter_1/part_3/book_1_3_test.clj b/test/sicp/chapter_1/part_3/book_1_3_test.clj index 12032ed..d28543d 100644 --- a/test/sicp/chapter_1/part_3/book_1_3_test.clj +++ b/test/sicp/chapter_1/part_3/book_1_3_test.clj @@ -1,10 +1,10 @@ (ns sicp.chapter-1.part-3.book-1-3-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.book-1-3 :as b13] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.book-1-3 :as b13] + [sicp.misc :as m])) -(comment "1.3.1") -; Procedures as Arguments -------------------------------------------------------------------------- +(comment "1.3.1 Procedures as Arguments ----------------------------------------------------------") (deftest sum-integers-test (is (= 0 (b13/sum-integers 0 0))) @@ -54,8 +54,7 @@ (is (= 0.24998750000000042 (b13/integral m/cube 0 1 0.01))) (is (= 0.249999875000001 (b13/integral m/cube 0 1 0.001)))) -(comment "1.3.2") -; Constructing Procedures Using Lambda ------------------------------------------------------------- +(comment "1.3.2 Constructing Procedures Using Lambda ---------------------------------------------") (deftest pi-sum-lamda-test (is (= 0.3333333333333333 (b13/pi-sum-lamda 1 1))) @@ -67,8 +66,7 @@ (is (= 0.24998750000000042 (b13/integral-lamda m/cube 0 1 0.01))) (is (= 0.249999875000001 (b13/integral-lamda m/cube 0 1 0.001)))) -(comment "1.3.3") -; Procedures as General Methods -------------------------------------------------------------------- +(comment "1.3.3 Procedures as General Methods ----------------------------------------------------") (deftest search-test (is (= 0 (b13/search m/cube -1 1))) @@ -89,8 +87,7 @@ (is (= 1.4142135623746899 (b13/sqrt 2))) (is (= 3.0 (b13/sqrt 9)))) -(comment "1.3.4") -; Procedures as Returned Values -------------------------------------------------------------------- +(comment "1.3.4 Procedures as Returned Values ----------------------------------------------------") (deftest average-damp-test (is (= 55 ((b13/average-damp m/square) 10)))) diff --git a/test/sicp/chapter_1/part_3/ex_1_29_test.clj b/test/sicp/chapter_1/part_3/ex_1_29_test.clj index 645c6b4..afbf7cd 100644 --- a/test/sicp/chapter_1/part_3/ex_1_29_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_29_test.clj @@ -1,8 +1,9 @@ (ns sicp.chapter-1.part-3.ex-1-29-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.book-1-3 :refer [integral]] - [sicp.chapter-1.part-3.ex-1-29 :refer [integral-simpson]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.book-1-3 :refer [integral]] + [sicp.chapter-1.part-3.ex-1-29 :refer [integral-simpson]] + [sicp.misc :as m])) (deftest integral-test (is (= 1/8 (integral m/cube 0 1 1))) diff --git a/test/sicp/chapter_1/part_3/ex_1_30_test.clj b/test/sicp/chapter_1/part_3/ex_1_30_test.clj index d6264f3..f25825b 100644 --- a/test/sicp/chapter_1/part_3/ex_1_30_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_30_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-1.part-3.ex-1-30-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-30 :refer [sum-2]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-30 :refer [sum-2]] + [sicp.misc :as m])) (deftest sum-2-test (is (= 385 (sum-2 m/square 1 inc 10))) diff --git a/test/sicp/chapter_1/part_3/ex_1_31_test.clj b/test/sicp/chapter_1/part_3/ex_1_31_test.clj index fc26c0f..44b9aa2 100644 --- a/test/sicp/chapter_1/part_3/ex_1_31_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_31_test.clj @@ -1,10 +1,12 @@ (ns sicp.chapter-1.part-3.ex-1-31-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-31 :refer [factorial pi pi-iter]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-31 :refer [factorial pi pi-iter]])) -(def pi_manual (* 4.0 - (/ (* 2 4 4 6 6 8 8) - (* 3 3 5 5 7 7 9)))) +(def pi_manual + (* 4.0 + (/ (* 2 4 4 6 6 8 8) + (* 3 3 5 5 7 7 9)))) (deftest factorial-test (is (= 1 (factorial 1))) diff --git a/test/sicp/chapter_1/part_3/ex_1_32_test.clj b/test/sicp/chapter_1/part_3/ex_1_32_test.clj index 741ef0a..a475859 100644 --- a/test/sicp/chapter_1/part_3/ex_1_32_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_32_test.clj @@ -1,9 +1,10 @@ (ns sicp.chapter-1.part-3.ex-1-32-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-32 :refer [accumulate - accumulate-iter - factorial - factorial-iter]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-32 :refer [accumulate + accumulate-iter + factorial + factorial-iter]])) (deftest accumulate-test (is (= 55 (accumulate + 0 identity 1 inc 10))) diff --git a/test/sicp/chapter_1/part_3/ex_1_33_test.clj b/test/sicp/chapter_1/part_3/ex_1_33_test.clj index bd17310..c22a106 100644 --- a/test/sicp/chapter_1/part_3/ex_1_33_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_33_test.clj @@ -1,10 +1,11 @@ (ns sicp.chapter-1.part-3.ex-1-33-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-33 :refer [filtered-accumulate - filtered-accumulate-iter - product-of-primes - sum-of-primes]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-33 :refer [filtered-accumulate + filtered-accumulate-iter + product-of-primes + sum-of-primes]] + [sicp.misc :as m])) (deftest filtered-accumulate-test (is (= 1061 (filtered-accumulate + 0 identity 1 inc 100 m/prime?))) diff --git a/test/sicp/chapter_1/part_3/ex_1_34_test.clj b/test/sicp/chapter_1/part_3/ex_1_34_test.clj index ac97c2d..7e85450 100644 --- a/test/sicp/chapter_1/part_3/ex_1_34_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_34_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-1.part-3.ex-1-34-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-34 :refer [f]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-34 :refer [f]] + [sicp.misc :as m])) (deftest f-test (is (= 4 (f m/square))) diff --git a/test/sicp/chapter_1/part_3/ex_1_35_test.clj b/test/sicp/chapter_1/part_3/ex_1_35_test.clj index c47c197..c855b8a 100644 --- a/test/sicp/chapter_1/part_3/ex_1_35_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_35_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-3.ex-1-35-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-35 :refer [gorlden-ration]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-35 :refer [gorlden-ration]])) (deftest gorlden-ration-test (is (= 1.6180327868852458 (gorlden-ration 2))) diff --git a/test/sicp/chapter_1/part_3/ex_1_36_test.clj b/test/sicp/chapter_1/part_3/ex_1_36_test.clj index 57f5cce..3512beb 100644 --- a/test/sicp/chapter_1/part_3/ex_1_36_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_36_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-1.part-3.ex-1-36-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-36 :refer [fixed-point-print]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-36 :refer [fixed-point-print]] + [sicp.misc :as m])) (def print-logs false) diff --git a/test/sicp/chapter_1/part_3/ex_1_37_test.clj b/test/sicp/chapter_1/part_3/ex_1_37_test.clj index 3f8c50b..5e4ddc9 100644 --- a/test/sicp/chapter_1/part_3/ex_1_37_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_37_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-1.part-3.ex-1-37-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-37 :refer [cont-frac cont-frac-iter]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-37 :refer [cont-frac cont-frac-iter]] + [sicp.misc :as m])) ; See https://en.wikipedia.org/wiki/Golden_ratio (def invert-psi (/ 1 1.618033988749894)) ; 0.6180339887498948 @@ -9,7 +10,8 @@ ; 1. Recursive process ----------------------------------------------------------------------------- ; Moving from 1 => k -(defn frac-wrap [k precision] +(defn frac-wrap + [k precision] (m/close-enough? invert-psi (cont-frac (fn [_] 1.0) (fn [_] 1.0) k) precision)) @@ -27,7 +29,8 @@ ; 2. Iterative process ----------------------------------------------------------------------------- ; Moving from k => 1. So wie have +1 iteration -(defn frac-iter-wrap [k precision] +(defn frac-iter-wrap + [k precision] (m/close-enough? invert-psi (cont-frac-iter (fn [_] 1.0) (fn [_] 1.0) k) precision)) diff --git a/test/sicp/chapter_1/part_3/ex_1_38_test.clj b/test/sicp/chapter_1/part_3/ex_1_38_test.clj index 0332571..ed122ec 100644 --- a/test/sicp/chapter_1/part_3/ex_1_38_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_38_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-1.part-3.ex-1-38-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-38 :refer [euler-fraction]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-38 :refer [euler-fraction]] + [sicp.misc :as m])) (def e-2 (- Math/E 2)) ; 0.7182818284590451 (def precision 0.000001) diff --git a/test/sicp/chapter_1/part_3/ex_1_39_test.clj b/test/sicp/chapter_1/part_3/ex_1_39_test.clj index 1d6348d..f378574 100644 --- a/test/sicp/chapter_1/part_3/ex_1_39_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_39_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-3.ex-1-39-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-39 :refer [tan-cf]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-39 :refer [tan-cf]])) (def angle (/ Math/PI 3)) diff --git a/test/sicp/chapter_1/part_3/ex_1_40_test.clj b/test/sicp/chapter_1/part_3/ex_1_40_test.clj index bc31d00..5bd576d 100644 --- a/test/sicp/chapter_1/part_3/ex_1_40_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_40_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-1.part-3.ex-1-40-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.book-1-3 :as b13] - [sicp.chapter-1.part-3.ex-1-40 :refer [cubic]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.book-1-3 :as b13] + [sicp.chapter-1.part-3.ex-1-40 :refer [cubic]])) (deftest cubic-test (is (= -1.2756822036498454 (b13/newtons-method (cubic 1 2 3) 1)))) diff --git a/test/sicp/chapter_1/part_3/ex_1_41_test.clj b/test/sicp/chapter_1/part_3/ex_1_41_test.clj index adec1f2..7844997 100644 --- a/test/sicp/chapter_1/part_3/ex_1_41_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_41_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-3.ex-1-41-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-41 :refer [double-fn]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-41 :refer [double-fn]])) (deftest double-fn-test (is (= 3 ((double-fn inc) 1))) diff --git a/test/sicp/chapter_1/part_3/ex_1_42_test.clj b/test/sicp/chapter_1/part_3/ex_1_42_test.clj index e83857d..c5c42dc 100644 --- a/test/sicp/chapter_1/part_3/ex_1_42_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_42_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-1.part-3.ex-1-42-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-42 :refer [compose]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-42 :refer [compose]] + [sicp.misc :as m])) (deftest compose-test (is (= 49 ((compose m/square inc) 6)))) diff --git a/test/sicp/chapter_1/part_3/ex_1_43_test.clj b/test/sicp/chapter_1/part_3/ex_1_43_test.clj index c187ebb..1b21e7c 100644 --- a/test/sicp/chapter_1/part_3/ex_1_43_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_43_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-1.part-3.ex-1-43-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-43 :refer [repeated repeated-compose]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-43 :refer [repeated repeated-compose]] + [sicp.misc :as m])) (deftest repeated-test (is (= (m/square 5) diff --git a/test/sicp/chapter_1/part_3/ex_1_44_test.clj b/test/sicp/chapter_1/part_3/ex_1_44_test.clj index 39ef18e..ad1e59b 100644 --- a/test/sicp/chapter_1/part_3/ex_1_44_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_44_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-1.part-3.ex-1-44-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-44 :refer [n-fold smooth]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-44 :refer [n-fold smooth]] + [sicp.misc :as m])) (deftest smooth-test (is (= 1.0000019999999998 ((smooth m/cube) 1))) diff --git a/test/sicp/chapter_1/part_3/ex_1_45_test.clj b/test/sicp/chapter_1/part_3/ex_1_45_test.clj index 7dba0f7..7b540a5 100644 --- a/test/sicp/chapter_1/part_3/ex_1_45_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_45_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-3.ex-1-45-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-45 :refer [damp-number nth-root nth-root-auto-damps]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-45 :refer [damp-number nth-root nth-root-auto-damps]])) (deftest nth-root-test (is (= 2.0000000000039666 (nth-root 256 8 3)))) diff --git a/test/sicp/chapter_1/part_3/ex_1_46_test.clj b/test/sicp/chapter_1/part_3/ex_1_46_test.clj index 34de37f..58d2506 100644 --- a/test/sicp/chapter_1/part_3/ex_1_46_test.clj +++ b/test/sicp/chapter_1/part_3/ex_1_46_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-1.part-3.ex-1-46-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-1.part-3.ex-1-46 :refer [fixed-point-improved sqrt-improved]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-1.part-3.ex-1-46 :refer [fixed-point-improved sqrt-improved]])) (deftest sqrt-improved-test (is (= 1.7321428571428572 (sqrt-improved 3 1.0))) diff --git a/test/sicp/chapter_2/part_1/book_2_1_test.clj b/test/sicp/chapter_2/part_1/book_2_1_test.clj index 5be53b2..192d135 100644 --- a/test/sicp/chapter_2/part_1/book_2_1_test.clj +++ b/test/sicp/chapter_2/part_1/book_2_1_test.clj @@ -1,15 +1,14 @@ (ns sicp.chapter-2.part-1.book-2-1-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.book-2-1 :as b21])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.book-2-1 :as b21])) -(comment "2") -; Building Abstractions with Data ------------------------------------------------------------------ +(comment "2 Building Abstractions with Data ------------------------------------------------------") (deftest linear-combination-test (is (= 11 (b21/linear-combination 1 2 3 4)))) -(comment "2.1.1") -; Example: Arithmetic Operations for Rational Numbers ---------------------------------------------- +(comment "2.1.1 Example: Arithmetic Operations for Rational Numbers ------------------------------") (def one-half (b21/make-rat 1 2)) (def one-third (b21/make-rat 1 3)) @@ -51,8 +50,7 @@ (is (= "1/2" (b21/print-rat one-half))) (is (= "1/3" (b21/print-rat one-third)))) -(comment "2.1.2") -; Abstraction Barriers ----------------------------------------------------------------------------- +(comment "2.1.2 Abstraction Barriers -------------------------------------------------------------") (def one-half-alt (b21/make-rat-alt 1 2)) (def one-third-alt (b21/make-rat-alt 1 3)) @@ -73,8 +71,7 @@ (is (= 3 (b21/denom-alt one-third-alt))) (is (= 4 (b21/denom-alt no-optimal-alt)))) -(comment "2.1.3") -; What Is Meant by Data? --------------------------------------------------------------------------- +(comment "2.1.3 What Is Meant by Data? -----------------------------------------------------------") (deftest pair-alt-test (is (= 1 (b21/car-alt (b21/pair-alt 1 2)))) diff --git a/test/sicp/chapter_2/part_1/ex_2_01_test.clj b/test/sicp/chapter_2/part_1/ex_2_01_test.clj index fb795dd..08499a5 100644 --- a/test/sicp/chapter_2/part_1/ex_2_01_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_01_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-1.ex-2-01-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.book-2-1 :as b21] - [sicp.chapter-2.part-1.ex-2-01 :refer [make-rat-2]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.book-2-1 :as b21] + [sicp.chapter-2.part-1.ex-2-01 :refer [make-rat-2]])) (deftest make-rat-2-test (is (= [1 1] (make-rat-2 1 1))) diff --git a/test/sicp/chapter_2/part_1/ex_2_02_test.clj b/test/sicp/chapter_2/part_1/ex_2_02_test.clj index fcd17a3..26f20b9 100644 --- a/test/sicp/chapter_2/part_1/ex_2_02_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_02_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-1.ex-2-02-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.ex-2-02 :as ex-2-02])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.ex-2-02 :as ex-2-02])) (def point-1 (ex-2-02/make-point 1 2)) (def point-2 (ex-2-02/make-point 3 4)) diff --git a/test/sicp/chapter_2/part_1/ex_2_03_test.clj b/test/sicp/chapter_2/part_1/ex_2_03_test.clj index 645fc20..ee2eaa4 100644 --- a/test/sicp/chapter_2/part_1/ex_2_03_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_03_test.clj @@ -1,12 +1,17 @@ (ns sicp.chapter-2.part-1.ex-2-03-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.ex-2-02 :as ex-2-02] - [sicp.chapter-2.part-1.ex-2-03 :as ex-2-03])) - -(def seg-width (ex-2-02/make-segment (ex-2-02/make-point 0 0) - (ex-2-02/make-point 5 12))) -(def seg-height (ex-2-02/make-segment (ex-2-02/make-point 1 1) - (ex-2-02/make-point 4 5))) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.ex-2-02 :as ex-2-02] + [sicp.chapter-2.part-1.ex-2-03 :as ex-2-03])) + +(def seg-width + (ex-2-02/make-segment (ex-2-02/make-point 0 0) + (ex-2-02/make-point 5 12))) + +(def seg-height + (ex-2-02/make-segment (ex-2-02/make-point 1 1) + (ex-2-02/make-point 4 5))) + (def rectangle (ex-2-03/make-rectangle seg-width seg-height)) (deftest length-segment-test diff --git a/test/sicp/chapter_2/part_1/ex_2_04_test.clj b/test/sicp/chapter_2/part_1/ex_2_04_test.clj index 7fe89cc..2660920 100644 --- a/test/sicp/chapter_2/part_1/ex_2_04_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_04_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-1.ex-2-04-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.ex-2-04 :as ex-2-04])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.ex-2-04 :as ex-2-04])) (deftest pair-test (is (= 1 (ex-2-04/car (ex-2-04/pair 1 2)))) diff --git a/test/sicp/chapter_2/part_1/ex_2_05_test.clj b/test/sicp/chapter_2/part_1/ex_2_05_test.clj index 436e724..e10dabd 100644 --- a/test/sicp/chapter_2/part_1/ex_2_05_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_05_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-1.ex-2-05-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.ex-2-05 :as ex-2-05])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.ex-2-05 :as ex-2-05])) (deftest pair-test (is (= 1 (ex-2-05/car (ex-2-05/pair 1 2)))) diff --git a/test/sicp/chapter_2/part_1/ex_2_06_test.clj b/test/sicp/chapter_2/part_1/ex_2_06_test.clj index 58c318a..43116a7 100644 --- a/test/sicp/chapter_2/part_1/ex_2_06_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_06_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-1.ex-2-06-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.ex-2-06 :as ex-2-06])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.ex-2-06 :as ex-2-06])) (def f (fn [x] x)) (def placeholder (rand-int 100)) ; Hackish to execute test cases diff --git a/test/sicp/chapter_2/part_1/ex_2_07_test.clj b/test/sicp/chapter_2/part_1/ex_2_07_test.clj index 5796f19..41f109c 100644 --- a/test/sicp/chapter_2/part_1/ex_2_07_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_07_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-1.ex-2-07-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.book-2-1 :as b21] - [sicp.chapter-2.part-1.ex-2-07 :refer [lower-bound make-interval upper-bound]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.book-2-1 :as b21] + [sicp.chapter-2.part-1.ex-2-07 :refer [lower-bound make-interval upper-bound]])) (def interval-1 (make-interval 1 2)) (def interval-2 (make-interval 4 5)) diff --git a/test/sicp/chapter_2/part_1/ex_2_08_test.clj b/test/sicp/chapter_2/part_1/ex_2_08_test.clj index 7016bf2..17e378b 100644 --- a/test/sicp/chapter_2/part_1/ex_2_08_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_08_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-1.ex-2-08-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.ex-2-07 :refer [make-interval]] - [sicp.chapter-2.part-1.ex-2-08 :refer [sub-interval]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.ex-2-07 :refer [make-interval]] + [sicp.chapter-2.part-1.ex-2-08 :refer [sub-interval]])) (def interval-1 (make-interval 1 1.1)) (def interval-2 (make-interval 5 5.5)) diff --git a/test/sicp/chapter_2/part_1/ex_2_09_test.clj b/test/sicp/chapter_2/part_1/ex_2_09_test.clj index d16f489..f227cf7 100644 --- a/test/sicp/chapter_2/part_1/ex_2_09_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_09_test.clj @@ -1,9 +1,10 @@ (ns sicp.chapter-2.part-1.ex-2-09-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.book-2-1 :as b21] - [sicp.chapter-2.part-1.ex-2-07 :as ex-2-07] - [sicp.chapter-2.part-1.ex-2-08 :as ex-2-08] - [sicp.chapter-2.part-1.ex-2-09 :refer [radius]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.book-2-1 :as b21] + [sicp.chapter-2.part-1.ex-2-07 :as ex-2-07] + [sicp.chapter-2.part-1.ex-2-08 :as ex-2-08] + [sicp.chapter-2.part-1.ex-2-09 :refer [radius]])) (def interval-1 (ex-2-07/make-interval 20 30)) (def interval-2 (ex-2-07/make-interval 60 100)) diff --git a/test/sicp/chapter_2/part_1/ex_2_10_test.clj b/test/sicp/chapter_2/part_1/ex_2_10_test.clj index 0267321..b3bdf80 100644 --- a/test/sicp/chapter_2/part_1/ex_2_10_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_10_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-1.ex-2-10-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.ex-2-10 :refer [div-interval]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.ex-2-10 :refer [div-interval]] + [sicp.misc :as m])) (deftest div-interval-test (is (= [-1.5 -0.5] diff --git a/test/sicp/chapter_2/part_1/ex_2_11_test.clj b/test/sicp/chapter_2/part_1/ex_2_11_test.clj index b0c323e..4d0802d 100644 --- a/test/sicp/chapter_2/part_1/ex_2_11_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_11_test.clj @@ -1,11 +1,12 @@ (ns sicp.chapter-2.part-1.ex-2-11-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.ex-2-11 :refer [center - interval-case - make-center-width - mul-interval - width]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.ex-2-11 :refer [center + interval-case + make-center-width + mul-interval + width]] + [sicp.misc :as m])) (def interval (m/make-interval 95 105)) diff --git a/test/sicp/chapter_2/part_1/ex_2_12_test.clj b/test/sicp/chapter_2/part_1/ex_2_12_test.clj index b6b9571..a0c1f56 100644 --- a/test/sicp/chapter_2/part_1/ex_2_12_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_12_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-1.ex-2-12-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.ex-2-12 :refer [make-center-percent percent]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.ex-2-12 :refer [make-center-percent percent]] + [sicp.misc :as m])) (deftest make-center-percent-test (is (= [95.0 105.0] (make-center-percent 100 5))) diff --git a/test/sicp/chapter_2/part_1/ex_2_13_test.clj b/test/sicp/chapter_2/part_1/ex_2_13_test.clj index 40c298d..192c90b 100644 --- a/test/sicp/chapter_2/part_1/ex_2_13_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_13_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-1.ex-2-13-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.ex-2-13 :refer [par1 par2]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.ex-2-13 :refer [par1 par2]] + [sicp.misc :as m])) ; Huge tolerance - ±0.5) (def i-01 (m/make-interval 1 2)) diff --git a/test/sicp/chapter_2/part_1/ex_2_14_test.clj b/test/sicp/chapter_2/part_1/ex_2_14_test.clj index cc47fbf..ba19de1 100644 --- a/test/sicp/chapter_2/part_1/ex_2_14_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_14_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-1.ex-2-14-test - (:require [clojure.test :refer [deftest is]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.misc :as m])) ; Huge tolerance - ±0.5) (def a-wide (m/make-interval 1 2)) diff --git a/test/sicp/chapter_2/part_1/ex_2_15_test.clj b/test/sicp/chapter_2/part_1/ex_2_15_test.clj index 887af09..6705598 100644 --- a/test/sicp/chapter_2/part_1/ex_2_15_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_15_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-1.ex-2-15-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-1.ex-2-13 :as ex-2-13] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-1.ex-2-13 :as ex-2-13] + [sicp.misc :as m])) ; Huge tolerance - ±0.5) (def a-wide (m/make-interval 1 2)) diff --git a/test/sicp/chapter_2/part_2/book_2_2_test.clj b/test/sicp/chapter_2/part_2/book_2_2_test.clj index 0802c96..9c642dc 100644 --- a/test/sicp/chapter_2/part_2/book_2_2_test.clj +++ b/test/sicp/chapter_2/part_2/book_2_2_test.clj @@ -1,28 +1,29 @@ (ns sicp.chapter-2.part-2.book-2-2-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.book-2-2 :refer [accumulate - append - count-leaves - enumerate-interval - enumerate-tree - even-fibs - even-fibs-v2 - flatmap - length - length-recursice - length-tree - list-fib-squares - list-ref - make-pair-sum - my-map - permutations - prime-sum-pairs - prime-sum? - product-of-squares-of-odd-elements - scale-tree - scale-tree-v0 - sum-odd-squares - sum-odd-squares-v2]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.book-2-2 :refer [accumulate + append + count-leaves + enumerate-interval + enumerate-tree + even-fibs + even-fibs-v2 + flatmap + length + length-recursice + length-tree + list-fib-squares + list-ref + make-pair-sum + my-map + permutations + prime-sum-pairs + prime-sum? + product-of-squares-of-odd-elements + scale-tree + scale-tree-v0 + sum-odd-squares + sum-odd-squares-v2]])) (deftest list-ref-test (is (= nil (list-ref '(1 4 9 16 25) -1))) @@ -64,8 +65,7 @@ (is (= '(10 2.5 11.6 17) (map abs (list -10 2.5 -11.6 17)))) (is (= '(1 4 9 16) (map #(* % %) (list 1 2 3 4))))) -(comment "2.2.2") -; Hierarchical Structures -------------------------------------------------------------------------- +(comment "2.2.2 Hierarchical Structures ----------------------------------------------------------") (def tree (cons (list 1 2) (list 3 4))) ; ((1 2) 3 4) (def tree-2 (list tree tree)) ; (((1 2) 3 4) ((1 2) 3 4)) @@ -88,8 +88,7 @@ (deftest scale-tree-test (is (= '(10 (20 (30 40) 50) (60 70)) (scale-tree '(1 (2 (3 4) 5) (6 7)) 10)))) -(comment "2.2.3") -; Sequences as Conventional Interfaces ------------------------------------------------------------- +(comment "2.2.3 Sequences as Conventional Interfaces ---------------------------------------------") (deftest sum-odd-squares-test (is (= 84 (sum-odd-squares '(1 (2 (3 4) 5) (6 7)))))) @@ -153,6 +152,6 @@ (3 1 2) (3 2 1)) (permutations '(1 2 3))))) -(comment "2.2.4") -; Example: A Picture Language ---------------------------------------------------------------------- +(comment "2.2.4 Example: A Picture Language ------------------------------------------------------") + ; No tests diff --git a/test/sicp/chapter_2/part_2/ex_2_17_test.clj b/test/sicp/chapter_2/part_2/ex_2_17_test.clj index 2bd3ced..116f339 100644 --- a/test/sicp/chapter_2/part_2/ex_2_17_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_17_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-17-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-17 :refer [last-pair]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-17 :refer [last-pair]])) (deftest last-pair-test (is (= nil (last-pair '()))) diff --git a/test/sicp/chapter_2/part_2/ex_2_18_test.clj b/test/sicp/chapter_2/part_2/ex_2_18_test.clj index 503570f..a479906 100644 --- a/test/sicp/chapter_2/part_2/ex_2_18_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_18_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-18-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-18 :refer [list-reverse]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-18 :refer [list-reverse]])) (deftest list-reverse-test (is (= '() (list-reverse nil))) diff --git a/test/sicp/chapter_2/part_2/ex_2_19_test.clj b/test/sicp/chapter_2/part_2/ex_2_19_test.clj index baf7293..29ec5d5 100644 --- a/test/sicp/chapter_2/part_2/ex_2_19_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_19_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-19-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-19 :refer [cc]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-19 :refer [cc]])) (deftest cc-test (is (= 1 (cc 100 '(1)))) diff --git a/test/sicp/chapter_2/part_2/ex_2_20_test.clj b/test/sicp/chapter_2/part_2/ex_2_20_test.clj index c3f8690..f78b786 100644 --- a/test/sicp/chapter_2/part_2/ex_2_20_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_20_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-20-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-20 :refer [same-parity]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-20 :refer [same-parity]])) (deftest same-parity-test (is (= '(2 4 6) (same-parity 2 3 4 5 6 7))) diff --git a/test/sicp/chapter_2/part_2/ex_2_21_test.clj b/test/sicp/chapter_2/part_2/ex_2_21_test.clj index ca575d3..cb56bc5 100644 --- a/test/sicp/chapter_2/part_2/ex_2_21_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_21_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-21-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-21 :refer [square-list-1 square-list-2]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-21 :refer [square-list-1 square-list-2]])) (deftest square-list-2-test (is (= '(1 4 9 16) (square-list-1 '(1 2 3 4)))) diff --git a/test/sicp/chapter_2/part_2/ex_2_22_test.clj b/test/sicp/chapter_2/part_2/ex_2_22_test.clj index 164672c..b070e9d 100644 --- a/test/sicp/chapter_2/part_2/ex_2_22_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_22_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-22-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-22 :refer [square-list-1]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-22 :refer [square-list-1]])) (deftest square-list-1-test (is (= '(16 9 4 1) (square-list-1 (list 1 2 3 4))))) diff --git a/test/sicp/chapter_2/part_2/ex_2_23_test.clj b/test/sicp/chapter_2/part_2/ex_2_23_test.clj index 3530150..fa705c7 100644 --- a/test/sicp/chapter_2/part_2/ex_2_23_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_23_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-23-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-23 :refer [for-each]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-23 :refer [for-each]])) (deftest for-each-test (is (= true (for-each #(comment (println %)) diff --git a/test/sicp/chapter_2/part_2/ex_2_24_test.clj b/test/sicp/chapter_2/part_2/ex_2_24_test.clj index 3eebe37..09c0217 100644 --- a/test/sicp/chapter_2/part_2/ex_2_24_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_24_test.clj @@ -1,5 +1,6 @@ (ns sicp.chapter-2.part-2.ex-2-24-test - (:require [clojure.test :refer [deftest is]])) + (:require + [clojure.test :refer [deftest is]])) (deftest list-test (is (= '(1 (2 (3 4))) diff --git a/test/sicp/chapter_2/part_2/ex_2_25_test.clj b/test/sicp/chapter_2/part_2/ex_2_25_test.clj index 43a9d5a..5d1bab8 100644 --- a/test/sicp/chapter_2/part_2/ex_2_25_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_25_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-25-test - (:require [clojure.test :refer [deftest is]] - [sicp.misc :refer [car cdr]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.misc :refer [car cdr]])) (deftest list-test (is (= 7 diff --git a/test/sicp/chapter_2/part_2/ex_2_26_test.clj b/test/sicp/chapter_2/part_2/ex_2_26_test.clj index 7281908..9ee8246 100644 --- a/test/sicp/chapter_2/part_2/ex_2_26_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_26_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-26-test - (:require [clojure.test :refer [deftest is]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.misc :as m])) (def x (list 1 2 3)) (def y (list 4 5 6)) diff --git a/test/sicp/chapter_2/part_2/ex_2_27_test.clj b/test/sicp/chapter_2/part_2/ex_2_27_test.clj index 2198535..e45721f 100644 --- a/test/sicp/chapter_2/part_2/ex_2_27_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_27_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-27-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-27 :refer [deep-reverse]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-27 :refer [deep-reverse]])) (deftest deep-reverse-test (is (= '((4 3) (2 1)) (deep-reverse '((1 2) (3 4))))) diff --git a/test/sicp/chapter_2/part_2/ex_2_28_test.clj b/test/sicp/chapter_2/part_2/ex_2_28_test.clj index 4e7d931..04db1fc 100644 --- a/test/sicp/chapter_2/part_2/ex_2_28_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_28_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-28-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-28 :refer [fringe]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-28 :refer [fringe]])) (def x '((1 2) (3 4))) diff --git a/test/sicp/chapter_2/part_2/ex_2_29_test.clj b/test/sicp/chapter_2/part_2/ex_2_29_test.clj index 8a40836..e6b577e 100644 --- a/test/sicp/chapter_2/part_2/ex_2_29_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_29_test.clj @@ -1,14 +1,15 @@ (ns sicp.chapter-2.part-2.ex-2-29-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-29 :refer [branch-has-structure? - branch-moment - branch-structure - left-branch - make-branch - make-mobile - mobile-balanced? - right-branch - total-weight]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-29 :refer [branch-has-structure? + branch-moment + branch-structure + left-branch + make-branch + make-mobile + mobile-balanced? + right-branch + total-weight]])) (def mobile-balanced (make-mobile (make-branch 5 2) (make-branch 1 10))) diff --git a/test/sicp/chapter_2/part_2/ex_2_30_test.clj b/test/sicp/chapter_2/part_2/ex_2_30_test.clj index 60890de..5f5ddc4 100644 --- a/test/sicp/chapter_2/part_2/ex_2_30_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_30_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-30-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-30 :refer [square-tree square-tree-map]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-30 :refer [square-tree square-tree-map]])) (deftest square-tree-test (is (= '(1 (4 (9 16) 25) (36 49)) (square-tree '(1 (2 (3 4) 5) (6 7)))))) diff --git a/test/sicp/chapter_2/part_2/ex_2_31_test.clj b/test/sicp/chapter_2/part_2/ex_2_31_test.clj index 0cb9d03..f97e3d2 100644 --- a/test/sicp/chapter_2/part_2/ex_2_31_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_31_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-2.ex-2-31-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-31 :refer [square-tree tree-map]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-31 :refer [square-tree tree-map]] + [sicp.misc :as m])) (deftest square-tree-test (is (= '(1 (4 (9 16) 25) (36 49)) (square-tree '(1 (2 (3 4) 5) (6 7)))))) diff --git a/test/sicp/chapter_2/part_2/ex_2_32_test.clj b/test/sicp/chapter_2/part_2/ex_2_32_test.clj index 71e4d42..a8bea0d 100644 --- a/test/sicp/chapter_2/part_2/ex_2_32_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_32_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-32-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-32 :refer [subsets]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-32 :refer [subsets]])) (deftest subsets-test (is (= '(() (3) (2) (2 3) (1) (1 3) (1 2) (1 2 3)) (subsets '(1 2 3))))) diff --git a/test/sicp/chapter_2/part_2/ex_2_33_test.clj b/test/sicp/chapter_2/part_2/ex_2_33_test.clj index f83c335..42262ee 100644 --- a/test/sicp/chapter_2/part_2/ex_2_33_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_33_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-33-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-33 :refer [my-map my-length my-append]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-33 :refer [my-map my-length my-append]])) (deftest my-map-test (is (= '(1 2 3) (my-map inc '(0 1 2))))) diff --git a/test/sicp/chapter_2/part_2/ex_2_34_test.clj b/test/sicp/chapter_2/part_2/ex_2_34_test.clj index 96145fc..c5daaf2 100644 --- a/test/sicp/chapter_2/part_2/ex_2_34_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_34_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-34-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-34 :refer [horner-eval]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-34 :refer [horner-eval]])) (deftest horner-eval-test (is (= 79 (horner-eval 2 '(1 3 0 5 0 1))))) diff --git a/test/sicp/chapter_2/part_2/ex_2_35_test.clj b/test/sicp/chapter_2/part_2/ex_2_35_test.clj index 601ae84..1e4ce98 100644 --- a/test/sicp/chapter_2/part_2/ex_2_35_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_35_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-35-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-35 :refer [count-leaves]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-35 :refer [count-leaves]])) (def tree (cons (list 1 2) (list 3 4))) ; ((1 2) 3 4) diff --git a/test/sicp/chapter_2/part_2/ex_2_36_test.clj b/test/sicp/chapter_2/part_2/ex_2_36_test.clj index ee024ed..c51ff39 100644 --- a/test/sicp/chapter_2/part_2/ex_2_36_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_36_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-36-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-36 :refer [accumulate-n]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-36 :refer [accumulate-n]])) (deftest accumulate-n-test (is (= '(22 26 30) (accumulate-n + 0 '((1 2 3) (4 5 6) (7 8 9) (10 11 12)))))) diff --git a/test/sicp/chapter_2/part_2/ex_2_37_test.clj b/test/sicp/chapter_2/part_2/ex_2_37_test.clj index e79d2ff..745067e 100644 --- a/test/sicp/chapter_2/part_2/ex_2_37_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_37_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-37-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-37 :refer [dot-product matrix-*-matrix matrix-*-vector transpose]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-37 :refer [dot-product matrix-*-matrix matrix-*-vector transpose]])) (deftest dot-product-test (is (= (+ 1 4) (dot-product '(1 2) '(1 2)))) diff --git a/test/sicp/chapter_2/part_2/ex_2_38_test.clj b/test/sicp/chapter_2/part_2/ex_2_38_test.clj index 38ce09c..243f227 100644 --- a/test/sicp/chapter_2/part_2/ex_2_38_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_38_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-38-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-38 :refer [fold-left fold-right]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-38 :refer [fold-left fold-right]])) (deftest fold-right-test (is (= 3/2 (fold-right / 1 (list 1 2 3)))) diff --git a/test/sicp/chapter_2/part_2/ex_2_39_test.clj b/test/sicp/chapter_2/part_2/ex_2_39_test.clj index b75ed32..c4fe2ab 100644 --- a/test/sicp/chapter_2/part_2/ex_2_39_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_39_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-39-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-39 :refer [reverse-left reverse-right]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-39 :refer [reverse-left reverse-right]])) (deftest reverse-right-test (is (= '(3 2 1) (reverse-right '(1 2 3))))) diff --git a/test/sicp/chapter_2/part_2/ex_2_40_test.clj b/test/sicp/chapter_2/part_2/ex_2_40_test.clj index 3d32109..c72beb3 100644 --- a/test/sicp/chapter_2/part_2/ex_2_40_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_40_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-40-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-40 :refer [prime-sum-pairs]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-40 :refer [prime-sum-pairs]])) (deftest prime-sum-pairs-test (is (= '((2 1 3) diff --git a/test/sicp/chapter_2/part_2/ex_2_41_test.clj b/test/sicp/chapter_2/part_2/ex_2_41_test.clj index c00c1ad..3ad7e43 100644 --- a/test/sicp/chapter_2/part_2/ex_2_41_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_41_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-41-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-41 :refer [duplets find-triplets triplets]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-41 :refer [duplets find-triplets triplets]])) (deftest duplets-test (is (= '((1 1)) (duplets 1))) diff --git a/test/sicp/chapter_2/part_2/ex_2_42_test.clj b/test/sicp/chapter_2/part_2/ex_2_42_test.clj index c22b1d5..08bfd5b 100644 --- a/test/sicp/chapter_2/part_2/ex_2_42_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_42_test.clj @@ -1,12 +1,13 @@ (ns sicp.chapter-2.part-2.ex-2-42-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-42 :refer [adjoin-position - attacks? - make-pos - pos-col - pos-row - queens - safe?]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-42 :refer [adjoin-position + attacks? + make-pos + pos-col + pos-row + queens + safe?]])) (deftest make-pos-test (is (= '(1 2) (make-pos 1 2)))) diff --git a/test/sicp/chapter_2/part_2/ex_2_43_test.clj b/test/sicp/chapter_2/part_2/ex_2_43_test.clj index 6abd7e4..287fc1f 100644 --- a/test/sicp/chapter_2/part_2/ex_2_43_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_43_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-43-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-43 :refer [queens-2-42 queens-2-43]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-43 :refer [queens-2-42 queens-2-43]])) (deftest queens-2-42-test (is (= 10 (count (queens-2-42 5)))) ; Elapsed time: 6.745917 msecs diff --git a/test/sicp/chapter_2/part_2/ex_2_44_test.clj b/test/sicp/chapter_2/part_2/ex_2_44_test.clj index 361612f..29efb97 100644 --- a/test/sicp/chapter_2/part_2/ex_2_44_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_44_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-44-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-44 :refer [up-split]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-44 :refer [up-split]])) (deftest up-split-test (is (= nil (up-split 1 1)))) ; I can't cover it with unit tests. :( diff --git a/test/sicp/chapter_2/part_2/ex_2_45_test.clj b/test/sicp/chapter_2/part_2/ex_2_45_test.clj index fb24fdc..cf2b8e1 100644 --- a/test/sicp/chapter_2/part_2/ex_2_45_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_45_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-45-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-45 :refer [right-split up-split]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-45 :refer [right-split up-split]])) ; I can't cover it with real unit tests. :( diff --git a/test/sicp/chapter_2/part_2/ex_2_46_test.clj b/test/sicp/chapter_2/part_2/ex_2_46_test.clj index ccca670..29ec7a0 100644 --- a/test/sicp/chapter_2/part_2/ex_2_46_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_46_test.clj @@ -1,11 +1,13 @@ (ns sicp.chapter-2.part-2.ex-2-46-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-46 :refer [add-vect - make-vect - scale-vect - sub-vect - xcor-vect - ycor-vect]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-46 :refer [add-vect + make-vect + scale-vect + sub-vect + xcor-vect + ycor-vect]])) + (def vec-1 (make-vect 1 2)) (def vec-2 (make-vect 3 4)) diff --git a/test/sicp/chapter_2/part_2/ex_2_47_test.clj b/test/sicp/chapter_2/part_2/ex_2_47_test.clj index bcfccf0..b376645 100644 --- a/test/sicp/chapter_2/part_2/ex_2_47_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_47_test.clj @@ -1,12 +1,14 @@ (ns sicp.chapter-2.part-2.ex-2-47-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46] - [sicp.chapter-2.part-2.ex-2-47 :refer [edge1-frame - edge2-frame - edge2-frame-2 - make-frame - make-frame-2 - origin-frame]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-46 :as ex-2-46] + [sicp.chapter-2.part-2.ex-2-47 :refer [edge1-frame + edge2-frame + edge2-frame-2 + make-frame + make-frame-2 + origin-frame]])) + (def vec-1 (ex-2-46/make-vect 0 0)) (def vec-2 (ex-2-46/make-vect 0.1 0.2)) (def vec-3 (ex-2-46/make-vect 0.5 0.75)) diff --git a/test/sicp/chapter_2/part_2/ex_2_48_test.clj b/test/sicp/chapter_2/part_2/ex_2_48_test.clj index edf602c..bc8b4c0 100644 --- a/test/sicp/chapter_2/part_2/ex_2_48_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_48_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-48-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-48 :refer [end-segment make-segment start-segment]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-48 :refer [end-segment make-segment start-segment]])) (def seg-1 (make-segment 1 2)) (def seg-2 (make-segment 3 4)) diff --git a/test/sicp/chapter_2/part_2/ex_2_49_test.clj b/test/sicp/chapter_2/part_2/ex_2_49_test.clj index ddbb031..a613749 100644 --- a/test/sicp/chapter_2/part_2/ex_2_49_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_49_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-49-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-49 :refer [outline-painter x-painter diamond-painter]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-49 :refer [outline-painter x-painter diamond-painter]])) ; I don't know to test it properly... diff --git a/test/sicp/chapter_2/part_2/ex_2_50_test.clj b/test/sicp/chapter_2/part_2/ex_2_50_test.clj index e0ceadb..1f625ec 100644 --- a/test/sicp/chapter_2/part_2/ex_2_50_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_50_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-50-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-50 :refer [flip-horiz rotate-180-cc rotate-270-cc]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-50 :refer [flip-horiz rotate-180-cc rotate-270-cc]])) ; I don't know to test it properly... diff --git a/test/sicp/chapter_2/part_2/ex_2_51_test.clj b/test/sicp/chapter_2/part_2/ex_2_51_test.clj index 59d792c..c0d372e 100644 --- a/test/sicp/chapter_2/part_2/ex_2_51_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_51_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-51-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-51 :refer [below below-v2]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-51 :refer [below below-v2]])) ; I don't know to test it properly... diff --git a/test/sicp/chapter_2/part_2/ex_2_52_test.clj b/test/sicp/chapter_2/part_2/ex_2_52_test.clj index b838393..4a3e293 100644 --- a/test/sicp/chapter_2/part_2/ex_2_52_test.clj +++ b/test/sicp/chapter_2/part_2/ex_2_52_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-2.ex-2-52-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-2.ex-2-52 :refer [corner-split]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-2.ex-2-52 :refer [corner-split]])) ; I don't know to test it properly... diff --git a/test/sicp/chapter_2/part_3/book_2_3_test.clj b/test/sicp/chapter_2/part_3/book_2_3_test.clj index cd232e4..d3ca603 100644 --- a/test/sicp/chapter_2/part_3/book_2_3_test.clj +++ b/test/sicp/chapter_2/part_3/book_2_3_test.clj @@ -1,45 +1,43 @@ (ns sicp.chapter-2.part-3.book-2-3-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.book-2-3 :refer [adjoin-set - adjoin-set-h - deriv - element-of-set-sorted? - element-of-set-tree? - element-of-set? - entry - huffman-tree - huffman-tree-as-list - intersection-set - intersection-set-sorted - leaf? - left-branch - left-branch-h - lookup - make-code-tree - make-leaf - make-leaf-set - make-tree - memq - right-branch - right-branch-h - symbol-leaf - symbols - weight - weight-leaf]])) - -(comment "2.3") -; symbolic data ------------------------------------------------------------------------------------ - -(comment "2.3.1") -; quotation ---------------------------------------------------------------------------------------- + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.book-2-3 :refer [adjoin-set + adjoin-set-h + deriv + element-of-set-sorted? + element-of-set-tree? + element-of-set? + entry + huffman-tree + huffman-tree-as-list + intersection-set + intersection-set-sorted + leaf? + left-branch + left-branch-h + lookup + make-code-tree + make-leaf + make-leaf-set + make-tree + memq + right-branch + right-branch-h + symbol-leaf + symbols + weight + weight-leaf]])) + +(comment "2.3 Symbolic data ----------------------------------------------------------------------") + +(comment "2.3.1 Quotation ------------------------------------------------------------------------") (deftest memq-test (is (= false (memq 'apple '(pear banana prune)))) (is (= '(pear banana prune) (memq 'pear '(pear banana prune)))) (is (= '(apple pear) (memq 'apple '(x (apple sauce) y apple pear))))) -(comment "2.3.2") -; example: symbolic differentiation ---------------------------------------------------------------- +(comment "2.3.2 Example: symbolic differentiation ------------------------------------------------") (deftest deriv-test (is (= 1 (deriv '(+ x 3) 'x))) @@ -47,8 +45,7 @@ (is (= '(+ (* x y) (* y (+ x 3))) (deriv '(* (* x y) (+ x 3)) 'x)))) -(comment "2.3.3") -; example: representing sets ----------------------------------------------------------------------- +(comment "2.3.3 Example: representing sets -------------------------------------------------------") (deftest element-of-set?-test (is (= true (element-of-set? 1 '(1 2 3)))) @@ -118,8 +115,7 @@ (is (= '{:key 3 :value "C"} (lookup 3 records))) (is (= false (lookup 4 records))))) -(comment "2.3.4") -; Example: Huffman Encoding Trees ------------------------------------------------------------------ +(comment "2.3.4 Example: Huffman Encoding Trees --------------------------------------------------") (def sample-tree (make-code-tree diff --git a/test/sicp/chapter_2/part_3/ex_2_53_test.clj b/test/sicp/chapter_2/part_3/ex_2_53_test.clj index 48c8ddf..cbc2ee7 100644 --- a/test/sicp/chapter_2/part_3/ex_2_53_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_53_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-3.ex-2-53-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-53 :refer [print-vars]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-53 :refer [print-vars]])) (deftest print-vars-test (is (= ['(a b c) diff --git a/test/sicp/chapter_2/part_3/ex_2_54_test.clj b/test/sicp/chapter_2/part_3/ex_2_54_test.clj index 42308bb..368de2f 100644 --- a/test/sicp/chapter_2/part_3/ex_2_54_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_54_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-3.ex-2-54-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-54 :refer [equal?]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-54 :refer [equal?]])) (deftest equal?-test (is (= true (equal? '(this is a list) diff --git a/test/sicp/chapter_2/part_3/ex_2_55_test.clj b/test/sicp/chapter_2/part_3/ex_2_55_test.clj index 098ede3..3022246 100644 --- a/test/sicp/chapter_2/part_3/ex_2_55_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_55_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-3.ex-2-55-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-55 :refer [print-test]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-55 :refer [print-test]])) (deftest print-test-test (is (= 'quote (print-test)))) diff --git a/test/sicp/chapter_2/part_3/ex_2_56_test.clj b/test/sicp/chapter_2/part_3/ex_2_56_test.clj index 81af799..9a8c2a6 100644 --- a/test/sicp/chapter_2/part_3/ex_2_56_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_56_test.clj @@ -1,10 +1,11 @@ (ns sicp.chapter-2.part-3.ex-2-56-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-56 :refer [base - deriv - exponent - exponentiation? - make-exponentiation]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-56 :refer [base + deriv + exponent + exponentiation? + make-exponentiation]])) (deftest exponentiation?-test (is (= true (exponentiation? '(** 1 1)))) diff --git a/test/sicp/chapter_2/part_3/ex_2_57_test.clj b/test/sicp/chapter_2/part_3/ex_2_57_test.clj index 1544cb0..1dd86ca 100644 --- a/test/sicp/chapter_2/part_3/ex_2_57_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_57_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-3.ex-2-57-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-57 :refer [augend deriv make-product make-sum multiplicand]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-57 :refer [augend deriv make-product make-sum multiplicand]])) (deftest augend-test (is (= 5 (augend '(+ 1 2 3))))) diff --git a/test/sicp/chapter_2/part_3/ex_2_58_test.clj b/test/sicp/chapter_2/part_3/ex_2_58_test.clj index 777b144..0b1d226 100644 --- a/test/sicp/chapter_2/part_3/ex_2_58_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_58_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-3.ex-2-58-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-58 :refer [deriv]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-58 :refer [deriv]])) (deftest deriv-test (is (= '((x * y) + (y * (x + 3))) ; (+ (* x y) (* y (+ x 3))) diff --git a/test/sicp/chapter_2/part_3/ex_2_59_test.clj b/test/sicp/chapter_2/part_3/ex_2_59_test.clj index fc66fd8..750b39a 100644 --- a/test/sicp/chapter_2/part_3/ex_2_59_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_59_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-3.ex-2-59-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-59 :refer [union-set]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-59 :refer [union-set]])) (deftest union-set-test (is (= '() (union-set '() '()))) diff --git a/test/sicp/chapter_2/part_3/ex_2_60_test.clj b/test/sicp/chapter_2/part_3/ex_2_60_test.clj index 8d27477..e375827 100644 --- a/test/sicp/chapter_2/part_3/ex_2_60_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_60_test.clj @@ -1,9 +1,10 @@ (ns sicp.chapter-2.part-3.ex-2-60-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-60 :refer [adjoin-set - element-of-set? - intersection-set - union-set]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-60 :refer [adjoin-set + element-of-set? + intersection-set + union-set]])) (deftest element-of-set?-test (is (= false (element-of-set? 1 '()))) diff --git a/test/sicp/chapter_2/part_3/ex_2_61_test.clj b/test/sicp/chapter_2/part_3/ex_2_61_test.clj index f948695..4eba468 100644 --- a/test/sicp/chapter_2/part_3/ex_2_61_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_61_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-3.ex-2-61-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-61 :refer [adjoin-set-sorted]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-61 :refer [adjoin-set-sorted]])) (deftest adjoin-set-sorted-test (is (= '(1) (adjoin-set-sorted 1 '()))) diff --git a/test/sicp/chapter_2/part_3/ex_2_62_test.clj b/test/sicp/chapter_2/part_3/ex_2_62_test.clj index 3001c47..3f8efb1 100644 --- a/test/sicp/chapter_2/part_3/ex_2_62_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_62_test.clj @@ -1,6 +1,7 @@ (ns sicp.chapter-2.part-3.ex-2-62-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-62 :refer [union-set-sorted]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-62 :refer [union-set-sorted]])) (deftest union-set-sorted-test (is (= '() (union-set-sorted '() '()))) diff --git a/test/sicp/chapter_2/part_3/ex_2_63_test.clj b/test/sicp/chapter_2/part_3/ex_2_63_test.clj index 0119726..2b0c24d 100644 --- a/test/sicp/chapter_2/part_3/ex_2_63_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_63_test.clj @@ -1,9 +1,12 @@ (ns sicp.chapter-2.part-3.ex-2-63-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.book-2-3 :as b23] - [sicp.chapter-2.part-3.ex-2-63 :refer [tree->list-1 tree->list-2]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.book-2-3 :as b23] + [sicp.chapter-2.part-3.ex-2-63 :refer [tree->list-1 tree->list-2]])) -(defn tree-empty [x] (b23/make-tree x '() '())) +(defn tree-empty + [x] + (b23/make-tree x '() '())) ; (7 (3 (1 () ()) (5 () ())) (9 () (11))) (def tree1 diff --git a/test/sicp/chapter_2/part_3/ex_2_64_test.clj b/test/sicp/chapter_2/part_3/ex_2_64_test.clj index 6c60f4b..10fa513 100644 --- a/test/sicp/chapter_2/part_3/ex_2_64_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_64_test.clj @@ -1,9 +1,12 @@ (ns sicp.chapter-2.part-3.ex-2-64-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.book-2-3 :as b23] - [sicp.chapter-2.part-3.ex-2-64 :refer [list->tree]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.book-2-3 :as b23] + [sicp.chapter-2.part-3.ex-2-64 :refer [list->tree]])) -(defn tree-empty [x] (b23/make-tree x '() '())) +(defn tree-empty + [x] + (b23/make-tree x '() '())) (def expected-tree (b23/make-tree 5 diff --git a/test/sicp/chapter_2/part_3/ex_2_65_test.clj b/test/sicp/chapter_2/part_3/ex_2_65_test.clj index 92d6ac8..2bbeced 100644 --- a/test/sicp/chapter_2/part_3/ex_2_65_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_65_test.clj @@ -1,8 +1,9 @@ (ns sicp.chapter-2.part-3.ex-2-65-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-63 :refer [tree->list-2]] - [sicp.chapter-2.part-3.ex-2-64 :refer [list->tree]] - [sicp.chapter-2.part-3.ex-2-65 :refer [intersection-set-tree union-set-tree]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-63 :refer [tree->list-2]] + [sicp.chapter-2.part-3.ex-2-64 :refer [list->tree]] + [sicp.chapter-2.part-3.ex-2-65 :refer [intersection-set-tree union-set-tree]])) (def tree1 (list->tree '(1 2 3))) (def tree2 (list->tree '(1 2 4))) diff --git a/test/sicp/chapter_2/part_3/ex_2_66_test.clj b/test/sicp/chapter_2/part_3/ex_2_66_test.clj index 12d8ff2..3808ec1 100644 --- a/test/sicp/chapter_2/part_3/ex_2_66_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_66_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-3.ex-2-66-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-64 :refer [list->tree]] - [sicp.chapter-2.part-3.ex-2-66 :refer [lookup-tree]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-64 :refer [list->tree]] + [sicp.chapter-2.part-3.ex-2-66 :refer [lookup-tree]])) (def tree (list->tree (range 10))) diff --git a/test/sicp/chapter_2/part_3/ex_2_67_test.clj b/test/sicp/chapter_2/part_3/ex_2_67_test.clj index c492979..4e65f7f 100644 --- a/test/sicp/chapter_2/part_3/ex_2_67_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_67_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-3.ex-2-67-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.book-2-3 :as b23] - [sicp.chapter-2.part-3.ex-2-67 :refer [decode-message]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.book-2-3 :as b23] + [sicp.chapter-2.part-3.ex-2-67 :refer [decode-message]])) (deftest decode-message-test (is (= b23/huffman-message-encoded diff --git a/test/sicp/chapter_2/part_3/ex_2_68_test.clj b/test/sicp/chapter_2/part_3/ex_2_68_test.clj index 0309026..d43c251 100644 --- a/test/sicp/chapter_2/part_3/ex_2_68_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_68_test.clj @@ -1,7 +1,8 @@ (ns sicp.chapter-2.part-3.ex-2-68-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.book-2-3 :as b23] - [sicp.chapter-2.part-3.ex-2-68 :refer [encode encode-symbol]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.book-2-3 :as b23] + [sicp.chapter-2.part-3.ex-2-68 :refer [encode encode-symbol]])) (deftest encode-symbol-test (is (= b23/huffman-A (encode-symbol :A b23/huffman-tree))) @@ -24,10 +25,10 @@ (deftest encode-test (is (= '(0 ; A - 1 1 0 ; D - 0 ; A - 1 0 ; B - 1 0 ; B - 1 1 1 ; C - 0) ; A + 1 1 0 ; D + 0 ; A + 1 0 ; B + 1 0 ; B + 1 1 1 ; C + 0) ; A (encode b23/huffman-message-encoded b23/huffman-tree)))) diff --git a/test/sicp/chapter_2/part_3/ex_2_69_test.clj b/test/sicp/chapter_2/part_3/ex_2_69_test.clj index 84a1c94..8460d4a 100644 --- a/test/sicp/chapter_2/part_3/ex_2_69_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_69_test.clj @@ -1,8 +1,9 @@ (ns sicp.chapter-2.part-3.ex-2-69-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.book-2-3 :as b23] - [sicp.chapter-2.part-3.ex-2-68 :refer [encode-symbol]] - [sicp.chapter-2.part-3.ex-2-69 :refer [generate-huffman-tree]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.book-2-3 :as b23] + [sicp.chapter-2.part-3.ex-2-68 :refer [encode-symbol]] + [sicp.chapter-2.part-3.ex-2-69 :refer [generate-huffman-tree]])) (deftest generate-huffman-tree-test (is (= b23/huffman-tree (generate-huffman-tree b23/huffman-pairs)))) diff --git a/test/sicp/chapter_2/part_3/ex_2_70_test.clj b/test/sicp/chapter_2/part_3/ex_2_70_test.clj index 01fbe7a..af3d9f5 100644 --- a/test/sicp/chapter_2/part_3/ex_2_70_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_70_test.clj @@ -1,8 +1,9 @@ (ns sicp.chapter-2.part-3.ex-2-70-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-3.ex-2-68 :as ex-2-68] - [sicp.chapter-2.part-3.ex-2-69 :as ex-2-69] - [sicp.chapter-2.part-3.ex-2-70 :refer [abc song]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-3.ex-2-68 :as ex-2-68] + [sicp.chapter-2.part-3.ex-2-69 :as ex-2-69] + [sicp.chapter-2.part-3.ex-2-70 :refer [abc song]])) (deftest abc-test (is (= '[[:leaf :na 16] @@ -42,10 +43,10 @@ (deftest song-encode-test (is (= '(1 1 1 1 1 1 1 0 0 1 1 1 1 0 1 1 1 - 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 - 0 1 1 1 1 0 1 1 1 0 0 0 0 0 0 0 0 - 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 - 0 1 0 1 0 1 0 1 1 1 0 1 1 0 1 1) + 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 + 0 1 1 1 1 0 1 1 1 0 0 0 0 0 0 0 0 + 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 + 0 1 0 1 0 1 0 1 1 1 0 1 1 0 1 1) (ex-2-68/encode song (ex-2-69/generate-huffman-tree abc)))) ; (~11 bytes) 84 bits insted of 124 charates OR 992 bits! ; 992/84 = ~11.8 times smaller diff --git a/test/sicp/chapter_2/part_5/book_2_5_test.clj b/test/sicp/chapter_2/part_5/book_2_5_test.clj index 32a56da..de4a2c1 100644 --- a/test/sicp/chapter_2/part_5/book_2_5_test.clj +++ b/test/sicp/chapter_2/part_5/book_2_5_test.clj @@ -1,13 +1,14 @@ (ns sicp.chapter-2.part-5.book-2-5-test - (:require [clojure.test :refer [deftest is]] - [sicp.chapter-2.part-5.book-2-5 :refer [add - div - install-rational-package - install-scheme-number-package - make-rational - make-scheme-number - mul - sub]])) + (:require + [clojure.test :refer [deftest is]] + [sicp.chapter-2.part-5.book-2-5 :refer [add + div + install-rational-package + install-scheme-number-package + make-rational + make-scheme-number + mul + sub]])) (install-scheme-number-package) (install-rational-package) diff --git a/test/sicp/misc_test.clj b/test/sicp/misc_test.clj index bf4b12a..8243957 100644 --- a/test/sicp/misc_test.clj +++ b/test/sicp/misc_test.clj @@ -1,9 +1,9 @@ (ns sicp.misc-test - (:require [clojure.test :refer [deftest is]] - [sicp.misc :as m])) + (:require + [clojure.test :refer [deftest is]] + [sicp.misc :as m])) -(comment "Chapter #1") -; The Elements of Programming ---------------------------------------------------------------------- +(comment "Chapter #1 The Elements of Programming -------------------------------------------------") (deftest error-test (try @@ -94,8 +94,7 @@ (is (= 10.55 (m/round-to-dec 10.55 2))) (is (= 11.0 (m/round-to-dec 10.55 0)))) -(comment "Chapter #2") -; Introduction to Data Abstraction ----------------------------------------------------------------- +(comment "Chapter #2 Introduction to Data Abstraction --------------------------------------------") (deftest pair-test (is (= '(1 2) (m/pair 1 2)))