2 ms·
If you want to be picky, you might even say this doesn't really implement Church numerals in C++ (it implements them in the C++ compiler), since the conversion
by Niten 16y ago
If you want to be picky, you might even say this doesn't really implement Church numerals in C++ (it implements them in the C++ compiler), since the conversion between integers and Church numerals takes place in compile time template expansion. What happens if you want to throw a new integer at this program at runtime?
> You might have to pass -ftemplate-depth-1024 to g++ to get this compiled.
Yeah, no kidding. Semantic arguments aside, this made my head hurt, and I've actually done a reasonable amount of C++ programming.
For comparison, here are what Church numerals look like in Clojure, another language that (like Haskell) has first-class higher order functions:
(defn church [n]
(cond (= n 0) (fn [f x] x)
(> n 0) (fn [f x] (f ((church (dec n)) f x)))))
(defn unchurch [n]
(n inc 0))
And it works at runtime.