Skip to content

Instantly share code, notes, and snippets.

@lhrb
Last active December 6, 2021 19:03
Show Gist options
  • Save lhrb/93e3471182560945d0576a1fe4742d02 to your computer and use it in GitHub Desktop.
Save lhrb/93e3471182560945d0576a1fe4742d02 to your computer and use it in GitHub Desktop.
Haskell Curry's Y Combinator
;; https://en.wikipedia.org/wiki/Fixed-point_combinator#Fixed-point_combinators_in_lambda_calculus
(defn ycombinator [f]
((fn [x]
(x x))
(fn [x]
(f (fn [y]
((x x) y))))))
(defn factorial [f]
(fn [n]
(if (zero? n)
1
(* n (f (dec n))))))
((ycombinator factorial) 5) ;; => 120
(defn fib [f]
(fn [n]
(cond (zero? n) 0
(= 1 n) 1
:else (+ (f (dec n)) (f (- n 2))))))
((ycombinator fib) 20) ;; => 6765
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment