Chapter 4. Tour

Example Haskell code:

Prelude> 42
42 :: Integer
Prelude> length [1984, 42, 2003]
3 :: Int
Prelude> length [1..10]
10 :: Int
Prelude> [1,3..10]
[1,3,5,7,9] :: [Integer]
Prelude> [x^2 | x <- [1..10]]
[1,4,9,16,25,36,49,64,81,100] :: [Integer]
Prelude>length "dog food"
8 :: Int
Prelude>"coffee" ++ "Tea"
"coffeeTea" :: [Char]

Those familiar with Lisp will find Haskell has some similarities. For instance, list processing and higher order functions are all over the place.

Prelude> head [1..10]
1 :: Integer
Prelude> tail [1..10]
[2,3,4,5,6,7,8,9,10] :: [Integer]
Prelude> 10:[1..9]
[10,1,2,3,4,5,6,7,8,9] :: [Integer]
Prelude> map (^ 3) [1..5]
[1,8,27,64,125] :: [Integer]
Prelude> foldl (+) 0 [1..10]
55 :: Integer

There are several Haskell compilers available. In this lecture, we'll be using Hugs which is a very portable interpreter. Its already installed on the CIS machines. If you're not able to log into the computers in front of you, you may want to sit with someone who can.