-
Types and values:
True :: Bool,Just 7 :: Maybe Int.BoolandMaybe Intare types;TrueandJust 7are values. -
Functions and calls:
double :: Integer -> Integer,double 3 :: Integer. Define quadrupling asquadruple x = double (double x). -
Pattern matching:
isZero :: Integer -> Bool. Define cases withisZero 0 = TrueandisZero _ = False. -
Recursion:
sumTo 0 = 0,sumTo n = n + sumTo (n - 1). Reduce the input until reaching the base case. -
Type variables:
astands for a type.head :: [a] -> areturns one element;tail :: [a] -> [a]returns the remaining list.
With numbers specialized to Int:
head [True, False] -- True :: Bool
tail [True, False] -- [False] :: [Bool]
head [[1, 2], [3, 4]] -- [1, 2] :: [Int]
tail [[1, 2], [3, 4]] -- [[3, 4]] :: [[Int]]
Connection to earlier lessons: for Maybe, pure wraps a value in Just; fmap applies an ordinary function, and <*> applies a wrapped function. >>= passes the inner value to a function returning Maybe and uses that result directly.
Next session: Helsinki MOOC Part 1, 2.6–2.7: Maybe and constructors. Implement intOrZero :: Maybe Int -> Int using pattern matching.
Reference: Haskell MOOC Part 1
Top comments (0)