Last active
November 9, 2020 17:42
-
-
Save mszajna/0520e193b45a7d22f5878fb5e1554f70 to your computer and use it in GitHub Desktop.
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://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