Difference between revisions of "FP Laboratory 12"
Jump to navigation
Jump to search
(Marked this version for translation) |
|||
Line 1: | Line 1: | ||
<translate> | <translate> | ||
− | == Working with files - monad IO == | + | == Working with files - monad IO == <!--T:1--> |
* Create a program that reads a file and number it's lines and writes it on the screen. | * Create a program that reads a file and number it's lines and writes it on the screen. | ||
</translate> | </translate> |
Revision as of 10:50, 21 October 2021
Working with files - monad IO
- Create a program that reads a file and number it's lines and writes it on the screen.
import System.IO
import Control.Exception
main = do fromHandle <- opf "Read from: " ReadMode
contents <- hGetContents fromHandle
putStr (numberLines contents)
hClose fromHandle
opf :: String -> IOMode -> IO Handle
opf prompt mode = do putStr prompt
name <- getLine
catch (openFile name mode )
(\e -> do putStr ("Can't open "++name++":"++show (e :: IOException) ++"\n")
opf prompt mode)
numberLines::String -> String
numberLines text = let l = lines text
numbered = zip [1..] l
result = [show x ++".\t"++content++"\n" | (x, content)<-numbered]
in concat result