Thursday, July 2, 2009

Real-World Haskell

Normally I put more time into a blogpost, but this time I'm just going to rant for a bit instead.

I bought a book called Real World Haskell hoping that I would learn all kinds of wonderful things about the (in my book) top programming language Haskell. As it turns out, a Gentle Introduction to Haskell and the Haskell 99 Problems are a WAY BETTER APPROACH to learning the language IMHO. The book does a relatively decent job at teaching the language in the first few chapters but there is a distinct drop in quality in the later chapters.

Let Gentle Introduction to Haskell be GIH and Real World Haskell be RWH: RWH - GIH = (consider cost of thunk, consider readability of where clause over lambda). Consider the cost of thunk: when you are left-folding a list it will create a list of thunks which can be expensive. For example, if you

foldl (+) [1..1000000]

it will probably cause stack overflow. Do the substitution and you'll notice that the value isn't required unless the result is used so Haskell will delay that evaluation (in he process it creates a number of thunk procedures which end up eating the stack). Of course you can use the strict version (foldl') or foldr instead (foldr is primitive recursive for you computability geeks out there). So that is RWH good thing number one.

I like the Haskell take on lambda: \x -> x + 1. In particular the symbol '\' is the next best standard key on your keyboard to an actual lambda. This is not unlike the choice for not equals '/=' which looks much like the mathematical symbol for not equals. In RWH they introduction lambda expressions but mention that it's generally better to use a named function which is defined in a where clause instead of a lambda... I would agree with this. Readability should be considered very important because there are so many programmer that will look at your Haskell code be think "dude, what the hell is this?" anyway. Of course, I'm not taking a stab as Haskell... rather the reader because it's probable that they aren't familiar with functional programming (lambda's in particular) to start with.

In summary, don't buy Real World Haskell unless you are a hack-programmer who likes to feel good about themselves after reading a book with 'Haskell' printed on the cover (this would be the same type of programmer that thinks C is a functional language because you can only define functions and not class methods... get your shit together). You are much better off reading GIH over and over until it sinks in than RWH (with the exception of the difference set I just detailed). Point is... just pull it together and read GIH until you understand it... RWH is just masturbation.