# Introduction To Haskell

Lecture 3

types :: type1 → type2

### Using These Slides

#### Every slide has a secret note.

• On Chrome: press `F12`, then click Console
• On IE: press `F12`, then click Console
• On Firefox: `Ctrl+Shift+k`

### Shortcut Keys:

 `↓`, `PgDn`, `n`, `j` next slide `↑`, `PgUp`, `p`, `k` prev slide `Esc` enables `ctrl+f` globally

### Haskell Programming

• Code lives in a file with a .hs extension

• Open the file with GHCi  Run it from the terminal `````` \$ ghci myfile.hs `````` Or run ghci, then load the file `````` \$ ghci Prelude> :load myfile ``````
• Call your function through ghci

## Previous Homework

Implement factorial
 A B C D E F G H I J ...
``````
-- Most popular solution
facA n = if n > 1
then n * facA(n-1)
else n

``````

### Types

• `Bool` = False | True

• `Char` = 'a' | 'b' | ... | 'A' | 'B' | ...

• `Int` = -231 | ... | -1 | 0 | 1 | ... | 231-1

• `Integer`

• `Double`

Types always start with a capital letter

## Everything has a Type

Haskell secretly infers that `True` is a `Bool`.

``````
Prelude> :type True
True :: Bool
``````

You can also explicitly use a type.

``````
Prelude> 3 :: Int
3

Prelude> 3 :: Double
3.0
``````

### Yes, Functions have a Type

``````
Prelude> head [1,2,3,4]
1

Prelude> :type head
head :: [a] -> a
``````
`head` has the type List of `a`'s to just `a`
``````
Prelude> fst ("left", "right")
"left"

Prelude> :type fst
fst :: (a, b) -> a
``````
fst has the type tuple of `a` and `b` to just `a`

### Like a Jigsaw Puzzle

Analogy from Real World Haskell

"if a piece has the wrong shape, it simply won't fit"

 Haskell Python*  What should be the type declaration for `fac`? (clear answer) (show answer)

``````
fac n = product [1..n]
``````

What's the type declaration for `&&` ? (clear answer) (show answer)

``````
(&&) ::
``````

How about `:` ? (clear answer) (show answer)

``````
Prelude> 0:[1,2,3]
[0,1,2,3]

Prelude> :type (:)

``````

(8:00 to 9:22)

## Typeclass

A Typeclass lets you generalize a function.

For example, the less-than sign can be used multiple ways.

• `10 < 20`
• `'a' < 'b'`
• `"aardvark" < "zzz"`
• `[6,2,4] < [6,3,8]`

## The `Ord` Typeclass

is used for things with total order

``````
Prelude> :type (<)

``````

### Similar to Interfaces

Remember them from Java?

``````
public class NumberUsedByLessThanSign implements Comparable {
...
}
``````

### Other Typeclasses

• `Show` - representable as a string
``````
Prelude> show 42
"42"
``````
• `Enum` - enumerable in a list
``````
Prelude> ['R'..'t']
"RSTUVWXYZ[\\]^_`abcdefghijklmnopqrst"
``````
• `Num` - usable as a number
``````
Prelude> 5.2 * 2.5
13.0
``````

### Have you noticed?

Functions with multiple arguments look funny.

The `take` function has two arguments:

• an integer, `n`
• a list, `ls`

and it produces the first `n` elements of `ls`

``````
Prelude> take 5 [1..]
[1,2,3,4,5]
``````

The type declaration is

``````
Prelude> :type take
take :: Int -> [a] -> [a]
``````

Weird...

## The Concept of Currying

All Haskell functions take only one argument ### A Closer Look

at the `take` function:

``````
take :: Int -> [a] -> [a]
``````

The `->` operator is right-associative

``````
take :: Int -> ([a] -> [a])
``````

`take` actually has type of Int to a function

### Partial Applications

Every function technically has one argument.

Actually, this is really cool!

``````
Prelude> :type take
take :: Int -> [a] -> [a]

Prelude> let takeFive = take 5

Prelude> :type takeFive
takeFive :: [a] -> [a]

Prelude> takeFive [1..]
[1,2,3,4,5]
``````

## Functional and Pure

A function only depends on its aguments.

A type declaration is a strong promise.

Focus on what is done, not how.

Inherently modular

Elegant

### Get Our Hands Dirty

Create `mycode.hs` and write a function called `zipTogether`

``````
-- mycode.hs
-- |The 'zipTogether' function binds together two lists.

zipTogether :: [a] -> [b] -> [(a,b)]

``````

Expected output:

``````
Prelude> zipTogether [1,2,3] "abc"
[(1,'a'),(2,'b'),(3,'c')]
``````
by the way, Haskell comes with `zip`

# Homework

1. Write a Caesar Cipher function called `cipher`
``````
cipher :: [Char] -> Int -> [Char]

``````
``````
Prelude> cipher "hello" 13
"uryyb"
``````
2. Bonus points for a cool solution!

3. Submit this week's homework form!