Welcome to the world of Haskell!

In the past three months I have had the pleasure of learning a strange yet interesting new programming language: Haskell. Initially I was quite sceptical about this language. After all, some of the most basic (imperative) concepts I knew looked so hard to implement in Haskell. And the fact that people allegedly had finally found some use for functional programming languages, did not really excite me either. But in the end, Haskell turned out to be quite fun.

In this article I would like to look at some Haskell features I really liked. As I know that most of our readers are not Haskell experts I have decided to keep it quite general and simple.

Function transformations

Function composition

Function composition is a technique of combining two functions into one function.

f1 . f2

This says that function f1 should be applied to the result of applying f2 to its first parameter. So, above code could be rewritten to:

(.) f g = h
where h x = f (g x)

The type of h can be deduced like this:

f :: a -> b
g :: b -> c
– Thus:
h :: a -> c

Function application

Function application is not exactly a very exciting feature, but it is helpful as it helps you to get rid of annoying extra parantheses.

f1 $ f2

This says that function f1 should be applied to the result of function f2. Now you are probably what the difference is with f1 f2.

When you simply type f1 f2, f2 is considered as a parameter for f1 and all of the parameters of f2 will be considered as n-ary parameters of f1. When you type f1 $ f2 you first apply f2 to all the following parameters, and f1 is then applied to the result of f2.

So, for example:

sum x y z = x + y + z

successor x = x + 1 — A better alternative is discussed in the next section!

test1 = successor sum 1 2 3 — Will not work. After all, you cannot take the successor of a function.
test2 = successor $ sum 1 2 3

Partial parameterisation

Partial parameterisation is a technique to partially satisfy a function. This technique can be used to create new specialized functions.

For example, if we would want to declare a function that gives us the successor of the given integer, we could simply write:

successor = (+) 1

or

successor = (+1)

This would make the following example valid:

> successor 41
42
> successor $ successor 41
44

successor is of course an incredibly easy example of partial parameterisation, and it might give you the impression that its just a gimmick. However, in combination with function application and function composition its an incredibly useful mechanism.

A slightly more complex example, is a function that gives us the successors of all elements in the given list.

successors = map (+1)

Operators

Another cool feature of Haskell is the ability to define your own operators. As this is actually rather simple I am not going to spend much attention to it.

Lets say we would want to express addition and substraction through smilies. It could be done like this:

x ^-^ y = x + y
x -.- y = x – y

Or with partial parameterisation in our mind:

(^-^) = (+)
(-.-) = (-)

Which would make the following valid:

> 1 ^-^ 2
3
> 3 -.- 1
2

In the latter definition you can see how operators can also be used as functions: you simply put parantheses around them. And the fun thing is that a similar trick can be applied to functions with two parameters:

min :: Int -> Int -> Int
min = (-)

> 3 `min` 1
2

In order to use a function as a operator you have to put backquotes around it.

Again, this might seem like a gimmick, but it can be pretty useful in the field of “symbolic evaluation”. But more on that another time.

Lazy evaluation

Lazy evaluation is one of the most powerful features, if not the most powerful feature, of Haskell. Lazy evaluation is the technique of delaying a computation until the result is required. This means that the data that is required for a computation is retrieved at the moment that the computation is actually executed. This makes it possible, for example, to work with lists of infinite length without actually computing the complete list. As lazy evaluation is beyond the scope of this article I would like to refer interested readers to Wikipedia, which has a nice article on the matter.

And with that I would like to conclude my first article about Haskell. I hope you enjoyed reading it. And as always: if you have questions or comments feel free to post them.

One Response to “Welcome to the world of Haskell!”

  1. [...] has already been several weeks since I posted an article here. And despite the fact that I have been busy will all kind of things, I have little to show at [...]

Leave a Reply