This commit is contained in:
2021-12-03 17:18:39 +00:00
parent b41e3e3637
commit a168a335b9
3 changed files with 70 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
{-# LANGUAGE TupleSections #-}
module Multiset where
import qualified Data.Map.Strict as M
import Control.Monad
powlistN :: Int -> [a] ->[[a]]
powlistN _ [] = [[]]
powlistN n (x:xs)
| n <=0 = [[]]
| otherwise = ((x:) <$> powlistN (n-1) xs) ++ powlistN n xs
-- exponential, so don't use it on long lists
powlist :: [a] -> [[a]]
powlist = filterM (const [True,False])
toMultiset :: Ord a => [a] -> M.Map a Int
toMultiset = foldr (uncurry $ M.insertWith (+)) M.empty . map (,1)