Week 9 – Optional Exercises

1. Write a method 'map' that takes an array a of integers and a delegate f that maps an integer to an integer. The method should return an array in which each integer i in a has been replaced by f(i).

2. Repeat the previous exercise, using a linked list of any type Tand a delegate that maps type T to type U.

3. Write a method 'filter' that takes an array a of integers and a delegate f that maps an integer to a bool. The method should return an array containing only the integers i in a for which f(i) is true.

4. Repeat the previous exercise, using a linked list of any type T and a delegate that maps type T to a bool.

5. Repeat the previous exercise, modifying the linked list in place rather than returning a new linked list.

6. Write a method that finds an approximate zero of a continuous function using a binary search. The method should take a delegate f that maps a double to a double, plus two start values x1 and x2 such that f(x1) < 0 and f(x2) > 0. Return a value y such that |f(y)| < 1 / 1,000,000.

7. Write a recursive method 'zip' that takes two linked lists (possibly of different types) and returns a linked list of pairs of values (using a generic struct Pair). Each pair should contain one value from each input list. The number of pairs should equal the minimum of the lengths of the input lists.

8. Write a GTK application that draws 100 random lines between random points, each with a random color.

9. Write a GTK application that lets the user draw squares by dragging the mouse. Each square should have a random color. If the user clicks on an existing square, they can drag it to a new position, and it should rise to the top of the Z-order.

10. Write a GTK application that animates a bubble sort. The application should display a row of random integers. As the bubble sort proceeds, adjacent integers should exchange positions by rotating around a point between them.

11. Write a GTK application that displays a bouncing ball. The ball should lose a bit of velocity each time it hits the ground, so that it eventually comes to a stop.

12. Extend the previous application so that the user can create new balls by clicking the mouse. Each ball should have a random size and color. All the balls should bounce simultaneously.