Types and values: True :: Bool , Just 7 :: Maybe Int . Bool and Maybe Int are types; True and Just 7 are values.
Functions and calls: double :: Integer -> Integer , double 3 :: Integer . Define quadrupling as quadruple x = double (double x) .
Pattern matching: isZero :: Integer -> Bool . Define cases with isZero 0 = True and isZero _ = False .
Recursion: sumTo 0 = 0 , sumTo n = n + sumTo (n - 1) . Reduce the input until reaching the base case.
Type variables: a stands for a type. head :: [a] -> a returns 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]]
Enter fullscreen mode
Exit fullscreen mode
Connection to earlier lessons: for Maybe , pure wraps a value in Just ; fmap applies an ordinary function, and <*> applies a wrapped function.…