Reading notes of “Learn you a Haskell for Great Good” — Part 1
This part covers Chapter 1 — Chapter 3 of the book. I’m listing the points that I find new/interesting to me.
We usually use ‘ to either denote a strict version of a function after the function name.
There are naming convention in Haskell, e.g. function name can’t begin with upper case letters, and types are written in capital cases.
- use “++” to append two lists
- use “:” operator to put something at the beginning of a list.
- get something out of the list by index, then use “!!”.
The lists within a list can be of different lengths but they can’t be of different types. This is a bit weird to me at first sight.
- elem (it’s the same as Python’s “in” operator)
Range expression: [1..20] From big numbers to smaller ones: [20, 19..1] ? It’s a bit unintuitive.
List comprehension: [ x * 2 | x <- [1..10], x *2 >= 12], I’d argue the syntax is better than Python’s list comprehension. :-)
When drawing from several lists, comprehensions produce all combinations of the given lists and then join them by the output function we supply.
There’s an interesting function to produces a list of pairs. zip:
zip [1,2] [3,4] outputs [(1,3), (2,4)].
“::” is read as “has type of”.
(==) :: (Eq a) => a -> a -> Bool
The equality function takes any two values that are of the same type and returns a Bool. The types of those two values must be a member of the Eq class (this was the class constraint).
The “read” function can be inferred what kind of result we wanted, it’s a bit different than C++’s decide from input parameters. But we can use type annotation “read “5” :: Int”.