Relearn You a Haskell (Part 1: The Basics)
Tags: haskell, beginners, refresher
Published: 2018-10-30 23:04:18

I worked my way through [Learn You a Haskell for Great Good!](http://learnyouahaskell.com/) about six months ago and I haven't worked much with Haskell since then. So of course I forgot everything. So I've worked back through it again and made this handy refresher guide (mostly for myself but also) for you to have a quick reminder how things work in Haskell-land:

## `not`

Haskell doesn't use `~` or `!` to denote negation, rather, it uses the `not` function:

```haskell
ghci> not True -- comments come after two hyphens
False
```

...but it does use `/=` to mean "is not equal to":
```haskell
ghci> 5 /= 4
True
```

## function application

Haskell doesn't use parentheses to separate a function's name from its arguments; rather, we just use spaces:

```haskell
ghci> max 3 4
4
```

## partial function application

Partially-applied functions can be saved in variables and used later!

```haskell
ghci> min5 = min 5

ghci> min5 4
4

ghci> min5 6
5
```

## infix notation

Some functions make more sense when they appear between their two parameters, rather than before them. For instance:

```haskell
ghci> div 100 5 -- prefix notation
20

ghci> 100 `div` 5 -- infix notation
20
```

## functions

Define basic functions by putting the named parameters before the `=` and the function definition after:

```haskell
ghci> pythagoras a b = sqrt (a*a + b*b)

ghci> pythagoras 5 12
13.0
```

Note that function names can contain `'` (usually used to denote strict versions of otherwise lazy functions) and cannot begin with capital letters.

## lists

In Haskell, lists are homogeneous and strings are lists of characters:

```haskell
ghci> ll = [1,2,3,4]

ghci> ss = ['e','l','l','o']
```

A single element can be prepended (fast) to a list with `:` and a list can be appended (slow) to a list with `++`:

```haskell
ghci> 'h':ss
"hello"

ghci> ll ++ [5] ++ [6,7]
[1,2,3,4,5,6,7]

ghci> 's':'m':ss -- : and ++ can be chained like this
"smello"
```

Elements can be extracted with the `!!` operator (unlike the `[]` operator in C):

```haskell
ghci> ll !! 2 -- equivalent to ll[2] in C
3
```

A list can itself contain lists of items or lists of lists of items and all of those lists can be different lengths, as long as they all contain the same type of objects (numbers or characters, but not both).

Finally, lists are compared in lexicographical order, meaning that the first elements are compared first, then the second elements, etc:

```haskell
ghci> [1, 2, 3] > [0, 4, 5]
True

ghci> [1, 2, 3] > [1, 2]
True
```

## list functions

### `head` / `tail` / `init` / `last` / `length`

```haskell
ghci> ll
[1,2,3,4]

ghci> head ll -- first element of list
1

ghci> tail ll -- all but first element of list
[2,3,4]

ghci> init ll -- all but last element of list
[1,2,3]

ghci> last ll -- last element of list
4

ghci> length ll -- length of list
4
```

Note that `head`, `tail`, `init` and `last` all throw errors if they're called on an empty list, but `length` returns 0. You can check if a list is empty with `null`:

```haskell
ghci> length [] -- length of an empty list is zero
0

ghci> length ll -- ll has 4 elements in it at indices 0..3
4

ghci> null [] -- an empty list is null
True

ghci> null ll
False
```

### `reverse`

```haskell
ghci> reverse ll -- reverse a list
[4,3,2,1]
```

### `take` / `drop`

```haskell
ghci> take 1 ll -- take 1 element from the beginning of the list
[1]

ghci> take 3 ll -- take 3 elements
[1,2,3]

ghci> drop 1 ll -- drop 1 element from the beginning of the list
[2,3,4]

ghci> drop 3 ll -- drop 3 elements
[4]
```

### `maximum` / `minimum`

```haskell
ghci> maximum ll
4

ghci> minimum ll
1
```

### `sum` / `product`

```haskell
ghci> sum ll -- 1 + 2 + 3 + 4
10

ghci> product ll -- 1 * 2 * 3 * 4
24
```

### `elem`

`elem` works like sort of like how `contains()` works in other languages, it returns true if the first argument is an element of the second argument, which must be a list:

```haskell
ghci> elem 2 ll
True

ghci> 5 `elem` ll
False
```

### `cycle` / `repeat` / `replicate`

Repeat a list with `cycle`; repeat a single object with `repeat`

```haskell
ghci> take 10 (cycle ll)
[1,2,3,4,1,2,3,4,1,2]

ghci> take 10 (repeat 5)
[5,5,5,5,5,5,5,5,5,5]

ghci> take 5 (repeat ll)
[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]

ghci> replicate 10 5 -- same as take 10 (repeat 5)
[5,5,5,5,5,5,5,5,5,5]
```

## ranges

Ranges work with integers and characters:

```haskell
ghci> [1..10]
[1,2,3,4,5,6,7,8,9,10]

ghci> ['a'..'j']
"abcdefghij"
```

The first two elements of a list can be given to define a pattern:

```haskell
ghci> ['z','w'..'a']
"zwtqnkheb"

ghci> [26,23..1]
[26,23,20,17,14,11,8,5,2]
```

Above, the list ends when the last element is hit, but you can also just define how many elements you want by using `take`:

```haskell
ghci> take 10 [1,3..]
[1,3,5,7,9,11,13,15,17,19]
```

---

_Coming up in Part 2: list comprehensions, tuples, and typeclasses_