Archive for May 13th, 2009

Real World Haskell: Chapters 1 and 2

Wednesday, May 13th, 2009

Notes: not really anything noteworthy for me in these chapters.  I already speak a bit of Haskell.

Quotes:

Because the result of applying a pure function can only depend on its arguments, we can often get a strong hint of what a pure function does by simply reading its name and understanding its type signature. As an example, let’s look at not.

ghci> :type not
not :: Bool -> Bool

Even if we didn’t know the name of this function, its signature alone limits the possible valid behaviours it could have.

  • Ignore its argument, and always return either True or False.
  • Return its argument unmodified.
  • Negate its argument. 

We also know that this function can not do some things: it cannot access files; it cannot talk to the network; it cannot tell what time it is. 

Purity makes the job of understanding code easier. (emphasis mine) The behaviour of a pure function does not depend on the value of a global variable, or the contents of a database, or the state of a network connection. Pure code is inherently modular: every function is self-contained, and has a well-defined interface.

– pp39-40

As a formalist, I’m all about reasoning about type signatures for things.  When I design larger systems, I’m always looking for the type of information being passed around, then looking at how to turn one stage’s input format into the input format of the next stage.  This is partly based on my experience with functional languages, and partly based on an old programming maxim: “Show me your flowcharts and conceal your tales, and I shall continue to be mystified. Show me your tables, and I won’t usually need your flowcharts; it’ll be obvious.” (Brooks, The Mythical Man-Month, Ch9)

Takeaway:

Reading through the first two chapters of Real World Haskell reminded me just how bizarre learning OCaml was the first time around.

A new coworker recently mentioned Scala as something he enjoyed; I’ll probably give it a go soonish, to see how it fits.  I really like the feel of Haskell, and the community seems to be vibrant and growing.  About two years ago, I stopped using OCaml for anything, and started lurking around Haskell.  After a bit of prodding and coaching by my friend Evan, I mostly came to understand monads, but am still a little rough on the details.  Hopefully the process of reading through Real World Haskell will refine this, and let me knock out some interesting algorithmic code in Haskell.