What Makes Haskell Worth Learning for Real World Applications
Around 750 people commented on the online preview of the Real World Haskell book. As described by one of its co-authors, John Goerzen, in a recent interview to O’Reilly, the book introduces Haskell with real code, real examples and tips to exploit in a business environment. In his interview, Goerzen explains why, in his opinion, this language is worth learning; he provides insights into its specificities and addresses some issues that may be a source of reluctance.
John Goerzen admits that Haskell requires a lot of re-learning of basics and identifies purity and laziness as the biggest mind-shifts for people coming from imperative and/or object oriented programming.
He highlights that in Haskell, “the things that can possibly change the state of the world are tightly isolated” and “marked with the type system”. This helps avoiding an important number of implementation bugs since impure elements of code are statically typed and errors can be indentified at a compile time, which differentiates Haskell from languages like Python. Purity also facilitates testing, for instance through QuickCheck, especially if the program is structured around a lot of small functions combined together.
Another central notion in Haskell is laziness. While asked as to “where does laziness help real world programs?” John Goerzen stresses how it relates to I/O:
If I write a little filter or parser program in Haskell, I don't have to really worry about the details of reading the input line-by-line or block-by-block because I can just use getContents and then I can string together a whole bunch of functions. I like writing Unix filters in Haskell.
[…]Once you have the laziness on the input, you have the laziness on the output because printing something doesn't require it all to be evaluated all at once. […]
You wind up with a program that is a very simple design because you're not having to deal with buffering or iterating over lines or what not and yet, runs in constant space.
John Goerzen also talks about benefits of Haskell in terms of expressiveness, achieved by pattern matching and the fact that functions can express things in succinct but readable way making it easier to state a problem and, then, “to reason about the logic” that is clearly identifiable. Moreover, as highlighted by the interviewer, one can “build a vocabulary of functions or words and then you write your solution in the language of that vocabulary”. Haskell, indeed, manipulates “functions as easily as Perl manipulates strings”, says Goerzen.
However, some features of this language may be a source of reluctance for people coming from OO or/and imperative programming. The recursive approach, for example, “can be a little bit scary” since “incrementing a variable is a pretty cheap operation” in languages like C. Nevertheless, Goerzen asserts that, even though “higher level languages tend to not be quite as fast as C”, GHC compiler that rewrites from recursion into iteration, “is pretty efficient at optimizing what you express in recursive terms into a format that's fairly efficient for the CPU.”
One can also believe that laziness trades “the ability to reason about types and purity and such for the inability to reason about time and space complexity”. John Goerzen prefers to put it into perspective:
[It is] just like if I'm looking at a program in whatever other language […]that tries to read in an entire file all at once and then process it. […] You'd open a file; you could call read, and it could read an entire file for you into memory if you like. Some people would do this expecting that maybe they're only dealing with files that are 100K or something. You try to give them a file that's 2 gig and that approach doesn't quite work anymore.
Based on his experience, John also refutes the belief that that Haskell IO system is poor and difficult to learn. According to him, the thing that scares probably most in Haskell are monads but he stresses that one “can write a perfectly good Haskell program that deals with IO without having to be terribly bothered by what monads are or what they do”. To use the IO monad, one does not have to understand monads but can simply consider it as “this kind of box that separates out the pure from the impure code”.
More generally speaking, John Goerzen believes that even though there is “a little bit of a learning curve on what are things called in Haskell”, learning is facilitated because “the types of functions tell you an awful lot about what they do”. Discovering Haskell is, according to him, a “mind-opening experience” and even if this language is not going to become the next big thing, “it's something that's different and interesting and worth learning some about anyhow.”