Skip to content

Instantly share code, notes, and snippets.

@rex-sheridan
Created August 5, 2023 11:40
Show Gist options
  • Save rex-sheridan/961a2babd4579892641f257ceee31d85 to your computer and use it in GitHub Desktop.
Save rex-sheridan/961a2babd4579892641f257ceee31d85 to your computer and use it in GitHub Desktop.
Y Combinator in Clojure
(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