A Taste of Haskell

The Pragmatic Programmer by Andy Hunt & Dave Thomas recommends learning one new language a year. Learning a new language-even if you never use it professionally-broadens your understanding of your craft, sheds new light on the programming language/s that you use on a day to day basis, and will help make you a better programmer. Also, … it’s fun!

Haskell is probably not the first language that comes to mind when you hear the word ‘Pragmatic’. Nevertheless, a “A Taste of Haskell” was mind-bending. The presenter; – Simon Peyton-Jones – is part of the Microsoft Research team. He is one of the fathers of Haskell and is largely responsible for writing the GHC (Glasgow Haskell Compiler). He was also a Keynote presenter on Wednesday, discussing Transactional Memory for Concurrent Programming.

Now I reveal to you my knowledge of Haskell-all 4.5 hours-and the features that set it apart from other languages:

Values: Mainstream Object Oriented programming languages (Java, C++, C#, Ruby) are about state. Objects have data attached to them, and that data is often modified by the program. This is a key aspect of the OO paradigm. Functional programming languages are about values. In a purely functional language like Haskell, functions return values, but they never change state.

This is one of the most important features of the language and is central to the purity (and safety) of Haskell. You never have to worry about side effects.This focus on side-effect free values was initially a big problem for Haskell because the creators didn’t know how they would do IO… but managed to get around it by selectively permitting effects (my four hours don’t enable me to explain this fully 😉 ). I think the goals of this pursuit can be appreciated by anyone who has used Transfer Objects (a.k.a. the Value Object pattern).

Type System: Haskell has a very sophisticated Type system. It is statically typed, but goes far beyond what we are used to in a languages like Java/C#. Part of the reason is it’s Haskell’s use of Type Inference. Type Inference refers to the ability to deduce automatically, either partially or fully, the type of the value derived from the eventual evaluation of an expression (http://en.wikipedia.org/wiki/Type_inference).

Haskell’s powerful type inference means that you never have to worry about a Null Pointer. It also permits the programmer to omit type annotations in some cases which makes the language feel less restrictive. The influence of sophisticated type systems from languages such as Haskell and ML can be seen in Java Generics. For a more in-depth discussion of type checking checkout Can your type checking system do this?

Lazy Evaluation: Haskell will only evaluate as much of the program as possible as is required to get an answer.

Pattern Matching: Haskell uses pattern matching. This requires a programmer to consider all possible cases before returning a value. Here is a function that calculates the nth Fibonacci number:

--start of fib function
fib :: Integer -> Integer
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
--end of fib function

The first line is the function declaration. The function is called fib. It takes a Type Integer and returns a Type Integer. The next 3 lines are all possible patterns for the given parameter. The first line states that if the given value is 0 return 1. The second states that if the given value is 1 return 1. The third line is a recursive call to the fib function.

The Big Picture:

Peyton-Jones summarizes the central challenge of programming languages to be both useful AND safe. Most mainstream languages (C, Java, Ruby, etc) begin as useful (indeed, it’s why we are using them), and attempting to move in the direction of safety (Garbage Collection, Static Typing, etc). The designers of Haskell are simply approaching from the other direction. Haskell is safe, and is working towards being (more) useful. The hope is that even if Haskell never becomes a mainstream language, the ideas in Haskell will find their way into future mainstream languages.


It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply