Skip to content

Instantly share code, notes, and snippets.

@baali
Created April 10, 2012 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save baali/2349226 to your computer and use it in GitHub Desktop.
Save baali/2349226 to your computer and use it in GitHub Desktop.
Permutation of List in Erlang
-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