News for hackers. Stuff that matters.
Re:Church Numerals in C++ (and Haskell) by cgranade on Saturday April 24, 2010 @03:32PM (#1291438)
Great argument as to why C++, though it works for functional programming in some technical sense, is far less than ideal.
Re:Church Numerals in C++ (and Haskell) by Niten on Saturday April 24, 2010 @06:13PM (#1291661)
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.