Created
August 5, 2023 11:40
-
-
Save rex-sheridan/961a2babd4579892641f257ceee31d85 to your computer and use it in GitHub Desktop.
Y Combinator in Clojure
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
(ns ycombinator) | |
;; Based on https://blog.klipse.tech/lambda/2016/08/07/pure-y-combinator-clojure.html | |
(defn factorial [n] | |
(if (zero? n) | |
1 | |
(* n (factorial (dec n))))) | |
(defn factorial-gen [func] | |
(fn [n] | |
(if (zero? n) | |
1 | |
(* n (func (dec n)))))) | |
(def Y-combinator (fn [f] ((fn [x] (x x)) | |
(fn [x] (f (fn [y] | |
((x x) y))))))) | |
(defn Y [f] | |
((fn [x] (x x)) | |
(fn [x] | |
(f (fn [y] ((x x) y))) | |
))) | |
#_( | |
((Y factorial-gen) 19) | |
121645100408832000 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment