Watch the Monkey Learn Haskell for Your Amusement - The Type System Rocks

Have you ever struggled with your data types? Ever had pages and pages of boilerplate for trivial conversions? How about checking every member function you were going to use so as not to throw an exception later? I sure have. There doesn’t seem to be a language out there that doesn’t frustrate me in at least one of these areas. Except Haskell.

Now, I can’t say that the situation won’t change as I write more in Haskell. I really liked C++’s type system until I got to know it. I’ll try to go easy on the fanboy-ism and stick to an overview of what I see that I like.


Functions as First Class Citizens

Haskell is all about the functions. They’re everywhere, which is unsurprising because Haskell is a functional language. But there are some neat side effects because of this. For instance, you can assign a function to a variable. No, not like a function pointer. Can you change the function pointed to into a new function to store in another variable? No. In Haskell, though, mucking with functions like that is how you do things. Have a function with two arguments but want a function with only one? Give the first function one of its arguments and store the resulting ‘one argument already filled out so it only needs one more to execute’ function in a new variable. Partial application, baby!

Strong Typing

Everything in Haskell has a type at compile time. I really like that. There was I time when I would’ve said a dynamic system could be useful, but Ruby did that and I always felt like someone had blindfolded me and shoved me into freeway traffic when it came to my variable types. I could be expecting a number for an argument, and some cheeky bastard would send me a string/class instance/array. If I cared to ensure a number was sent, which is the default for how I write code, I had to check at runtime and then deal with whatever I got! “But it’s more generic that way!” I hear someone yell. No. No it isn’t. How about an example, eh?

foldl (+) 0 [1,2,3]

That sums the list of numbers [1,2,3]. How about another?

foldl (max) 6 [1,2,3]

That returns the maximum number in the list or 6 if no number is greater than six. One more:

let fwibble a b = a + b + 10
foldl fwibble 0 [1,2,3]

That returns 36. “Wha? What just happened?” you say. Well I’ll tell you.
foldl takes a function as it’s first argument. A function that takes two arguments and returns one value of the same type. As GHC would put it:

forall a. (a -> a -> a)

That is a valid type in Haskell, and it is the type of the first argument to foldl. Anything matching that type can be the first argument, including the function ‘fwibble’ that I made. foldl takes this function and applies it to the second argument and the first element in the list, so:
0 + 1 + 10 = 11
It takes the result and uses it and the second element in the list as arguments to fwibble, so:
11 + 2 + 10 = 23
On down the list it goes until it runs out of list and returns:
23 + 3 + 10 = 36
All this and more just from the foldl function.

At this point you could do this all with function pointers and the like, but I’m sure you’ll agree that the syntax is much nicer in Haskell for doing it.


Type Inference

“But wait, if Haskell is so strongly typed, where are your type declarations?”
Not needed! Haskell has good type inference; in most cases you never have to specify a single type and you still get the benefits of strong typing! Glee!

Type Classes

Type classes are a beautiful thing. Think of them as compatible types with the same operations, like interfaces or a base class without data members in other languages. The key to the wonder of Haskell with regard to them, though, is that they are used by default. I’ll explain. Want to guess what type Haskell assigned to my ‘fwibble’ function from above?

forall a. (Num a) => a -> a -> a

What does it mean? It means fwibble can take anything of class Num, which includes ints, floats and their friends. Better yet, if I make a new type that supports a few functions needed to be a number, I can use it with fwibble too! It’s like templates in C++, but automagic and less stupid! As a side note, please don’t fill my comments area with why C++ templates are the most awesome thing evar. I used templates long enough, I don’t want to hear it. They will appear stupid when you take a look at what else is out there.

I have plenty more to say on Haskell typing, but my blogging gland has run out of juice for now. Tune in next time when I’ll say something. Oook.

Tags: ,

One Response to “Watch the Monkey Learn Haskell for Your Amusement - The Type System Rocks”

  1. Nathan Says:

    Cool man!
    I’m gonna have to work on understanding these type things so I can make my own functions, but I wish you well in your quest Monkey.
    Nathan

Leave a Reply