Contents

1 Overview

Haskell is a functional (that is, everything is done with function calls), statically, implicitly typed (types are checked by the compiler, but you don't have to declare them), lazy (nothing is done until it needs to be) language. Its closest popular relative is probably the ML family of languages (which are not, however, lazy languages).

The most common Haskell compiler is GHC. You can download GHC from http://www.haskell.org/ghc/download . GHC binaries are available for GNU/Linux, FreeBSD, MacOS, Windows, and Solaris. Once you've installed GHC, you get two programs you're interested in right now: ghc, and ghci. The first compiles Haskell libraries or applications to binary code. The second is an interpreter that lets you write Haskell code and get feedback right away.

2 Simple expressions

You can type most math expressions directly into ghci and get an answer. Prelude> is the default GHCi prompt.

You can build with ghc --make Test.hs, and the result will be called Test. (On Windows, Test.exe) You get an

if

expression as a bonus.
The first non-space character after

do

is special. In this case, it's the p from

putStrLn

. Every line that starts in the same column as that

p

is another statement in the

do

block. If you indent more, it's part of the previous statement. If you indent less, it ends the

do

block. This is called "layout", and Haskell uses it to avoid making you put in statement terminators and braces all the time. (The

then

and

else

phrases have to be indented for this reason: if they started in the same column, they'd be separate statements, which is wrong.)

(Note: Do not indent with tabs if you're using layout. It technically still works if your tabs are 8 spaces, but it's a bad idea. Also, don't use proportional fonts -- which apparently some people do, even when programming!)

4 Simple types

So far, not a single type declaration has been mentioned. That's because Haskell does type inference. You generally don't have to declare types unless you want to. If you do want to declare types, you use

::

to do it.

Prelude> 5 :: Int
5
Prelude> 5 :: Double
5.0

Types (and type classes, discussed later) always start with upper-case letters in Haskell. Variables always start with lower-case letters. This is a rule of the language, not a naming convention.

You can also ask ghci what type it has chosen for something. This is useful because you don't generally have to declare your types.