Haskell in 5 steps
Haskell is a general purpose, purely functional programming language. This page will help you get started as quickly as possible.
1 Why learn Haskell?
John Hughes has written a good essay: Why Functional Programming Matters.
2 Install Haskell
Haskell, like most other languages, comes in two flavors: batch oriented (compiler) and interactive (interpreter). An interactive system gives you a command line where you can experiment and evaluate expressions directly, and is probably a good choice.
|GHC||Compiler and interpreter (GHCi)||Probably the most feature-complete system|
|Hugs||Interpreter only||Very portable, and more lightweight than GHC.|
While both GHC and Hugs work on Windows, Hugs has perhaps the best integration on that platform. There is also information available on installing Haskell software on Mac OS X.
3 Your first Haskell program
Please note that this example will not run in hugs, at least not in version 20050308, because hugs does not support defining functions in command prompt. You have to put your definitions into a source file.
If you've learned to program another language, your first program probably was Hello world. In Haskell, your first program is the Factorial function.
1) Start an interpreter
Open a terminal. If you installed GHC type ghci (the GHC interpreter). If you installed Hugs type hugs.
$ ghci ___ ___ _ / _ \ /\ /\/ __(_) / /_\// /_/ / / | | GHC Interactive, version 6.4, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. Loading package base-1.0 ... linking ... done. Prelude>
2) Write your first program
From GHCi, we can either load programs from files, or we can define them directly at the command promt. Try the following:
Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)
This defines a function called fac which computes the factorial of an integer (see step 4 for details).
3) Run it
Type fac 12 to pass the argument 12 to the function fac.
*Main> fac 12 479001600
Congratulations! You've successfully run your first Haskell program.
3.1 But I want Hello World!
Before venturing into the long explanation, here is Hello World:
putStrLn "Hello World"
Typing this into GHCi will give you the result you expector at least, the result you should be expecting. If you want an executable that prints the magic string when it is executed, you can put the following in a file called hello.hs:
main = putStrLn "Hello World"
The compiler requires a value called main, so it knows what to execute. Compile it with ghc hello.hs -o hello and run the resulting executable (./hello on Unix, hello.exe on Windows).
3.2 Making a Short Story Long
One interesting aspect of functional programming is that we are working with functions. Functions don't have side effects, they return a value that depends on the parameters. In order to do IO (say, printing out messages), you could imagine functions taking a parameter world encompassing all external state, and returning a modified world (one with the words Hello World on your monitor), which in turn can be passed on to subsequent functions.
Haskell provides something similar, but more convenient: it treats functions that interact with the world as a separate type, often called IO actions. So while a function converting a number to its printed representation can have type Int -> String, a function reading a string with a specific length from the terminal will probably have type Int -> IO String. This means that, given an Int, it returns an IO action for reading a String. Hopefully, this explains why main has type IO () -- you generally want your program to be able to interact with the world, and thus it must itself be an IO action.
This may sound complicated, but it isn't all that different from other languages that separate statements from expressions, and the advantage is that Haskell has a solid framework for working with this, namely the IO Monad.
If you'd like a quick introduction to how IO works in Haskell, please have a look at Introduction to IO.
4 Where to go from here
There are quite a few good Haskell tutorials and books. Here are some we recommend:
- Yet Another Haskell Tutorial (English)
- Haskell-Tutorial (English)
- A Gentle Introduction to Haskell (English)
- Haskell Kurs (Deutsch)
- Programming in Haskell (English)
- Functional programming (English, Español, Netherlands) (Note, that this uses an old version of Haskell; e.g. input/output has changed since then)
- Tour of Haskell Syntax
- Haskell Reference
- Tour of the Haskell Prelude
- A tour of the Haskell Monad functions
- The Haskell School of Expression
- Haskell: The Craft of Functional Programming
- Introduction to Functional Programming using Haskell
- An Introduction to Functional Programming Systems Using Haskell
- Algorithms: A functional programming approach
- The Haskell Road to Logic, Maths and Programming
5 Get help
Information about Haskell support for various operating systems is here.