Created
March 8, 2010 06:50
-
-
Save jutememo/324943 to your computer and use it in GitHub Desktop.
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
-- ページ | |
data Page = Page [String] | |
-- 文字列をページに変換 | |
toPage :: String -> Page | |
toPage = Page . lines | |
-- 行番号のついたページ | |
data PageWithNumber = Pwn { numberWidth :: Int -- 行番号の幅 | |
, ls :: [(Int, String)] -- 行の内容 [(行番号, 内容)] | |
} | |
main = getContents >>= print . numbering . toPage | |
-- ページを行番号のついたページに変換 | |
numbering :: Page -> PageWithNumber | |
numbering (Page ls) = Pwn 6 numbering' | |
where | |
numbering' = zip [1..] ls | |
-- 行番号のついたページの出力方法 | |
instance Show PageWithNumber where | |
show (Pwn width ls) = unlines $ map lineToStr ls | |
where | |
lineToStr :: (Int, String) -> String | |
lineToStr (n, l) = numToStr n ++ " " ++ l | |
numToStr :: Int -> String | |
numToStr n = space ++ num | |
where | |
space = replicate (width - length num) ' ' | |
num = show n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment