Created
July 19, 2013 19:14
-
-
Save melrief/6041646 to your computer and use it in GitHub Desktop.
Lazily prepend the line number to each line
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
import Control.Monad (unless) | |
import Prelude hiding (catch) | |
import System.IO (isEOF) | |
prependLineNums :: Int -> IO () | |
prependLineNums n = isEOF >>= flip unless prependLineNum | |
where prependLineNum = getLine >>= putStrLn . (++) (padTo 5 $ show n) | |
>> prependLineNums (n+1) | |
padTo c s = case c `compare` length s of | |
GT -> s ++ replicate (c - length s) ' ' | |
_ -> take c s ++ " " | |
main :: IO () | |
main = prependLineNums 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment