I've recently decided to take a time-out from Python development and immerse myself in the group of languages that's usually called "functional programming languages"; they had always sounded very interesting (because different) to me, but I couldn't muster the time and will to look at them in depth.
I decided to skip Lisp, having encountered some Emacs Lisp code in the past, looked at OCaml and Erlang and finally stuck with Haskell
. Why? I think it was because Haskell is the most "different" of all those languages, and I wanted to learn something as "different" as possible.
(If you already know Haskell, you can skip the following explanations.)
What makes it "different"? Among the fundamental design decisions that make Haskell pretty unique (among the more or less well-known languages) are purity
. Let me explain these terms a bit (I hope I do them justice):
- Purity means that a function calculates its return value only from its arguments - it cannot have side effects, such as writing to global memory or printing to the screen. (Nevertheless, I/O is of course possible, and it does not violate this principle. How? See below.)
- Laziness means that calculations are not done until their result is really needed. It is similar to generators producing the next item on request only, but ingrained in every part of the language. For example, laziness makes it possible to work with infinite lists. There is no map vs. imap distinction, the normal map is already lazy enough not to produce lists that are never needed.
Despite the limitation that purity seems to pose on code, one is able to write quite formidable programs in Haskell, for a not-so-small part with the help of monads
(Having uttered that magic word now, let me tell you that Simon Peyton Jones, one of Haskell's fathers, once said that the biggest mistake made in the introduction of monads into the language was to call them by such a scary name and not, for example, "warm fuzzy thing".)
Monads may come from "the most abstract branch of mathematics called category theory" but are, in real life, a way to build "computations"
and stick them together in a specific way, so that the dull, repeated boilerplate code needed to stick them together is hidden by the monad. I won't say much more on them (yet - they say one of the duties of a new Haskell programmer is to write Another Monad Tutorial).
Monads allow you to do I/O purely, to have mutable state, to parse text, to write nondeterministic functions easily or, for a more advanced example, to use Software Transactional Memory to manage concurrent threads.
More stuff that makes Haskell fun(ctional):
- The whole functional paradigma, meaning functions passed around everywhere, partial application made easy, recursion etc.
- The type system. It's based on Hindley-Milner, enriched with ad-hoc polymorphism facilities of type classes (think function overloading based on arguments as well as the return type), and allows type inference of the whole program. Static typing done right!
- The nice mathematical-notation-like syntax, and the whitespace-based grouping.
- The well-designed module system, including import statements!
In other words, you should try it out! It takes a while getting used to, especially if you have, like me, never really done functional programming (except what Python offers), but it is fun to write. More than once, I've had the feeling that Haskell and Python share a similar spirit of expression - to exaggerate, you write down pseudo-code, and it's executable already.
After the first few steps (which included a primitive unpickler and such things), I ported a Python library to Haskell, namely CleverCSS
, which is a preprocessor for CSS with variables and nesting enabling DRY, and after fighting with the Parsec parser generator a bit, it was a really pleasant exercise with lots of stuff learned.
I also have to note that while the Python implementation had 1072 lines of code, the functional equivalent Haskell one is down at 760. You can look at the code in the repository
or the documentation
(it's also on Hackage
, the Haskell package repository under the name clevercss
Excellent sources for taking a more in-depth look at Haskell are:
There is also a very nice, friendly and always-helpful IRC channel #haskell on Freenode.