Skip to content

Instantly share code, notes, and snippets.

@nobsun
Created December 10, 2012 12:19
Show Gist options
  • Save nobsun/4250255 to your computer and use it in GitHub Desktop.
Save nobsun/4250255 to your computer and use it in GitHub Desktop.
逆順ナンバリング ref: http://qiita.com/items/e57d03a92ad6d0ffe0ac
テキストファイルを指定するとそのファイル名と,行番号をふった内容を印字するプログラムを作成せよ.
ただし,ファイルは複数指定できるものとし,行番号は(そのファイルでの)残りの行数を表す数である.
(対象ファイルがn行からなる場合,n-1から0までの番号が降順にふられる.)
module Main where
import System.Environment (getArgs)
main :: IO ()
main = mapM_ revNumFileProc =<< getArgs
revNumFileProc :: FilePath -> IO ()
revNumFileProc = undefined
numberings = map (uncurry (printf "%06d: %s")) . zip [0..]
map (uncurry f) . zip xs = zipWith f xs
numberings = zipWith (printf "%06d: %s") [0..]
module Main where
import System.Environment (getArgs)
main :: IO ()
main = mapM_ revNumFileProc =<< getArgs
revNumFileProc :: FilePath -> IO ()
revNumFileProc = uncurry (>>) . pair (putFileName, revNumFile)
putFileName :: FilePath -> IO ()
putFileName = putStrLn
revNumFile :: FilePath -> IO ()
revNumFile fn = putStr . unlines . revnumberings . lines =<< readFile fn
revnumberings :: [String] -> [String]
revnumberings = reverse . numberings . reverse
numberings :: [String] -> [String]
numberings = zipWith numbering [0..]
numbering :: Int -> String -> String
numbering = printf "%06d: %s"
-- Utility
pair :: (a -> b, a -> c) -> a -> (b, c)
pair (f,g) x = (f x, g x)
numbering :: Int -> String -> (Int,String)
numbering i s = (i+1,printf "%06d: %s" i s)
numberings = snd . mapAccumL numbering 0
revnumberings = reverse . numberings . reverse
行 : 行 ... : 行 : []
↓ ↓ ↓ ↓
n ← number ← n-1 ← number ←...← 1 ← number ← 0 ← number ← 0
↓ ↓ ↓ ↓
番号++行 : 番号++行 : 番号++行 : []
revnumberings = snd . mapAccumR numbering 0
pair :: (a -> b, a -> c) -> a -> (b,c)
pair (f,g) x = (f x, g x)
revNumFileProc :: FilePath -> IO ()
revNumFileProc = uncurry (>>) . pair (putFileName, revNumFile)
module Main where
import System.Environment (getArgs)
main :: IO ()
main = mapM_ revNumFileProc =<< getArgs
revNumFileProc :: FilePath -> IO ()
revNumFileProc = uncurry (>>) . pair (putFileName, revNumFile)
putFileName :: FilePath -> IO ()
putFileName = undefined
revNumFile :: FilePath -> IO ()
revNumFile = undefined
-- Utility
pair :: (a -> b, a -> c) -> a -> (b, c)
pair (f,g) x = (f x, g x)
revNumFile :: FilePath -> IO ()
revNumFile fn = putStr . unlines . revnumbering . lines =<< readFile fn
module Main where
import System.Environment (getArgs)
main :: IO ()
main = mapM_ revNumFileProc =<< getArgs
revNumFileProc :: FilePath -> IO ()
revNumFileProc = uncurry (>>) . pair (putFileName, revNumFile)
putFileName :: FilePath -> IO ()
putFileName = putStrLn
revNumFile :: FilePath -> IO ()
revNumFile fn = putStr . unlines . revnumberings . lines =<< readFile fn
revnumberings :: [String] -> [String]
revnumberings = undefined
-- Utility
pair :: (a -> b, a -> c) -> a -> (b, c)
pair (f,g) x = (f x, g x)
revnumberings :: [String] -> [String]
revnumberings = reverse . numberings . reverse
numberings :: [String] -> [String]
numberings = map numbering . zip [0..]
numbering :: (Int,String) -> String
numbering (i,s) = printf "%06d: %s" i s
numbering = uncurry (printf "%06d: %s")
module Main where
import Data.List (mapAccumR)
import System.Environment (getArgs)
import Text.Printf (printf)
main :: IO ()
main = mapM_ revNumFileProc =<< getArgs
revNumFileProc :: FilePath -> IO ()
revNumFileProc = uncurry (>>) . pair (putFileName, revNumFile)
putFileName :: FilePath -> IO ()
putFileName = putStrLn
revNumFile :: FilePath -> IO ()
revNumFile fn = putStr . unlines . revnumberings . lines =<< readFile fn
revnumberings :: [String] -> [String]
revnumberings = snd . mapAccumR numbering 0
numbering :: Int -> String -> (Int,String)
numbering i s = (i+1, printf "%06d: %s" i s)
-- Utility
pair :: (a -> b, a -> c) -> a -> (b, c)
pair (f,g) x = (f x, g x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment