Re:Seemingly impossible functional programs (2007)
by lmm on Tuesday November 17, 2015 @05:00AM
(#10580114)
C++ still doesn't have (tagged) union types so you'd have to implement Maybe by hand, which is a little tedious. But I don't see anything in this code that requires laziness or the Haskell type system, so you can translate it pretty directly into pretty much any language (though as sibling mentions, in a language without TCO you might have to manually trampoline it - again tedious, and unperformant).
Re:Seemingly impossible functional programs (2007)
by hellofunk on Tuesday November 17, 2015 @05:30AM
(#10580218)
Tagged unions are a pretty common exercise in C++, they don't require much code to implement.
As for Maybe, if it is basically an "optional" as in other languages, then the newer c++17 has <optional>, if I'm not mistaken.
Re:Seemingly impossible functional programs (2007)
by fakedrake on Tuesday November 17, 2015 @04:48AM
(#10580078)
I wonder what the state of tail call optimization is in mainstream c++ compilers these days.
Re:Seemingly impossible functional programs (2007)
by lhecker on Tuesday November 17, 2015 @05:45AM
(#10580268)
AFAIK most compiler will do tail-recursion optimization if the method ends in a tail call to the current function (obviously) or to a "sibling" function (i.e. if a() calls b() which calls a() again and so on) and is sufficiently short to be suitable for inlining.
It's important to remember local objects with destructors will make this impossible, since destructors are run at the end of the function scope and thus after what's seemingly a perfect tail call.
The article predates C++11 by many years, and with functions-as-values and lambdas now in the language, would be interesting to see if it could also be translated to C++.