Created
July 23, 2012 11:13
-
-
Save martintrojer/3163126 to your computer and use it in GitHub Desktop.
Untying the Recursive Knot
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 fact' [fact n] | |
(if (= n 0) 1 | |
(* n (fact (dec 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 fact [n] | |
(if (= n 0) 1 | |
(* n (fact (dec 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 even' [odd es os [e & t]] | |
(if e | |
(odd (conj es e) os t) | |
[es os])) | |
(defn odd' [even es os [o & t]] | |
(if o | |
(even es (conj os o) t) | |
[es os])) |
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
(y (fn [f & args] (apply even' (partial odd' f) args)) [] [] (range 7)) | |
;; [[0 2 4 6] [1 3 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
(declare odd) | |
(defn even [es os [e & xs]] | |
(if e | |
(odd (conj es e) os xs) | |
[es os])) | |
(defn odd [es os [o & xs]] | |
(if o | |
(even es (conj os o) xs) | |
[es os])) | |
(even [] [] (range 7)) | |
;; [[0 2 4 6] [1 3 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
(declare odd) | |
(defn even [es os [e & xs]] | |
(if e | |
(odd (conj es e) os xs) | |
[es os])) | |
(defn odd [es os [o & xs]] | |
(if o | |
(even es (conj os o) xs) | |
[es os])) | |
(even [] [] (range 7)) | |
;; [[0 2 4 6] [1 3 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 y [f & xs] | |
(apply f (partial y f) xs)) |
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
(y (fn [f n] | |
(println "fact" n) | |
(fact' f n)) | |
5) | |
;; fact 5 | |
;; fact 4 | |
;; fact 3 | |
;; fact 2 | |
;; fact 1 | |
;; fact 0 | |
;; 120 |
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
(y fact' 10) | |
;; 3628800 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment