Skip to content

Instantly share code, notes, and snippets.

@jutememo
Created March 8, 2010 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jutememo/324943 to your computer and use it in GitHub Desktop.
Save jutememo/324943 to your computer and use it in GitHub Desktop.
-- ページ
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