Table of Contents

4. Higher order functions

4.1. Map, foldl, foldr, filter, zipWith

Map

The function map has signature: (a→b) → [a] → [b]. It takes as parameter:

  1. a transformer function f :: a→b
  2. a list of objects of type a

It returns:

  1. a list of transformed objects of type b

4.1.1. Write a function which takes a list of integers and adds 4 to each element.

4.1.2. Write a function which takes a list of strings and adds a whitespace at the beginning of each string.

4.1.3. Write a function which takes a value k, a list of strings and returns a list with the first k characters of each string.

Filter

The function filter :: (a → Bool) → [a] → [a] takes as parameter:

It returns:

4.1.4. Write a function which removes all strings which are equal to the empty string.

4.1.5. Write a function which takes a list of strings and returns only those which start with a capital.

Foldr

The function foldr has type (a → b → b) → b → [a] → b. It takes as parameter:

It returns:

Example:

foldr (*) 0 [1,2,3] = 1 * (2 * (3 * 0))

4.1.6. Write a function using foldr which computes the product of a list of integers.

4.1.7. Write a function which computes the maximum of a list of positive integers using foldr.

4.1.8. Write a function which concatenates a list of strings using foldr.

Foldl

The function foldl has type (b → a → b) → b → [a] → b. It takes as parameter:

It returns:

Unlike foldr, foldl reduces element in a different order: Example:

foldl (*) 0 [1,2,3] = ((0 * 1) * 2) * 3

4.1.9. Write a function which uses foldl to check if an element is member of a list.

4.1.10. Write a function which uses foldl to implement the reversal of a list.

zipWith

4.1.11. Use ghci> :t zipWith to inspect the type of zipWith. Think about a possible example to run this function. What does it do?