Last active
December 23, 2015 22:59
-
-
Save msysyamamoto/6707035 to your computer and use it in GitHub Desktop.
ナムドット問題。自力では解けなかったので、解説をもとに実装してみた。
https://codeiq.jp/ace/yuki_hiroshi/q468
This file contains hidden or 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 Control.Applicative ((<$>)) | |
| import Data.List (intercalate) | |
| import System.Environment (getArgs) | |
| main :: IO () | |
| main = do | |
| n <- read . (!! 0) <$> getArgs | |
| let (h, ts) = splitAt 1 . map show $ take n [1..] | |
| decode = foldl (\acc x -> injects x acc) [h] ts | |
| mapM_ (putStrLn . intercalate ".") decode | |
| injects :: String -> [[String]] -> [[String]] | |
| injects _ [] = [] | |
| injects x (y:ys) = interleave x y ++ injects x ys | |
| -- 「プログラミングHaskell」の11章を参考にした | |
| interleave :: String -> [String] -> [[String]] | |
| interleave x [] = [[x]] | |
| interleave x (y:ys) = ((y ++ x):ys) : map (y:) (interleave x ys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment