1 Prelude

A lot of functions are defined in the "Prelude". Also, if you ever want to search for a function, based on the name, type or module, take a look at the excellent Hoogle. This is for a lot of people a must-have while debugging and writing Haskell programs.

5.1 Random numbers

6 Dates and time

7 Lists

In Haskell, lists are what Arrays are in most other languages. Haskell has all of the general list manipulation functions, see also

Data.List

.

Prelude>head[1,2,3]1
Prelude>tail[1,2,3][2,3]
Prelude>length[1,2,3]3

Furthermore, Haskell supports some neat concepts.

7.1 Infinite lists

Prelude>[1..]

The list of all squares:

square x = x*x
squares =map square [1..]

But in the end, you probably don't want to use infinite lists, but make them finite. You can do this with

take

:

Prelude>take10 squares
[1,4,9,16,25,36,49,64,81,100]

7.2 List Comprehensions

The list of all squares can also be written in a more comprehensive way, using list comprehensions:

squares =[x*x | x <-[1..]]

8 Pattern matching

Haskell does implicit pattern matching.

A good example of pattern matching is done in the fact function for finding a factorial.

fact ::Integer->Integer
fact 0=1
fact n = n * fact (n -1)

In this function,

fact ::Integer->Integer

is the functions type definition.
The next line,

fact 0=1

is a pattern match, so when the argument to the function fact is 0, the return value is 1.

The 3rd and final line of this function is another pattern match, which says that, whatever number was entered as the argument, is multiplied by the factorial of that number, minus 1. Notice this function is recursive.

Pattern matching in Haskell evaluates the patterns in the order they are written, so