Created
December 14, 2010 20:42
-
-
Save citizen428/741070 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns step1) | |
(defn fact | |
[n] | |
(if (< n 2) 1 (* n (fact (dec n))))) | |
;; (fact 5) => 120 |
This file contains hidden or 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
(ns step2) | |
(defn fact | |
[f] | |
(fn [n] | |
(if (< n 2) 1 (* n ((f f) (dec n)))))) | |
;; ((fact fact) 5) => 120 |
This file contains hidden or 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
(ns step3) | |
(defn yrecur | |
[f] | |
(f f)) | |
(def fact | |
(yrecur (fn [f] | |
(fn [n] | |
(if (< n 2) 1 (* n ((f f) (dec n)))))))) | |
;; (fact 5) => 120 |
This file contains hidden or 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
(ns step4) | |
(defn yrecur | |
[f] | |
(f f)) | |
(def fact | |
(yrecur | |
(fn [f] | |
(let [g (fn [n] ((f f) n))] | |
(fn [n] | |
(if (< n 2) 1 (* n (g (dec n))))))))) | |
;; (fact 5) => 120 |
This file contains hidden or 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
(ns step5) | |
(defn yrecur | |
[f] | |
(f f)) | |
(defn wrap | |
[h] | |
(yrecur | |
(fn [f] | |
(let [g (fn [n] ((f f) n))] | |
(h g))))) | |
(def fact | |
(wrap | |
(fn [g] | |
(fn [n] | |
(if (< n 2) 1 (* n (g (dec n)))))))) | |
;; (fact 5) => 120 |
This file contains hidden or 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
(ns step6) | |
(defn yrecur | |
[f] | |
(f f)) | |
(defn wrap | |
[h] | |
(yrecur | |
(fn [f] | |
(h (fn [n] ((f f) n)))))) | |
(def fact | |
(wrap | |
(fn [g] | |
(fn [n] | |
(if (< n 2) 1 (* n (g (dec n)))))))) | |
;; (fact 5) => 120 |
This file contains hidden or 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
(ns step7) | |
(defn Y | |
[h] | |
((fn [f] (f f)) (fn [f] | |
(h (fn [n] | |
((f f) n)))))) | |
(def fact | |
(Y (fn [g] | |
(fn [n] | |
(if (< n 2) 1 (* n (g (dec n)))))))) | |
;; (fact 5) => 120 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment