Quiz Stuff

Quiz 6, February 19, 2018
2' (2 mins); 1 + 1 + 1 points; 3 points total

  1. What is the result of the following expression?
    foldl1 (*) [2,1,2]
    
  2. The first argument of the function foldl is a folding function. How many arguments does that folding function take?
  3. What's the BIG difference between foldl1 and foldl?
EC ½ point: What is the type of foldr1?

Answers

  1. What is the result of the following expression?
    > foldl1 (*) [2,1,2]
    4
    
  2. The first argument of the function foldl is a folding function. How many arguments does that folding function take?
    Two—an accumulated value and an element of the list that is foldl's third argument.

    That accumulated value is called "state" by some because it represents the state of the computation, but that seems a little imprecise to me because it typically doesn't contain any representation of how much of the list has been processed and thus isn't the full state of the computation.

    I've sometimes named that accumulated value parameter thusFar because it represents what we've computed thus far in the folding.

  3. What's the BIG difference between foldl1 and foldl?
    foldl1 (and foldr1) can be thought of as reductions—they produce a value of the same type as in the list they're folding. foldl (and foldr) can fold a list of values into a different type. Examples:
    • A list of Bools can be folded into a String.
    • A list of Ints can be folded into a Bool.
    • A list of chickens could be folded into the transportation cost of the chicken feed they're expected to consume in a year.

EC: What is the type of foldr1?

:t foldr1 shows
foldr1 :: Foldable t => (a -> a -> a) -> t a -> a
but as slide 135 suggests, we think of that as
foldr1 :: (a -> a -> a) -> [a] -> a