Functional programming in C++11, F# style
-
23-08-2019 - |
Question
I've been looking at the new features in C++11 and it really looks like it will be possible to program in a very functional programming style using it. I've gotten use to using the types List, Seq, Array in F# and I see no reason why their members couldn't be ported into some sort of C++11 template. What problems or advantages do you see in using C++11 vs something like F# for a mixed functional programming style? Maybe the Boost guys will make a new functional
once C++11 comes out.
Solution
The biggest problem with trying to program in a functional style in C++ is that it does not support tail recursion. In a functional language you don't have to worry about stack explosion when you tail recurse correctly, but in C++ you always have to worry about that. Therefore, many "functional" type algorithms will be clumsy or heavy.
OTHER TIPS
You might find this interesting:
Here are some of the problems I encountered trying to write functional code in C#, mixed with some goodies from my time when I was still using C++:
- Lack of pattern matching. Once you get used to it, not having it can drive me crazy.
- Lack of syntactic sugar for tuples.
- Lack of syntax for copying records and setting fields in one go.
- Lack of syntax for lists and arrays. That goes for constructors and pattern-matching.
- Not having a GC, and unsafe memory accesses. Not being constrained by a GC is an advantage, but remembering the reports I got from my first runs of Valgrind on C++ code I thought was bug free scared me for ever.
- Understanding template code isn't exactly accessible to all mortals. I don't have problem understanding mine, but whenever I looked into implementations of the STL, boost or cgal I found myself wondering what language they were using. My C++ and their C++ don't live in the same world.
- The total lack of fun in dealing with a library that uses another version of boost (or any library that uses templates).
- Verbosity of separate header/implementation files.
- Type inference in C++ does not go as far as e.g. F#. I know it's been improved in C++11, but as I understand it's similar to var in C#, which isn't enough once you tasted F#-style inference.
- Lack of computation expressions, including sequence expressions, comprehensions, async...
It would not surprise me if several of these points were actually possible in C++ using some template and preprocessor magic, but you can't really use these in a production environment unless you have very adventurous and tolerant co-workers.
I was a die-hard C++ enthusiast before. Then I started using generic programming with templates and higher-order functions using function objects. It just was too tiresome to write. After I tried a functional language I never looked back.
What problems of advantages do you see in using c++0x vs something like f# for a mixed functional programming style?
The upward funarg problem, which was debated in the context of Lisp 40 years ago!
I imagine that it would be… interesting… to implement certain optimizations common to functional languages in C++0x (like common subexpression elimination).