Created
April 10, 2012 08:05
-
-
Save baali/2349226 to your computer and use it in GitHub Desktop.
Permutation of List 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('permute_list'). | |
-export([permute/1, permute/2]). | |
permute(List) -> | |
permute(List, length(List)). | |
permute(List, Length) -> | |
Indices = [], | |
Permuted_List = [], | |
jumble(List, Permuted_List, Indices, Length). | |
jumble(_, Permuted_List, _, 0) -> | |
Permuted_List; | |
jumble(List, Permuted_List, Indices, Length) -> | |
Rand_Ind = random:uniform(length(List)), | |
case lists:member(Rand_Ind, Indices) of | |
true -> | |
jumble(List, Permuted_List, Indices, Length); | |
false -> | |
New_Permutation = lists:append(Permuted_List, [lists:nth(Rand_Ind, List)]), | |
New_Indices = lists:append(Indices, [Rand_Ind]), | |
jumble(List, New_Permutation, New_Indices, Length - 1) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment