c++ rant

Why not just use Haskell?

I am a fully paid up member of the ACCU. I joined because it seemed most of my now ex-colleagues were members and I’m a sucker for peer pressure.

Anyway, as you’d expect from a magazine dedicated to C and C++ users it occasionally has articles about C and C++. This quarter’s doozy is from a chap we’ll call Stuart. Now it looks like some of the things that Stuart is doing for his PhD are quite interesting. However his article about the similarities he has managed to draw between Haskell and using partial-template specializations in C++ gives me the shudders. It is based, in part, on the work of Andrei Alexandrescu. As charming as Mr Alexandrescu is in person I wouldn’t let him anywhere near any of my production C++ code (if I had any). But that’s beside the point.

Now I have to admit that I don’t know any Haskell and I’m also not a lover of C++ much either so I can’t bring myself to actually RTFA. However the article is full of little code snippets like this:

    template <typename T> struct Head;
    template <int x, typename xs>
       struct Head<IntList<x,xs> > {
       enum { value = x };

    template <typename T> struct Tail;
    template <int x, typename xs>
       struct Tail<IntList<x,xs> > {
        typedef xs result;

Why anyone would hate themselves enough to actually do this and write about it I don’t know. Especially when you could just use Haskell and not have to wrap your head around this lunacy.

But I know there is a simple answer. I should just unsubscribe from the magazine if I don’t like it. But sadly for me it’s not that simple. Stuart is obviously a clever guy, it almost goes without saying. But he’s wasting his brain cycles. Surely he must have better things to do with his time.

Then I read this 2005 interview with the glorious Alan Kay. If you have the time I suggest you read all of it because he makes some really very excellent points. In reference to my pain though this one stands out:

Perhaps it was commercialization in the 1980s that killed off the next expected new thing. Our plan and our hope was that the next generation of kids would come along and do something better than Smalltalk around 1984 or so. We all thought that the next level of programming language would be much more strategic and even policy-oriented and would have much more knowledge about what it was trying to do. But a variety of different things conspired together, and that next generation actually didn’t show up. One could actually argue—as I sometimes do—that the success of commercial personal computing and operating systems has actually led to a considerable retrogression in many, many respects.

You could think of it as putting a low-pass filter on some of the good ideas from the ’60s and ’70s, as computing spread out much, much faster than educating unsophisticated people can happen.

And while Stuart carries on trying to make C++ like Haskell, rather than just using Haskell, another wasted brain drops into the soup.

7 replies on “Why not just use Haskell?”

The thing is… Haskell isnt suited for young people whereas the OBJECT model of smalltalk (lets ignore the ugly syntax) is a very intuitive way…

but if you can explain a 3 year old what a monad is, then you convinced me. 😉
(Maybe they just inflate the explanation… things should be obvious to 3 year olds too)

Monads are like playing at someone else’s house. For example, you’ve got some good toys at home, but grandma has this great old erector set you can’t take home. If you want to play with it, you have to go over there. And, there’s special rules. you can’t take *all* your legos to grandma’s right? just one or two toys. And, you have to act differently. no running, no yelling. And, you don’t get to leave when you want. you have to wait for mom to come pick you up.

Same goes for your friend with the gi joes. Except you get to be louder and run more over there.

monad transformers are like picking up a friend who brings some gi joes to grandmas. you get the whole set of lego, gi joe and erector. but there’s all kinds of crazy rules about when you can do that. you have to get your mom and his mom to agree on a time… and grandma has to be ok with two kids running that afternoon.

I’ve explained to 11 year olds what monads are, with fairly good results. There’s really not a *whole* lot to it. Of course, that wasn’t always the case, but explanations improve with experience over time.

Depending on your definition of young people, I agree and disagree.

I’m fairly young (20) and I understand Haskell just fine, in fact, I think the functional paradigm in general is more intuitive than the Object model. Though I like them both alot.

As for explaining a monad to a 3 year old, I don’t know, but this explaination managed to suffice my 7 year old neice, (She asked me this just a few minutes ago, so I guided the conversation towards monads, here’s a transcript, more or less)

“Uncle Joe! Whateryoudoin?!”
“I’m programming my computer to do cool stuff, like math!”
“Icky, I don’t like math”
“Really? You do it all the time though, have you ever tied your shoes?”
“Sure! I’m wicked good at it!”
“Well, thats math!”
“Well, you have to do things in order, right?”
“Uh-huh,” she then proceeds to explain the “Bunny around the tree” method of shoe tying
“Well, in math, we call those monads, they help us do things in order- its a way to make the computer understand that the bunny can’t go around the tree before he comes out of the hole.”
“More or less, we can even use that idea to do cooler things- like what I’m doing.”
“Neat! I’m gonna go practice programming my shoes now, bye!”

I don’t know if that satisfies the requirement, but I’d bet that any reasonably intelligent 5 year old could understand monads. At least at a very basic level, I don’t think discussions of state monads, or monad transformers would even be out of the reach of a fairly bright 10 year old.

You try explaining things like Generic Objects, Dynamic Types, Dependency Injection, Interfaces, Mixins, Inheritence (and all associated evilness), Design patterns, etc.

Thats not to say that those things aren’t explainable, or that OO is horrible because of it. I rather like smalltalk (more than Java anyway) though I rarely have a chance to use it.

>> Did you even bother to read his conclusions?

Actually, I did skim read the article and I did very closely read the conclusions. What I didn’t do was try to comprehend the meaning of the partial template specialisations. The conclusions, though, were:

1) You could use it to sort a static table/tree at compile time

Err if it’s static, why not just sort it outside of the code? I appreciate this might not be practical on a very large static binary search tree but it would save yourself maaany lines of obfuscated template code, and head scratching.

2) Understanding typelists in Andrei’s book

Err, don’t go there. It’s best left alone. I’ve done C++ for about 13 years and I’ve never seen any large-scale production project that has made successful use of what’s in Andre’s book. But your mileage might vary.

3) The capabilities are astounding and provide real benefits to the brevity of the code.

Well not that astounding. In fact why not just use Haskell?!

Comments are closed.