Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Check my sleepsort

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.

Name: Anonymous 2012-01-27 6:30

Check 'em dubz base 1.

Name: Anonymous 2012-01-27 7:05

And that's why nobody programs in Erlang

Name: Anonymous 2012-01-27 9:38

Check 'em dubz base 1.

Name: Anonymous 2012-01-27 11:43

>>4
That's quads, bro.

Don't change these.
Name: Email:
Entire Thread Thread List