Exercises for week 7

1. Lab 1 revisited.

Recall the function buildHistogram from Lab 1. You probably defined it by explicit recursion over the list of observations. Can you redefine it by folding over this list?

2. Folding with bracketing to the left.

As discussed in chapter 9, we can understand the action of foldr as
foldr ¤  e [x1, x2, ... xn] = x1 ¤ (x2 ¤ ... ¤ (xn ¤ e)...)
(Here I used the (illegal) operator symbol ¤ to indicate an "arbitrary" operator). Define a function foldl that is similar but brackets to the left and, accordingly, puts the unit element e in the beginning of the expression. So we want
foldl ¤  e [x1, x2, ... xn] = (...((e ¤ x1) ¤ x2)... ) ¤ xn
Note: Also foldl is in the Prelude, so you have to come up with another name to test your definition.

After having done this, we could define e.g. sum using foldl instead of foldr. Would there be any advantage in doing so?

3. Minimal spanning trees.

This is a harder programming problem.

We consider the following problem: We are given n points in the plane and want to connect some of them pairwise by straight line segments ("roads") in such a way that

For example, if the points are those given to the left below, the solution is shown to the right.
Hopefully, it is intuitively clear that the solution to this problem will never include cycles, i.e. closed paths leading away from a point along one road and coming back along another. Connected graphs without cycles are called trees. A spanning tree is one which connects all the points. Finally, a minimal spanning tree is a spanning tree of minimal total length. The problem of finding a minimal spanning tree for a given set of points occurs frequently in applications.

The problem can be solved by the following famous algorithm (Kruskal's algorithm):

  1. form the list of all edges, i.e. two-element sets {P,Q} of distinct points.
  2. sort the edge list so that the shortest edges (where the length of an edge is the ordinary distance between its two points) come first.
  3. Start with the empty graph, i.e. only the points, and add edges as follows.

    Go through the sorted edge list from beginning to end and add the road (a line segment) described by this edge to the graph if this does not give a cycle. For example, at one stage of the construction of the above solution we will have the following situation:

    The shortest edge that has not yet been considered is now {P,Q} but this gives a cycle and is discarded. The next two are {S,T} and {S,Q}, which are also discarded. Thereafter comes is {Q,R}, which will be added.

    When all edges have been connected, the algorithm terminates.

It is far from obvious that this actually gives a minimal spanning tree, but for now we take this fact for granted.

Your task is to program this algorithm. Your function should take a list of points as input and produce a list of edges, those that form a minimal spanning tree. The hard part is the test of whether a proposed edge gives a cycle. You will have to design an auxiliary data structure to record which edges are connected at each stage and update this as you add new edges.

4. Giving Change

This and the next question are exam questions from last year.

This question concerns finding change. Suppose you have a collection of coins in your pocket, which we represent by a list of numbers such as [1,1,5,5,5,10,10]. Then you can pay 16kr by paying three 5kr coins and a 1kr, but you cannot pay 18kr at all. Suppose the person you are paying also has some coins, say [1,1,1,5]. Then you can pay 18kr by paying 20kr, and receiving 2kr in change. We will design functions to decide whether one person can pay another a specific amount, with or without giving change.

5. Generating Web Pages

Your task in this question is to write a program that converts a list of names and email addresses in a file, such as
John Hughes rjmh@cs.chalmers.se
Mary Sheeran ms@cs.chalmers.se
into a web page which when viewed in a browser looks like this, Clicking on the underlined links should send mail to the given address.

The web page is to be generated as HTML; for example, the web page in the example can be represented by the following HTML source:

<ul>
<li>John Hughes, 
    <a href="mailto:rjmh@cs.chalmers.se">
      rjmh@cs.chalmers.se
    </a></li>
<li>Mary Sheeran, 
    <a href="mailto:ms@cs.chalmers.se">
      ms@cs.chalmers.se
    </a></li>
</ul>
Here the <ul> tag introduces a bulleted list, the <li> tag introduces a list element, and the <a href=...> introduces a link. The layout of HTML source is not important: only the HTML tags affect what one sees in a browser. You need not reproduce the layout used above, only the tags.