Learning Haskell - First Date

Monday is here and so I met for the first time with Haskell.

Installation went quickly and without any problems (installed the Haskell platform for macosx). You get a compiler, a bunch of standard modules and an interactive interpreter. I like having the interpreter as a way to quickly try out stuff in Python, so the fact that Haskell has its own as well immediately gained points with me :)

It is much more convenient while you learn to have an immediate feedback than having to type some code to a file and compiling.

First thing you try is the “command line calculator”. Haskell is behaving as you would expect it to behave: ghci> 2 + 4 ghci> 6

All the usual suspects are here: addition, multiplication, subtraction … nothing differs from Python. But then quickly the similarities stop to be, well, similar.

Haskell is a functional language and as such it does not modify state. That, translated to something meaningful to me is: variables are just symbols that hold values. Once you assign a value to a variable you can’t change it. Variables in Python and variables in Haskell are not the same. At the moment it is still difficult for me to grasp what this mean and how it can be useful. But right now I am still looking and evaluating Haskell from the imperative languages point of view. Also the fact that Haskell is known to be lazy is somehow odd, but then it sounds like a cool feature - the fact that it won’t bother to compute things until it is really forced to do so.

This means that you can create lists with infinite number of elements and Haskell it is quite happy to create them for you. Fast. Well, unless you try to create them at the interactive interpreter. You will have to stop it from spitting all the numbers till the end: ghci> [1..]

Lists.

They can have unlimited amounts of elements. But the elements must be all of the same type. Strings as we know them are lists of chars so the following examples are all the same to Haskell: ghci> “hello” ghci> [“h”,”e”,”l”,”l”,”o”]

If you want quickly construct a list of ranges you can do: [1..100]. This will create a list of numbers from 1 to 100. You can specify a step to the range as well: [1,3..20]. This will create the following list: [1,3,5,7,9,11,13,15,17,19].

For concatenating strings or lists you would use: [1,2] ++ [3,4] or “hello” ++ “ “ ++ “world”. Basically the ++ function inserts the stuff on the right (which must be a list) at the end of the list on the left side. But what if i want to insert something at the beginning? I can do that: ghci> a:[b,c,d] ghci> [a,b,c,d]

The only difference: the thing you insert is a single element. Not a list.

If you want to get the second element from a list (the indices start at 0) you would use the !! (yes, that is double exclamation mark): ghci> [1,2,3,4]!!1 ghci> 2

There is a whole bunch of functions to manipulate lists and they all sound quite exotic (although I did remembered my CS semester of Prolog while trying some of them out). Let’s say you want the first element from the list: ghci> head [1,2,3,4] ghci> 1

What about the last? ghci> last [1,2,3,4] ghci> 4

To get everything but the first element you use tail: ghci> tail [1,2,3,4] ghci> [2,3,4]

And everything but the last element? init. ghci> init [1,2,3,4] ghci> [1,2,3]

There is a lot of very well written and comprehensible material online that i am using as a learning material. Some of the best resource that I found are:

All in all, I enjoyed my Monday evening. Haskell does feel strange for now and I can’t possibly think of how i would go on about writing real life programs with it. But hey, it is only my first Monday :)