Functional Programming

Functional Programming is programming with functions. So much so obvious. But if the only thing you can do is creating and applying pure (side effect free) functions to jet more functions, how can you actually write useful programs? In imperative languages like C, every statement you write, changes the state of the program and often the state of the real world. So performing IO, which is nothing short of changing the state of the real world, is trivial.

How can functional languages do this. One possibility is to allow some functions to perform side effects. This is the approach of languages like Lisp. Another approach is to keep the language itself free of side effects, and outsource side effects into the type system. To make this practical, there needs to be mechanism to combine small effectfull computations into larger ones and eventually into whole applications that perform actual work. The mechanism that languages like Haskell use is Monads, a concept from category theory. In this series of posts I'll try to explain what monads are, how to use them in your code and how they solve the IO problem.

Functors

When you implement a data structure like lists or trees in Haskell, you might want to consider making that structure an instance of the typeclass functor. A functor is basically something you can map over. Lispers know about the map functions (mapcar, mapcan, maplist, ...) that allow you to apply a function to every element in a list. In Haskell this concept works for every data structure that is a functor. Lets look at this implementation of a binary tree and its functor instance: