Getting Started

Download sort.erl.

Start erl (Erlang interpreter) in the directory where you downloaded sort.erl. You can compile the code with the command

c(sort).

You can test your mergesort function with the command

sort:mergesort([11, 86, 2, 69, 22, 39, 85, 57, 78, 76]).

Your Task

Reimplement the merge function, which merges two sorted lists, using tail recursion.

You will need to define a tail-recursive helper function with an accumulator parameter. For example, let’s assume that your helper function will be called mergehelp. You could define your merge function this way:

merge(Left, Right) -> mergehelp(Left, Right, []).

Because tail-recursive list processing builds a list starting with the last element, you will need to reverse the result of the merge before returning the completed result. You can use the built-in lists:reverse function to do this.

Solution

Here is a solution: Lab 20 solution