Created
March 29, 2012 06:56
-
-
Save yihuang/2234342 to your computer and use it in GitHub Desktop.
split integer.
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 System.Environment | |
import Data.List (intersperse) | |
split :: Int -> [[Int]] | |
split x = go x 1 | |
where | |
go 0 _ = [[]] | |
go n i = flip concatMap [i..n] $ \i' -> | |
map (i':) $ go (n-i') i' | |
format :: Int -> [Int] -> String | |
format n l = (show n) ++ "=" ++ intersperse '+' (concatMap show l) | |
main = do | |
[n'] <- getArgs | |
let n=read n' | |
mapM_ putStrLn $ map (format n) $ filter ((>1) . length) $ split n | |
{- | |
> ./Main 4 | |
4=1+1+1+1 | |
4=1+1+2 | |
4=1+3 | |
4=2+2 | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment