Skip to content

Instantly share code, notes, and snippets.

@charsyam
Created August 9, 2013 10:19
Show Gist options
  • Save charsyam/6192634 to your computer and use it in GitHub Desktop.
Save charsyam/6192634 to your computer and use it in GitHub Desktop.
-module(mylist).
-export([append/2, sum/1]).
append([H|T], Tail) ->
[H] ++ append(T, Tail);
append([], Tail) ->
[Tail].
sum([H|T]) -> H + sum(T);
sum([]) -> 0.
-module(mysort).
-export([sort/1]).
partition(_, [], Smaller, Larger) -> {Smaller, Larger};
partition(Pivot, [H|T], Smaller, Larger) ->
if H =< Pivot -> partition(Pivot, T, [H|Smaller], Larger);
H > Pivot -> partition(Pivot, T, Smaller, [H|Larger])
end.
sort([]) -> [];
sort([Pivot|Rest]) ->
{Smaller, Larger} = partition(Pivot, Rest, [], []),
sort(Smaller) ++ [Pivot] ++ sort(Larger).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment