Haskell Quiz/Splitting the Loot/Solution TomPlick
From HaskellWiki
partitionSet
partitionSet [1, 2]
[([1,2],[]),([2],[1]),([1],[2]),([],[1,2])]
splitLoot
leftovers /= 0
mine
restOfJewels
mine
restOfJewels
splitLoot
x <- y
x
y
splitLoot
head
import Control.Monad.List partitionSet :: [a] -> [([a], [a])] partitionSet [] = [([], [])] partitionSet (x:xs) = concat [[(x:p1, p2), (p1, x:p2)] | (p1, p2) <- partitionSet xs] type Distribution = [[Integer]] splitLoot :: Integer -> [Integer] -> [Distribution] splitLoot 0 [] = return [] splitLoot 0 _ = mzero splitLoot numPeople jewels = let (share, leftovers) = (sum jewels) `quotRem` numPeople in if leftovers /= 0 then [] else do (mine, restOfJewels) <- partitionSet jewels if (sum mine) == share then do restOfDivision <- splitLoot (numPeople - 1) restOfJewels return (mine : restOfDivision) else mzero
