Week 8: Exercises

1. Curried Functions

A curried function takes its arguments one at a time:

> sum 3 4
7

An uncurried function takes its arguments in a tuple:

> sum (3, 4)
7

Write a function curry that converts an uncurried function of two arguments to a curried function, and a function uncurry that performs the inverse transformation.

2. Iterate

Write a function iterate f x that returns an infinite list of repeated applications of f to x:

> take 10 (iterate (*2) 1)
[1,2,4,8,16,32,64,128,256,512]

3. sortOn

Write a function sortOn f list that sorts a list by applying a key function to each element:

> sortOn length ["one", "two", "three", "four", "five", "six"]
["one","two","six","four","five","three"]

As in this example, the sort should be stable: elements with the same key value should keep their relative order.

4. 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..8] == [2, 1, 4, 3, 6, 5, 8, 7]