[ create a new paste ] login | about

Link: http://codepad.org/wH6gJRSo    [ raw code | output | fork | 2 comments ]

mohit_at_codepad - Haskell, pasted on Mar 20:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- Written by Mohit jain
-- Read a file and prepend line number to each line
module Main where

import Control.Monad.State

contents = unlines ["line 1", "line 2", "line 3"]

main   = do
   (st, ad) <- runStateT(addLineNumber(lines contents)) 0
   putStr  $ unlines st

addLineNumber    :: [String]  -> StateT Int IO [String]
addLineNumber    = f where
    f       = mapM g
    g li    = do
              modify (+1)
              liftM(h li) get
    h li ln = show ln ++ ": " ++ li


Output:
1
2
3
1: line 1
2: line 2
3: line 3


Create a new paste based on this one


Comments:
posted by mohit_at_codepad on Mar 21
If you pass file contents using << on command line, use following lines to read file contents.

import System.Environment
cn <- getContents

you can run the file by
$ ./a.out < input.txt > output.txt
reply
posted by mohit_at_codepad on Mar 21
contents <- getContents
reply