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