1 What is Haskell?

[Sources: 1, 2, 3]

1.1 Purely functional programming language

1.2 Non-strictness

1.3 Static typing

Simon Peyton Jones's "Region of Abysmal Pain" Venn diagram

Simon Peyton Jones's "Region of Abysmal Pain" Venn diagram

1.4 Call-by-need

1.5 Whitespace-sensitive syntax

1.6 Garbage collection

1.7 Naming conventions

1.8 What else?

2 Our first Haskell code

2.1 Interactive Haskell

First you'll need to start your terminal or command prompt. Once you've done that, we'll create a brand-new Stack project named hello-world:

stack new hello-world simple --resolver=lts-7.8
cd hello-world

The simple template is one of the simplest-possible Haskell projects: a project with a single executable target with the same name as the project itself, i.e. hello-world in this case. It consists of the following:

Next we'll start up GHCi, the interactive Haskell interpreter:

stack ghci
Input Output Comment
λ> x = 5 Assigns name x to value 5
λ> y = 6 Assigns name y to value 6
λ> z = x + y Assigns name z to value x + y
λ> z 11 Evaluates z and displays value
λ> :type z
λ> :t z
z :: Num a => a Shows type of z
λ> :t 5 5 :: Num t => t Shows type of 5
λ> z = "hello" Assigns name z to value "hello"
λ> z "hello" Evaluates z and displays value
λ> :t z z :: [Char] Shows type of z
λ> :t (+) (+) :: Num a => a -> a -> a Shows type of + operator
λ> :q Quits GHCi session


2.2 Your first Haskell source file

Now we'll create a source file and write similar code. We'll then run this through a compiler and execute it. In your favourite editor, open a new file Hello.hs in the existing hello-world project directory and type the following text into it:

x = 5
y = 6
z = x + y

main = print z

Now we can run the program as follows:

stack runhaskell Hello.hs

Now we'll change Hello.hs to the following to mimic our GHCi example:

x = 5
y = 6
z = x + y
z = "hello"

main = print z

And we'll try to run it again:

> stack runhaskell Hello.hs

Hello.hs:4:1: error:
    Multiple declarations of ‘z’
    Declared at: Hello.hs:3:1

2.3 Differences between GHC and GHCi

There are naturally many differences between the interactive and non-interactive Haskell environments. The most important ones for our immediate purposes are:

3 A more realistic example

[Sources: 1]

λ> x :: Integer; x = 5
λ> y :: Integer; y = 6
λ> z :: Integer; z = x + y
λ> z
λ> :t x
x :: Integer
λ> :t y
y :: Integer
λ> :t z
z :: Integer
λ> a = 5
λ> :t a
a :: Num t => t
x :: Integer
x = 5

y :: Integer
y = 6

z :: Integer
z = x + y

main :: IO ()
main = print z

3.1 When to use type annotations