Wednesday, January 31, 2007

Haskell

I've been hearing about Haskell a fair bit lately, so I thought I'd take a look at it. I've only played around with it for an hour or so, so it's too early to draw any real conclusions. I need to pick a small project to implement in the language.

I'm working through Yet Another Haskell Tutorial, using the GHC implementation.

The article that first got me to thinking about the language was this post on MarkCC's "Good Math, Bad Math" blog. There was another, more recent (and less positive) article on reddit, but I can't find it now. If you search for Haskell articles on reddit, there are plenty of interesting ones, though.

What's got me really excited is the idea of Monads. This is a language feature that I haven't seen in any other language, so I'm curious to learn more about them. I think they're related to Category Theory, which makes them seem more sexy to me.

So in my very basic playing around, I've noticed a few oddities. Here's how you build a 2-tuple (a pair):

(1, "hello")


And here's a 3-tuple (a triplet):

(1, 'a', "goodbye")


To grab the first element of a pair, you use the function fst; to grab the second element, you use snd, like so:

> fst (1, "hello")
1

> snd (1, "hello")
"hello"



But the weird thing is that fst (first), and snd (second) are only defined for pairs. So, if you try to do:

> fst (1, 2, 3)

you get a type error! That seems odd to me, as it still makes sense to talk about the first, or second element of a triple or an n-tuple, for that matter. I imagine it will become clearer as I learn more about the type system. Also, shortening "first" to "fst", and "second" to "snd" seems gratuitous, but I can live with it. At least they're the same length.

0 Comments:

Post a Comment

<< Home