Wednesday, 29 May 2013

7 Languages in 7 Workshops: Haskell

The guys of the Cambridge Software Craftsmanship community had the brilliant idea to start a series of workshops to introduce from scratch new programming languages. The titles of the workshops are taken from the book Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages (Pragmatic Programmers).

Yesterday, I participated to the first workshop by Jeff Foster that was about the Haskell programming language: 7 Languages in 7 Workshops: Haskell

I was completely new to the language and I really enjoyed playing with it.

On Windows, you need to install WinGHCi to get the interpreter. I used Notepad++ as editor and I used WinGHCi to compile and execute functions from it.

The first program was obviously Hello World.

Jeff showed us different ways to define functions introducing the concept of guard clauses and the possibility of multiple definitions of the same function. I found the last one very expressive.

Then I wrote my own implementation of Fibonacci:

So far, I didn't have problems. It was all pretty obvious.

The introduction of type signature actually made me think a while. In particular I played with the command :t to get the signature of a function.

Then Jeff introduced the concept of Type Classes (kind of interfaces) and the command :i to inspect a type class in GHCI.

I wrote some types and used type constructors to take things apart.

You could run the previous code in the following way

We also learnt about how to create records.

Finally we learnt about recursive types in particular lists and here is where I had the most of the fun. Jeff asked us to write some functions like contains, filter, append, reduce and reverse.

I have been able to do the first three before the time runs out.

Here few examples of execution:

It's also interesting to see how the Haskell compiler inferred the type signatures of the functions.

Two hours passed very quickly and I had a lot of fun.

You can find code and slides from Jeff at the following repository:

No comments:

Post a Comment

What you think about this post? I really appreciate your constructive feedback (positive and negative) and I am looking forward to start a discussion with you on this topic.