Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % This is file 'listsort.erl' (the compiler is made this way)
- -module(listsort).
- % Export 'by_length' with 1 parameter (don't care of the type and name)
- -export([by_length/1]).
- by_length(Lists) -> % Use 'qsort/2' and provides an anonymous function as a parameter
- qsort(Lists, fun(A,B) -> A < B end).
- qsort([], _)-> []; % If list is empty, return an empty list (ignore the second parameter)
- qsort([Pivot|Rest], Smaller) ->
- % Partition list with 'Smaller' elements in front of 'Pivot' and not-'Smaller' elements
- % after 'Pivot' and sort the sublists.
- qsort([X || X <- Rest, Smaller(X,Pivot)], Smaller)
- ++ [Pivot] ++
- qsort([Y || Y <- Rest, not(Smaller(Y, Pivot))], Smaller).
Add Comment
Please, Sign In to add comment