Created
September 8, 2018 21:50
-
-
Save lalabuy948/ab6a64d8951ed2eb9f689ccb58333092 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
import Data.Ord | |
partitions :: [a] -> [([a], [a])] | |
partitions [] = [([], [])] | |
partitions (x : xs) = let ps = partitions xs in | |
[(x : ys, zs) | (ys, zs) <- ps] ++ [(ys, x : zs) | (ys, zs) <- ps] | |
unbalance :: Num a => ([a], [a]) -> a | |
unbalance (ys, zs) = abs (sum ys - sum zs) | |
balanced :: (Num a, Ord a) => [a] -> ([a], [a]) | |
balanced = minimumBy (comparing unbalance) . partitions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment