Skip to content

Instantly share code, notes, and snippets.

@yihuang
Created March 29, 2012 06:56
Show Gist options
  • Save yihuang/2234342 to your computer and use it in GitHub Desktop.
Save yihuang/2234342 to your computer and use it in GitHub Desktop.
split integer.
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