4 ms·
>I will use the language Haskell, but it is possible to quickly translate the programs to e.g. ML or OCaml. The article predates C++11 by many years, and with
by hellofunk 11y ago
>I will use the language Haskell, but it is possible to quickly translate the programs to e.g. ML or OCaml.
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++.
- fakedrake 11y agoI wonder what the state of tail call optimization is in mainstream c++ compilers these days.
- lhecker 11y agoAFAIK 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.
- lmm 11y agoC++ 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).
- hellofunk 11y agoTagged 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.