Guest User

Untitled

a guest
Jun 19th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.70 KB | None | 0 0
  1. % This is file 'listsort.erl' (the compiler is made this way)
  2. -module(listsort).
  3. % Export 'by_length' with 1 parameter (don't care of the type and name)
  4. -export([by_length/1]).
  5.  
  6. by_length(Lists) -> % Use 'qsort/2' and provides an anonymous function as a parameter
  7.    qsort(Lists, fun(A,B) -> A < B end).
  8.  
  9. qsort([], _)-> []; % If list is empty, return an empty list (ignore the second parameter)
  10. qsort([Pivot|Rest], Smaller) ->
  11.     % Partition list with 'Smaller' elements in front of 'Pivot' and not-'Smaller' elements
  12.     % after 'Pivot' and sort the sublists.
  13.     qsort([X || X <- Rest, Smaller(X,Pivot)], Smaller)
  14.     ++ [Pivot] ++
  15.     qsort([Y || Y <- Rest, not(Smaller(Y, Pivot))], Smaller).
Add Comment
Please, Sign In to add comment