Skip to content

Instantly share code, notes, and snippets.

@mszajna
Last active November 9, 2020 17:42
Show Gist options
  • Save mszajna/0520e193b45a7d22f5878fb5e1554f70 to your computer and use it in GitHub Desktop.
Save mszajna/0520e193b45a7d22f5878fb5e1554f70 to your computer and use it in GitHub Desktop.
; https://stackoverflow.com/questions/4899113/fixed-point-combinator-for-mutually-recursive-functions
(defn Y* [& fs]
(map (fn [f] (f))
((fn [x] (x x))
(fn [p]
(map
(fn [f]
(fn []
(apply f
(map
(fn [ff]
(fn [& y] (apply (ff) y)))
(p p)))))
fs)))))
(defmacro my-letfn* [bindings & body]
(let [bindings (partition 2 bindings)
names (map first bindings)
fns (map second bindings)]
`(let [[~@names] (Y* ~@(for [f fns] `(fn [~@names] ~f)))]
~@body)))
(my-letfn* [my-even? (fn [n] (or (= 0 n) (my-odd? (dec n))))
my-odd? (fn [n] (and (not= 0 n) (my-even? (dec n))))]
(even? 122)) ; => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment