## Saturday, November 22, 2014

### Haskell class, so far

Well, I'm about 5 weeks into Introduction to Functional Programming, a.k.a. FP101x, an online class taught in Haskell by Eric Meijer. The class itself is a couple weeks ahead of that; I'm lagging a bit. So, how is it so far, you ask?

The first 4 weeks covered basic functional concepts and how to express them in Haskell, closely following chapters 1-7 of the book, Graham Hutton's Programming in Haskell:

• Defining and applying functions
• Haskell's type system
• parametric types
• type classes
• type signatures of curried functions
• pattern matching
• list comprehensions
• recursion
• higher-order functions

Haskell's hierarchy of type classes is elegant, but some obvious things seem to be missing. For example, you can't `show` a function. But, it would be really helpful to show something like a docstring, or at least the function's type signature. Also machine-word sized `Int`'s don't automatically promote, so if `n` is an `Int`, `n/5` produces a type error.

Most of the concepts were familiar already from other functional languages, Scheme via SICP, OCAML via Dan Grossman's programming languages class, and Clojure via The Joy of Clojure. So, this early part was mostly a matter of learning Haskell's syntax.

Some nifty examples

• a recursive definition of factorial:

``````factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * (factorial (n-1))``````
• sum of the first 8 powers of 2:

``sum (map (2^) [0..7])``
• a recursive definition of map:

``````map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x : map f xs``````
• get all adjacent pairs of elements from a list:

``````pairs :: [a] -> [(a,a)]
pairs xs = zip xs (tail xs)``````
• check if a list of elements that can be ordered is sorted by confirming that each pair of elements is ordered:

``````sorted :: Ord a => [a] -> Bool
sorted xs = and [x <= y |(x,y) <- pairs xs]``````

Haskell attains its sparse beauty by leaving a lot implied. One thing I figured out during my brief time with OCAML also seems to apply to Haskell. Although these languages lack the forest of parentheses you'll encounter in Lispy languages, it's not that the parentheses aren't there; you just can't see them. A key to reading Haskell is understanding the rules of precedence, associativity and fixity that imply the missing parentheses.

Pre- cedence Left associative Non- associative Right associative

9

`!!`

`.`

8

`^`,`^^`,`**`

7

`*`, `/`, ``div``, ``mod``, ``rem``, ``quot``

6

`+`,`-`

5

`:`,`++`

4

`==`, `/=`, `<`, `<=`, `>`, `>=`, ``elem``, ``notElem``

3

`&&`

2

`||`

1

`>>`, `>>=`

0

`\$`, `\$!`, ``seq``

Another key is reading type signatures of curried functions, as currying is the default in Haskell and is relied upon extensively in composing functions, particularly in the extra terse "point-free" style.

Currently, I'm trying to choke down Graham Hutton's Addendum on Monads. If I end up understanding that, it'll get me a code-monkey merit badge, for sure.