Skip to content

Instantly share code, notes, and snippets.

@msysyamamoto
Last active December 23, 2015 22:59
Show Gist options
  • Select an option

  • Save msysyamamoto/6707035 to your computer and use it in GitHub Desktop.

Select an option

Save msysyamamoto/6707035 to your computer and use it in GitHub Desktop.
ナムドット問題。自力では解けなかったので、解説をもとに実装してみた。 https://codeiq.jp/ace/yuki_hiroshi/q468
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