Week 9: Exercises

Solve the following problems in Haskell.

1. Folding Minus

What are the values of these expressions?

  1. foldl (-) 0 [1, 2, 3]

  2. foldr (-) 0 [1, 2, 3]

2. Types of Folds

  1. What are the types of foldl and foldr?

  2. Write foldl and foldr.

3. foldl1 and foldr1

foldl1 and foldr1 are versions of foldl and foldr that take no accumulator argument: they simply apply a binary operator to all elements in a list. They cannot be applied to empty lists. For example,

    foldl1 (+) [1, 2, 3] == 6
    foldr1 (+) [1, 2, 3] == 6
  1. What are the types of foldl1 and foldr1?

  2. Write these functions.