Haskell: the pleasure of bondage and discipline

Like Emacs vs Vim, static versus dynamic typing is apt to spark bloody holy wars in a brutal attempts to determine who has the kindest and most compassionate God.

My Go To language is Python, and one of the many things to like about it is its dynamism. If it looks like a duck, and quacks like a duck, then we can can cook it like a duck. Mmm, that’s tasty duck.

I am a newbie to Haskell, so I am making a lot of faltering first steps. Whenever I get a piece of code to work, I tend to attribute it less to brilliant programming on my part, and more to the providence of Fortuna. There’s a big, unmoving, unyielding block in Haskell, and that big, unmoving, unyielding block is called the IO Monad. It seems to exist purely to frustrate programmers.

I have yet to really understand how to interpret a function’s type signature … and yet … and yet … it is dawning on me that this is one of the jewels in the crown of Haskell. You see, what is the essence of programming? Its essence is this: the transformation of one data structure into another. I have data in one form, but I want data in another form.

Those type signatures tell you what types go in, and what types go out. As you increase the specificity of the type signature, you loose generality in what it can do, but you are solidifying exactly the nature of the transformation.

Now, even C has type signatures, and in fact requires it so that it can layout memory correctly. So it’s not as if Haskell is the first language to come up with the idea of type signatures. However, Haskell goes way beyond mere memory layout. It completely affects the way you think about programming. Traditionally, programmers think about procedures. With Haskell, you tend to look at you job as creating type transformations, and assembling them in the appropriate order. Documentation can be sparse, because the type signatures can tell you pretty much all you need to know.

What these signatures do for you is provide well-defined interfaces which are “impossible” to use incorrectly, in the sense that the compie will refuse to allow you to operate on incompatable types. As one programmer noted on reddit (http://is.gd/oi4zze):

This is what I like to call “type tetris.” It’s fun, and you end up with correct programs without even having to understand what you just assembled.

Advertisements

About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s