---
# System prepended metadata

title: The counting numbers

---

# The counting numbers

[Install Haskell](https://hackmd.io/@alexhkurz/Hk86XnCzD).

Open a file, add the line

    data NN = O | S NN
        deriving (Eq,Show)

and save the file as `numbers.hs`.

`NN` stands for Natural Numbers. The line  `data NN = O | S NN` is the classical mathematical definition of the natural numbers: 

- `O` is an natural number
- if `n` is an natural number then `S n` is a natural number
- nothing else is a natural number

We can teach this definition to Haskell by starting a Haskell console with

    ghci
    >Main>:load numbers.hs

Of course, right now there is only so much we can do with our numbers. We can ask whether `O` equals itself:
    
	*Main> O == O
	True

But even this has some interest. To see why, delete the line `deriving (Eq,Show)` from your program and load again.

	*Main> n = S O
	*Main> n
	S O
    
If you get here the error message `No instance for (Show NN)` you need to put `deriving (Eq,Show)` back into your program. Finally, we ask

	*Main> n == O
	False
    
Given that `n = S O` this tells us that zero and one are different numbers. This is more interesting than you may think ... after all we want to build numbers from scratch, not making any implicit assumptions. 

Let us summarise what we learned about the natural numbers. 

- There is a natural number we write as 'O' ("Oh") and we pronounce "zero".
- For all natural numbers `n`, there is a natural number `S n`, which we call the "successor of" `n`.

**Activity:** What next? Run some experiments ...

    *Main> S O == O
    *Main> S O == S(S O)
    *Main> S O == S(S(S O))
    *Main> S(S(S(S(S(S(S(S O))))))) == S(S(S(S(S(S(S(S O)))))))
    




