Extra Credit: Due Monday May 2


Erlang

1. Write a Quicksort function in Erlang to sort a list of items. Recall that the main feature of Quicksort is a partition step that selects a pivot value and partitions the list into values that are less than the pivot and values that are greater than the pivot. Then Quicksort is recursively applied to the partitions.

2. Write a version of Quicksort that uses processes to sort the partitions concurrently. This version needs a way to apply a function to all elements of a list in parallel. Here's a parallel version of the map function that does this (from Programming Erlang: Software for a Concurrent World, by Joe Armstrong):

-module(plists).
-export([pmap/2]).

pmap(F, L) ->
    S = self(),
    Pids = lists:map(fun(I) -> spawn(fun() -> pmap_f(S, F, I) end) end, L),
    pmap_gather(Pids).

pmap_gather([H|T]) ->
    receive
        {H, Ret} -> [Ret|pmap_gather(T)]
    end;
pmap_gather([]) ->
    [].

pmap_f(Parent, F, I) ->
    Parent ! {self(), (catch F(I))}.

3. Write an Erlang version of the Producer-Consumer problem

Source code should be zipped and submitted online.