Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Erlang (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Quicksort=== [[Quicksort]] in Erlang, using [[list comprehension]]:<ref>{{cite web |url=http://erlang.org/doc/programming_examples/list_comprehensions.html |title=Erlang β List Comprehensions |website=erlang.org}}</ref> <syntaxhighlight lang="erlang"> %% qsort:qsort(List) %% Sort a list of items -module(qsort). % This is the file 'qsort.erl' -export([qsort/1]). % A function 'qsort' with 1 parameter is exported (no type, no name) qsort([]) -> []; % If the list [] is empty, return an empty list (nothing to sort) qsort([Pivot|Rest]) -> % Compose recursively a list with 'Front' for all elements that should be before 'Pivot' % then 'Pivot' then 'Back' for all elements that should be after 'Pivot' qsort([Front || Front <- Rest, Front < Pivot]) ++ [Pivot] ++ qsort([Back || Back <- Rest, Back >= Pivot]). </syntaxhighlight> The above example recursively invokes the function <code>qsort</code> until nothing remains to be sorted. The expression <code>[Front || Front <- Rest, Front < Pivot]</code> is a [[list comprehension]], meaning "Construct a list of elements <code>Front</code> such that <code>Front</code> is a member of <code>Rest</code>, and <code>Front</code> is less than <code>Pivot</code>." <code>++</code> is the list concatenation operator. A comparison function can be used for more complicated structures for the sake of readability. The following code would sort lists according to length: <syntaxhighlight lang="erlang"> % This is file 'listsort.erl' (the compiler is made this way) -module(listsort). % Export 'by_length' with 1 parameter (don't care about 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) -> length(A) < length(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). </syntaxhighlight> A <code>Pivot</code> is taken from the first parameter given to <code>qsort()</code> and the rest of <code>Lists</code> is named <code>Rest</code>. Note that the expression <syntaxhighlight lang="erlang">[X || X <- Rest, Smaller(X,Pivot)]</syntaxhighlight> is no different in form from <syntaxhighlight lang="erlang">[Front || Front <- Rest, Front < Pivot]</syntaxhighlight> (in the previous example) except for the use of a comparison function in the last part, saying "Construct a list of elements <code>X</code> such that <code>X</code> is a member of <code>Rest</code>, and <code>Smaller</code> is true", with <code>Smaller</code> being defined earlier as <syntaxhighlight lang="erlang">fun(A,B) -> length(A) < length(B) end</syntaxhighlight> The [[anonymous function]] is named <code>Smaller</code> in the parameter list of the second definition of <code>qsort</code> so that it can be referenced by that name within that function. It is not named in the first definition of <code>qsort</code>, which deals with the base case of an empty list and thus has no need of this function, let alone a name for it.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)