Monday, February 09, 2009

Haskell comprehension measured through WTF/min

The top compliment I can give to Real World Haskell is that it manages to finally teach me the aspects of Haskell programming that I previously assumed to be both impenetrably complicated and useless. As I read I'm also reminded of what it was like when I first tried to comprehend Haskell code. I've concluded that the most noticeable sign of greater Haskell comprehension is a noticeable drop in my WTF/min when I'm figuring out a given code example.

WTF/min is "WTFs per minute". According to a highly-linked picture, this unit is "the only valid measurement of code quality" and it's determined through code reviews. My initial experiences of Haskell definitely exhibited high WTF/min. The following are some of the past Haskell-related thoughts I can recall having at one time or another.
  • Infinite lists like [1..]? WTF? Oh, lazy evaluation, right.
  • Functions defined more than once? WTF? Oh, each declaration pattern matches on a different set of parameters. It's like method overloading.
  • The underscore character has nothing to do with this problem domain but it's being matched against. WTF? Oh, it matches anything but discards the match.
  • Why is the scoping operator "::" strewn throughout? WTF? Oh, it's being used for types, not scopes.
  • Even a simple IO command like "putStrLn" has a type? WTF is "IO ()"? Oh, it's an expression with IO side-effects that evaluates to the value-that-is-not-a-value, ().
  • WTF? What is this ubiquitous 'a' or 't' type everywhere? Oh, it's like the type parameters of generics or templates.
  • Functions don't need "return" statements? WTF? Oh, all functions are expressions anyway.
  • WTF is going on with these functions not being passed all their parameters at once? Oh, applying a function once produces another function that only needs the rest of the parameters. That'll be helpful for reusing the function in different contexts.
  • This function definition doesn't have any parameters at all, and all it does is spit out the result from yet another function, a function that itself isn't being passed all the parameters it needs. WTF? Oh, "point-free" style.
  • Now I understand all those -> in the types. But WTF is this extra => in front? Oh, it's sorta like a list of interfaces that must be met by the included types, so the code is tied to a minimal "contract" instead of a particular set of explicit types. That's good, but how would I set those up?...
  • Ah, now this I'm sure I know. "class" and "instance" are easy. WTF?! How can that be it? Just more functions? Can't I store structured information anywhere? Oh, tuples or algebraic data types.
  • I like the look of these algebraic data types with the "|" that I know from regular expressions. Unions and enums in one swell foop. WTF? How do I instantiate it? Oh, what appear to be constituent data types are actually constructor functions.
  • After a value has been stuffed into the data type, how can my code possibly determine which data type constructor was used? WTF? Oh, just more pattern-matching.
  • WTF? Record syntax for a data type declaration results in automatic accessor functions for any value, but we use these same function names when we're creating a new record value? Oh.
  • I've acquainted with map and filter. WTF is foldr and zip and intercalate? Oh, I'll need to look over the standard list functions reference.
  • What's this "seq" sitting in the middle of the code and apparently doing jack? WTF for? Oh, to escape from laziness when needed.
  • WTF? How can a function name follow its first argument or a binary operator precede its first argument? Oh, `backticks` and (parentheses).
  • How come there's all these string escapes in the flow of code? W...T...F? Oh, lambda. Cute.
  • I've always been told that Haskell is heavily functional and pure. WTF are these do-blocks, then? Oh, monads. Wait, what?
  • Functor, Monoid, MonadPlus, WTF? Oh, more typeclasses whose definitions, like that of monads, enable highly generalized processing.
  • A way to gain the effects of several monads at once is to use "transformers"? WTF? Oh, when a transformer implements the monad functions it also reuses the monad functions of a passed monad.
  • Finally...I know that the ($) must be doing something. But what? Why use it? WTF? Oh, low-precedence function application (so one can put together the function and its arguments, then combine them).


  1. Cute, funny, insightful. Thanks!

  2. Michel Boto3:54 AM

    Once I understood the value of eta reductions and >>= my WTF/min when looking at real Haskell code dropped considerably.

  3. Why doesn't my code work when it looks exactly like the example?! WTF?

    Oh... whitespace sensitive.

  4. I remember ($) being a WTF when I started...

  5. Anonymous3:50 PM


  6. I had really forgot of this wtf/min code measurement unit. Now, I just don't know if I should thank you. It's realness ...

  7. Thanks for the comments! It turned out more repetitive than it should have, but once I start on a Big List post it's hard to resist just using the same form for each item. Plus, it didn't seem fair to write each of my past WTFs without including the "oh" answers. I didn't want people to mistake this for a diatribe against Haskell syntax, which is remarkably regular. It makes sense...once you know it.

    By the way, I had these WTFs when I was trying to understand the Haskell code I saw lying around (people writing "look how short it is in Haskell!"), and not necessarily when I was focused on actually learning Haskell. Honestly, some of these date from back when I was a functional language n00b reading up on OCaml out of curiosity.

    Apparently as of right now I'm the top Google result for "Haskell WTF". Hmm.

    @Don Stewart
    No, thank you! I'm enjoying the book a lot (and submitting a few comments to the online version).

    That's one I missed. My prior experience with Python made significant whitespace less of a WTF and more of a familiar annoyance.

  8. Wow, just gotta love those rouge brain waves!


  9. Anonymous10:18 PM

    > method overloading
    > scoping operator
    > etc.
    Back to C++, please

  10. Anonymous10:52 PM

    I can see it now - a new line of books - the "WTF" series. Sorta like Dummies/Idiots guides. Next one should be on Objective C, government bailouts, and the like..

    Very timely, I think.

  11. i remember wasting several days trying to figure out this method i didn't understand was just an automatic boilerplate method. another reduction of WTF/min due to RWH.

  12. Anonymous10:12 AM

    Somebody should write a book in this manner...Little WTfs...

    ....WTF?...Oh so this....

  13. WTF while NSFW is ideal for work out of the box; what you were learned in school for. Like a sentencing with a pre-position at the end, a student is being learned how the exit looks the same as the entrance exam -Hal Ix

  14. love this. Though found it whilst looking for some information on "comprehension in haskell" - have two data types 'suit' and 'rank' and trying to list the complete pack of cards...