Danger! If you are happy with your current knowledge of programming languages, don’t read on – Haskell might be responsible for some serious defects in your motivation.
I was always some kind of programming language geek. I loved learning and playing around with all kinds of programming languages1. Currently I’m trying to learn Haskell. Even though I learned functional programming at my university (two semesters using Haskell), I didn’t really learn how to interact with the outside world. This is where things start to get messy, no matter how beautiful the language is.
Haskell has many good ideas and takes programming to the next step. Things like implicit concurrency, explicit concurrency, purely functional, laziness, polymorphism, and various evaluation schemes provide many ways to get our job done (I wish I could show you how to do that, in the meantime look at the tutorials at the end of the article or the Haskell Wiki).
For me, Haskell was one of the harder languages to learn. The pure functional core is easy to understand, but the crossover from the outside world is not as easy.
The reason, I think, is that Haskell provides quite a lot of syntactic sugar3 and it is incredible flexible. Another reason is that Monads seem to be quite hard to understand, and Haskell is an ancient word meaning “programming in monads”.
Haskell tries to encapsulate the dirty world in a special Monad2, a show-stopper for many Haskell beginners (I’ve included some good tutorials on Monads at the end of this article to get you started).
Reading a Haskell program is a lot easier than writing one. The error messages from the compilers are very different, and due to type inference often hard to understand. It might be that someone who didn’t spent his last 15 years programming in an imperative paradigm is better suited.
Anyway, after you understand Haskell, your programs are much smaller and more clean than in any other language. Haskell is very expressive, and you tend to think more about your problem than how to formulate it in you language.
Currently my only concern is memory efficiency – looking at the great programming languages shootout Haskell is quite good, but in my current understanding it’s not straight forward to write memory efficient programs in Haskell. Anyway, we have the options and the possibility to improve since we won’t produce much code! Of course there are cases, where Haskell is not the first choice.
Currently I can’t teach anyone anything about Haskell programming – I’m still learning, but I can help you to get started.
When we start a Haskell program, the
main function of the
Main module gets called, so our entry-point (lets call it main.hs) should look like
module Main where main = do ...
Larger programs should be split into modules, just like Java’s package system, Haskell modules are hierarchic. For example if we’ve got a module
Interlinked.Demo, we’ll put it into the directory
Interlinked and call the file
Demo.hs. (This is not required for the
Demo.hs would look like:
module Interlinked.Demo where callMe = 10
to include the module in another module, we’ll have to import it:
module Main where import Interlinked.Demo main = do putStrLn $ show callMe
this imports all functions from the Demo.hs into the current namespace. If we have a name-clash (two functions with the same name), we have several possibilities to remedy the problem:
- hide the unused function
import Interlinked.Demo hiding (callMe)
- Qualify all calls
- Qualify only one package
import qualified Interlinked.Demo as D, and call it
Compiling and Running
To run the program I’d recommend using
runhaskell for development and compiling it to native code for the distribution or profiling.
To run our program we simply type
runhaskell main.hs (if our Main module is in main.hs), or we can compile the code into a single executable using ghc:
ghc --make main.hs, the
--make option compiles all dependencies (if needed) and links everything.
To sum it up:
runhaskell main.hsfor development
ghc --make main.hsfor distribution and profiling
We could also fire up a interactive shell and evaluate the methods selectively using
Profiling is quite important, especially in Haskell because it’s easy to make some stupid mistakes. To profile our program we have to compile it using
ghc -prof -auto-all --make main.hs, this adds profiling code to our program (our code isn’t recompiled if we didn’t change our files, so we may need to delete old object files by ourselves).
To run our program with profiling enabled, we start it with
./main +RTS -p. This generates a file (
main.prof) containing the profiling results.
See the profiling documentation for more information on profiling.
Glasgow Haskell Compiler
The Glasgow Haskell Compiler is one of the better known Haskell compilers, Haskell itself is just a specification and there are many implementations (like Hugs and the York Haskell Compiler). All of them implement Haskell 98, but in the meantime many extensions have been proposed and are available in some of the compilers (most of them in GHC).
More on Haskell
Since I didn’t tell you how to program in Haskell, I should point out some interesting tutorials:
- On Haskell, Intuition and Expressive Power, a love-letter for Haskell.
- Why Haskell another love-letter for Haskell.
- Haskell Hacking is a multi-part tutorial showing some amazing tricks. Nevertheless, it is aimed at the beginner: Part 1, Part 2, Part 3, and Part 4
- It is quite popular to implement Lisp (and Scheme) in Haskell: Write Yourself a Scheme in 48 hours and Writing a Lisp Interpreter in Haskell.
- Hal Daumé III has written a very good introduction into Haskell: Yet Another Haskell Tutorial maybe the Dive Into Python for Haskell.
The first two of the following tutorials give an easy to understand introduction (and a good metaphor) to Monads, and the third one (All about Monads) is the most complete one with quite a lot of background:
Other sources include the Haskell homepage and of course Google. If you like books more than online tutorials, check out The Haskell School of Expression, but it’s not meant for the beginner. The Haskell book for beginners seems to be Haskell. The Craft of Functional Programming. Recently (January 2007) a new book Programming in Haskell came up, this one is also targeted at the beginner.
2 Monads are used for a lot more than interacting with the world!
3 It’s certainly no syntactic minimalist like Lisp.
Update: I just found an interesting article with quite the same experiences like I had. Anyway, it’s good to see that it’s not completely my fault.
Truth emerges more readily from error than confusion.
— Francis Bacon