Skip to content

Instantly share code, notes, and snippets.

@TheEmpty
Last active December 26, 2015 17:08
Show Gist options
  • Save TheEmpty/7184625 to your computer and use it in GitHub Desktop.
Save TheEmpty/7184625 to your computer and use it in GitHub Desktop.
Quick Sort in Erlang
-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.
$ 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]
@TheEmpty
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment