Last active
August 29, 2015 13:56
-
-
Save cronin101/9328815 to your computer and use it in GitHub Desktop.
Y Combinator Example in Clojure
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
| (defn Y [f] | |
| ((fn [x] (x x)) | |
| (fn [x] | |
| (f (fn [& args] | |
| (apply (x x) args)))))) | |
| ;; Recursive step for apply_lots function: | |
| ; Return accumulator if no iterations remaning otherwise call self with modified stack. | |
| (defn lots_step [partial_fn] | |
| (fn [f r a] | |
| (if (= 0 r) | |
| a ; Base Case | |
| (partial_fn f (- r 1) (f a))))) ; Loop | |
| ;; apply_lots [function times_to_apply intitial_value] | |
| (let [ apply_lots (Y lots_step) | |
| square_it (fn [x] (* x x))] | |
| (prn | |
| (apply_lots square_it 4 2))) | |
| ;; > 65536 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment