Permutations/Rank of a permutation: Difference between revisions
m
→{{header|Haskell}}: Simplified `foldr (*) 1` to `product`
m (→{{header|Haskell}}: Simplified `foldr (*) 1` to `product`) |
|||
Line 675:
=={{header|Haskell}}==
Without the random part.
<lang haskell>fact
fact n = product [1 .. n]
▲-- always assume elements are unique
rankPerm [] _ = []
rankPerm list n = c : rankPerm (a ++ b) r
where
(a, c:b) = splitAt q list
permRank [] = 0
permRank (x:xs) = length (filter (< x) xs) * fact (length xs) + permRank xs
main
main = mapM_ f [0 .. 23]
f n = print (n, p, permRank p) where▼
where
p = rankPerm [0..3] n</lang>▼
where
{{out}}
from rank to permutation back to rank:
|