3 ms·
The Haskell comparision is I think wrong (from my limited knowledge of Haskell, rather than C++). He is folding over a tuple of different types, rather than an
by CJefferson 14y ago
The Haskell comparision is I think wrong (from my limited knowledge of Haskell, rather than C++). He is folding over a tuple of different types, rather than an array, allowing the return type of each tuple operation to be different. Not sure where this would be useful of course.
Doing foldl (+) 0 in C++ is done traditionally with something like (OK, still not that nice)
std::accumulate(v.begin(), v.end(), std::plus<int>(), 0)
- Evbn 14y agoTo make it work in Haskell you would need to wrap the elements in a FoldElement type and the functions in a FoldFunction type, and then allow not total functions or some Either/Maybe style defaulting, and use case analysis to unpack each FoldElement. It could be useful if you are building a general framework (like a calculator, or a game AI decision engine) for user-defined functions.