Week 8: Exercises

Solve the following problems in Haskell.

1. Alternating Map

Write a function altMap that takes two functions and a list. altMap should apply the two functions alternately to list elements. For example:

altMap (\i -> i + 1) (\i -> i - 1) [1..10] == [2, 1, 4, 3, 6, 5, 8, 7, 10, 9]

2. curry and uncurry

As we have learned, a curried function takes its arguments via successive function applications. For example, if f is a curried function of 2 arguments, then ((f x) y) is the value of f on the arguments x and y. (Note that ((f x) y) is the same thing as (f x y), since function application is left-associative.) Most functions in Haskell are curried.

By contrast, an uncurried function takes its arguments in a tuple. If f is an uncurried function of 2 arguments, then f (x, y) is the value of f on the arguments x and y.

Write a higher-order function curry that takes a uncurried function of two arguments, and returns a curried version of the function. Also write a function uncurry that takes a curried function of two arguments, and returns an uncurried version of the function. Determine appropriate types for these functions.