Functional Programming

Module code: CO2008

Many of the ideas used in imperative programming arose through necessity in the early days of computing when machines were much slower and had far less memory than they do today. Languages such as C(++) and Pascal carry a substantial legacy from the past. Even Java, despite its OO features, has been devised to look ‘a bit like C’. If one were to start again and design a programming language from scratch what would it look like?

For many applications, the chief concern should be to produce a language which is concise and elegant. It should be expressive enough for a programmer to work productively and efficiently but simple enough to minimise the chance of making serious errors. Rapid development requires the programmer to be able to write algorithms and data structures at a high level without worrying about the details of their machine-level implementation. These are some of the criteria which have led researchers to develop the functional programming language Haskell.

In this module you will learn how to program in Haskell, which exemplifies the functional style.

The flavour of programming in Haskell is very different from that in an imperative language. Much of the irrelevant detail has been swept away. For example, there are at least two different uses for a variable in Java: as a storage location, and as parameter in a method. There is only one use for a variable in Haskell: it stands for a quantity that you don't yet know, as is standard in mathematical practice. The constructs in Java include expressions, commands, and methods; whereas in Haskell there are only expressions and functions. The meaning of a program in Java or C is understood by the effect it has on the `state' of the machine as it runs.  Haskell does away with the idea of `state'; the meaning of a program is the values it computes.

On the other hand, Haskell is a very expressive language. The type system allows functions to be written polymorphically so that the same code can be re-used on data of different types, e.g. the same length function works equally well on lists of integers as on lists of reals or lists of strings. Furthermore, it allows one to write functions which take other functions as parameters. These are known as higher order functions and they give a second form of code re-use. There are powerful mechanisms for introducing user-defined datatypes such as trees, sets, graphic objects, etc.  Haskell also makes a great deal of use of recursion.  The combination of these features makes for very clean, short programs, which, with some experience, are easier to understand than many imperative programs.

Back to top
MENU