Skip to content

Instantly share code, notes, and snippets.

@melrief
Created July 19, 2013 19:14
Show Gist options
  • Save melrief/6041646 to your computer and use it in GitHub Desktop.
Save melrief/6041646 to your computer and use it in GitHub Desktop.
Lazily prepend the line number to each line
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