-- | Calculs de combinaisons. module Relotophile.Combin where import Data.Bool import Data.Eq (Eq(..)) import Data.Foldable (any, sum) import Data.Int (Int) import Data.List ((!!), length) import Data.Ord (Ord(..)) import Prelude (Integral(..), Num(..), pred, undefined) -- | @'nCk' n k@ retourne le nombre de combinaisons -- de longueur 'k' d’un ensemble de longueur 'n'. -- -- Computed using the formula: -- @'nCk' n (k+1) == 'nCk' n (k-1) * (n-k+1) / k@ nCk :: Integral i => i -> i -> i n`nCk`k | n<0||k<0||n, p.26 combinOfRank :: Integral i => i -> i -> i -> [i] combinOfRank n k rk | rk<0||n`nCk`k, pp.24-25 -- -- @ -- 'rankOfCombin' n ('combinOfRank' n k r) == r -- 'combinOfRank' n ('length' ns) ('rankOfCombin' n ns) == ns -- @ rankOfCombin :: Integral i => i -> [i] -> i rankOfCombin n ns | any (\x -> x<1||n [a] -> [a] permute ps xs = [xs !! pred p | p <- ps]