Revision as of 17:44, 22 July 2007

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.

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</p> is another statement in the <hask>do

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

do</code> block. This is called "layout",and Haskell uses it to avoid making you put in statement terminators and braces all the time.(The <hask>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.