[Haskell-cafe] algorithm-for-finding-numerical-permutation-given-lexicographic-index

Lone Wolf amslonewolf at gmail.com
Wed Apr 3 18:38:18 CEST 2013


>
>
> http://stackoverflow.com/questions/8940470/algorithm-for-finding-numerical-permutation-given-lexicographic-index
>
> How would you rewrite this into Haskell?  The code snippet is in Scala.
>
> /**
>     example: index:=15, list:=(1, 2, 3, 4)
> */
> def permutationIndex (index: Int, list: List [Int]) : List [Int] =
>   if (list.isEmpty) list else {
>     val len = list.size     // len = 4
>     val max = fac (len)     // max = 24
>     val divisor = max / len // divisor = 6
>     val i = index / divisor // i = 2
>     val el = list (i)
>     el :: permutationIndex (index - divisor * i, list.filter (_ != el)) }
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130403/c062de1b/attachment.htm>


More information about the Haskell-Cafe mailing list