Last active
December 6, 2021 19:03
-
-
Save lhrb/93e3471182560945d0576a1fe4742d02 to your computer and use it in GitHub Desktop.
Haskell Curry's Y Combinator
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
;; 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