Relearn You a Haskell (Part 1: The Basics)

Andrew (he/him) - Oct 30 '18 - - Dev Community

I worked my way through Learn You a Haskell for Great Good! 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:

ghci> not True -- comments come after two hyphens
False
Enter fullscreen mode Exit fullscreen mode

...but it does use /= to mean "is not equal to":

ghci> 5 /= 4
True
Enter fullscreen mode Exit fullscreen mode

function application

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

ghci> max 3 4
4
Enter fullscreen mode Exit fullscreen mode

partial function application

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

ghci> min5 = min 5

ghci> min5 4
4

ghci> min5 6
5
Enter fullscreen mode Exit fullscreen mode

infix notation

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

ghci> div 100 5 -- prefix notation
20

ghci> 100 `div` 5 -- infix notation
20
Enter fullscreen mode Exit fullscreen mode

functions

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

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

ghci> pythagoras 5 12
13.0
Enter fullscreen mode Exit fullscreen mode

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:

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

ghci> ss = ['e','l','l','o']
Enter fullscreen mode Exit fullscreen mode

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

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"
Enter fullscreen mode Exit fullscreen mode

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

ghci> ll !! 2 -- equivalent to ll[2] in C
3
Enter fullscreen mode Exit fullscreen mode

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:

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

ghci> [1, 2, 3] > [1, 2]
True
Enter fullscreen mode Exit fullscreen mode

list functions

head / tail / init / last / length

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
Enter fullscreen mode Exit fullscreen mode

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:

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
Enter fullscreen mode Exit fullscreen mode

reverse

ghci> reverse ll -- reverse a list
[4,3,2,1]
Enter fullscreen mode Exit fullscreen mode

take / drop

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]
Enter fullscreen mode Exit fullscreen mode

maximum / minimum

ghci> maximum ll
4

ghci> minimum ll
1
Enter fullscreen mode Exit fullscreen mode

sum / product

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

ghci> product ll -- 1 * 2 * 3 * 4
24
Enter fullscreen mode Exit fullscreen mode

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:

ghci> elem 2 ll
True

ghci> 5 `elem` ll
False
Enter fullscreen mode Exit fullscreen mode

cycle / repeat / replicate

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

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]
Enter fullscreen mode Exit fullscreen mode

ranges

Ranges work with integers and characters:

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

ghci> ['a'..'j']
"abcdefghij"
Enter fullscreen mode Exit fullscreen mode

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

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

ghci> [26,23..1]
[26,23,20,17,14,11,8,5,2]
Enter fullscreen mode Exit fullscreen mode

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

ghci> take 10 [1,3..]
[1,3,5,7,9,11,13,15,17,19]
Enter fullscreen mode Exit fullscreen mode

Coming up in Part 2: list comprehensions, tuples, and typeclasses

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .