Skip to content

Instantly share code, notes, and snippets.

@cronin101
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save cronin101/9328815 to your computer and use it in GitHub Desktop.

Select an option

Save cronin101/9328815 to your computer and use it in GitHub Desktop.
Y Combinator Example in Clojure
(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