[Haskell-cafe] file line operation perhaps need loop

Bernard Pope bjpop at cs.mu.OZ.AU
Wed Jul 20 03:36:11 EDT 2005


On Wed, 2005-07-20 at 14:27 +0800, Sun Yi Ming wrote:

[snip]

> i first write this snippet of code:
> ---
> import System.IO
>     
> mix :: [a] -> [a] -> [a]
> mix [] ys = ys
> mix xs [] = xs
> mix (x:xs) (y:ys) = [x,y] ++ mix xs ys
> 
> f1 = do contents1 <- readFile "url1.txt"
>         contents2 <- readFile "url2.txt"
>         let urls1 = lines contents1
>             urls2 = lines contents2
>             urls = mix urls1 urls2
>         writeFile "aha.txt" (unlines urls)
> --
> this works fine, but i think if the two file are very big, and the readFile will consume too many mem.so i need to read the file line by line but stunned by the loop in IO Monad:

Did you try it on a big file to see what happens?

There should not be any problem because readFile is lazy. That is it
reads the contents of the file on demand, not all at once. The only
thing you have to be careful about is that you do not require all the
contents of the file before any output can be produced. 

Bernie.




More information about the Haskell-Cafe mailing list