Last active
December 26, 2015 17:08
-
-
Save TheEmpty/7184625 to your computer and use it in GitHub Desktop.
Quick Sort in Erlang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(misc_lib). % namespace (like a class name, but modules only store functions) | |
-export([quickSort/1]). % what methods are *public*ly available in this module | |
quickSort([]) -> []; % base case | |
quickSort([Pivot | Data]) -> % Take first element (head) of array, call it Pivot, rest of array (tail), Data | |
Left = quickSort( % recursion, for each Item, return Item, in Data, where Item < Pivot | |
[Item || Item <- Data, Item < Pivot] | |
), | |
Right = [Pivot | quickSort( % same, but, Item >= Pivot, and we prepend (using pipe) with the pivot. | |
[Item || Item <- Data, Item >= Pivot] | |
)], | |
lists:append(Left, Right). % add Left and Right lists together. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ erl | |
1> c(misc_lib). % compile module | |
{ok,misc_lib} | |
2> misc_lib:quickSort([1, 9, 6, 3, 0, 5, 2, 5082, 5, 2]). | |
[0,1,2,2,3,5,5,6,9,5082] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The way I currently see it:
.
is end of definition,;
is end of pattern, but not definition (see line 4),,
is then like a;
in other languages, end of a general statement, but isn't the end of a higher scope.