Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional programming in C++11, F# style

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.

like image 480
gradbot Avatar asked Jul 06 '09 21:07

gradbot


2 Answers

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.

like image 167
Christopher Avatar answered Nov 10 '22 16:11

Christopher


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++:

  1. Lack of pattern matching. Once you get used to it, not having it can drive me crazy.
  2. Lack of syntactic sugar for tuples.
  3. Lack of syntax for copying records and setting fields in one go.
  4. Lack of syntax for lists and arrays. That goes for constructors and pattern-matching.
  5. 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.
  6. 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.
  7. The total lack of fun in dealing with a library that uses another version of boost (or any library that uses templates).
  8. Verbosity of separate header/implementation files.
  9. 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.
  10. 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.

like image 6
Joh Avatar answered Nov 10 '22 14:11

Joh