Heroes never panic.

Learning Haskell – Types And Functions

Posted: April 13th, 2010 | Author: | Filed under: haskell | Tags: , | No Comments »

Haskell is a strongly typed static language.

This means that Haskell will not automatically cast one type into another. In other words, you can’t expect it to treat an integer as a float. If you define a function that takes a float parameter and you feed it an integer afterwards, Haskell will complain. Having static types means that the compiler knows the type of every value and expression at compile time. Before anything is executed. So if we try and use types in an expression that don’t match (say, trying to add a string “3” to an integer 5), Haskell will complain. But there is a bonus: Haskell has type inference. Meaning, Haskell will deduce the type of a value or expression for us everywhere it can. This makes type declaration optional.

Continuing my journey into the abyss of Haskell, it occured to me that we all already learned functional programming. Do you still remember the basic Algebra classes in your elementary school? Well, that is just it – functional programming. And Haskell’s syntax is just like algebra. With some extra sugar and batteries included (yes, i took that last part from Python … i just couldn’t resist).

At the algebra classes we learned that functions return values. Later we were spoiled by imperative programming languages and the returning values became an option explicitly stated with return keyword. Not in Haskell. All functions return values, because they are expressions. Always. And we don’t need the return anymore because we get it implicitly. Oh, and did I tell you that there are no objects in Haskell? Just functions!

To define a function that returns a sum of two numbers I could write something like the following:

add a b = a + b

Here I tell Haskell that add takes two parameters and it should return a sum of the two. I didn’t have to declare the types of the parameters, nor the returned value. And i could have, if I weren’t so lazy! But I am safe anyway. If i try to pass two strings to the function, Haskell will yell at me. That’s inference at work.

Let’s say that i want to be a good citizen and declare the types as well, but being new to Haskell i just am not really sure what they could be. Well, I can always check it out after I write my function down with the help of the interpreter.

ghci> :t add
ghci> add :: (Num a) => a -> a -> a

Haskell is telling us that for him, the add function can take any two parameters that are of the same type and it will return a value of the same type back, but the type must be of a Number typeclass. Ok, I had some hard time to decipher it as well, so let me rephrase this a little bit. The (Num a) part is telling us that for all the “a”s that follow they must be of a numeric type. The part “a->a->a” is simplified like this: the last a is the return value, while the first two are function arguments. So, armed with this insight i can go back and modify my function so that it has a type declaration as well.

add :: (Num a) => a -> a -> a
add a b = a + b

Yes, it is weird at first. But quite soon it starts to make sense, trust me. And wait till next week when i show you patterns and guards!


Learning Haskell – First Date

Posted: March 30th, 2010 | Author: | Filed under: haskell | Tags: , | No Comments »

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 :)


Learning Haskell – Before I Start

Posted: March 23rd, 2010 | Author: | Filed under: haskell | Tags: , | No Comments »

Each Monday from now on, I am going to publish a small step that I made during the week into mastering Haskell. This is a commitment that I make to myself and by deciding to make the progress public (although you may be the one and only reader) I am kind of forcing myself to stick to it.

Why Haskell? Why now?

Well, first, let me tell you that learning a new programming language is actually very exciting. It is  like when you were a kid and you were given a new toy. Remember how excited you were? You just could not wait to unwrap it and start playing.

Having said that, Haskell has been on my radar for some time. I just never really took the time to start looking deeper into it. And I realized that if I am going to wait until I think I have more time, it is never going to happen. You have only so much time as you give it to yourself. So this excuse is not going to cut it anymore.

Oh, and why Haskell I hear you wonder? Because I want to add a functional programming language to my arsenal. Because it is gaining in popularity but it is still underground enough to feel exotic. Because it is lazy. Because I’ve been pretty intrigued about what monad is.