Created
May 8, 2011 22:31
-
-
Save fredhsu/961754 to your computer and use it in GitHub Desktop.
SICP 1.2 in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] | |
(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] | |
(cc amount 5)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn f-recur [n] | |
(cond (< n 3) n | |
:else (+ (f-recur (- n 1)) (* 2 (f-recur (- n 2))) (* 3 (f-recur (- n 3)))))) | |
(defn f-iter [n] | |
(defn iter [n1 n2 n3 count] | |
(if (= count 0) | |
n1 | |
(recur n2 n3 (+ n3 (* 2 n2) (* 3 n1)) (- count 1)))) | |
(iter 0 1 2 n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn pascal [row col] | |
;Gives the pascal triangle value for a given row and column | |
(if (or (= col 1) (= row col)) 1 | |
(+ (pascal (- row 1) (- col 1)) (pascal (- row 1) col)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn expt-iter [b n a] | |
(cond | |
(= n 0) a | |
(even? n) (recur (* b b) (/ n 2) a) | |
:else (recur b (- n 1) (* a b)))) | |
(defn expt [b n] | |
(expt-iter b n 1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn halve [a] (/ a 2)) | |
(defn dbl [a] (+ a a)) | |
(defn mult [a b] | |
(cond | |
(= a 0) 0 | |
(even? a) (recur (halve a) (dbl b)) | |
:else (+ b (mult (- a 1) b )))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn halve [a] (/ a 2)) | |
(defn dbl [a] (+ a a)) | |
(defn mult-iter [a b acc] | |
(cond | |
(= a 0) acc | |
(even? a) (recur (halve a) (dbl b) acc) | |
:else (mult-iter (- a 1) b (+ b acc)))) | |
(defn mult [a b] | |
(mult-iter a b 0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn square [n] (* n n)) | |
(defn fib-iter [a b p q cnt] | |
(cond (= cnt 0) b | |
(even? cnt) (recur a | |
b | |
(+ (square p) (square q)) | |
(+ (* 2 p q) (square q)) | |
(/ cnt 2)) | |
:else (recur (+ (* b q) (* a q) (* a p)) | |
(+ (* b p) (* a q)) | |
p | |
q | |
(- cnt 1)))) | |
(defn fib [n] | |
(fib-iter 1 0 0 1 n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn divides? [a b] | |
(= (rem b a ) 0)) | |
(defn square [x] (* x x)) | |
(defn find-divisor [n test-divisor] | |
(cond (> (square test-divisor) n) n | |
(divides? test-divisor n) test-divisor | |
:else (find-divisor n (+ test-divisor 1)))) | |
(defn smallest-divisor [n] | |
(find-divisor n 2)) | |
(smallest-divisor 199) | |
(smallest-divisor 1999) | |
(smallest-divisor 19999) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn gcd [a b] | |
(if (= b 0) | |
a | |
(recur b (rem a b)))) | |
(gcd 206 40) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn divides? [a b] | |
(= (rem b a ) 0)) | |
(defn square [x] (* x x)) | |
(defn find-divisor [n test-divisor] | |
(cond (> (square test-divisor) n) n | |
(divides? test-divisor n) test-divisor | |
:else (find-divisor n (+ test-divisor 1)))) | |
(defn smallest-divisor [n] | |
(find-divisor n 2)) | |
(defn prime? [n] | |
(= n (smallest-divisor n))) | |
(smallest-divisor 199) | |
(smallest-divisor 1999) | |
(smallest-divisor 19999) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment