Name: Anonymous 2012-01-27 4:46
-module(sleepsort).
-compile(export_all).
sort(In)->
T = string:tokens(In, " \t\n,"),
Ints = getints(T),
Max = lmax(Ints),
startt(Ints),
lists:reverse(collect(Max, [])).
collect(Max, Acc)->
receive
X ->
collect(Max, [X|Acc])
after
(Max + 1) ->
Acc
end.
sleepuntil(X, Main)->
timer:sleep(X),
Main ! X.
getints(In)->
getints(In, []).
getints([], Acc)->
Acc;
getints([X|Xs], Acc)->
case string:to_integer(X) of
{error, _} ->
io:format("Fail~n"),
exit(0);
{A, _} ->
getints(Xs, [A|Acc])
end.
startt([])->
ok;
startt([X|Xs])->
spawn(?MODULE, sleepuntil, [X, self()]),
startt(Xs).
lmax([])->
0;
lmax([X])->
X;
lmax([X|Xs])->
Max = lmax(Xs),
if (X > Max)->
X;
true ->
Max
end.Erlang style. I wasted a few loc reimplementing finding the maximum value ina list because I couldn;t find one in the standard library.