[Haskell-cafe] file line operation perhaps need loop

Benjamin Franksen benjamin.franksen at bessy.de
Wed Jul 20 12:25:05 EDT 2005


On Wednesday 20 July 2005 08:27, Sun Yi Ming wrote:
> Hello,
>   I have two txt file,and i want to mix the two files line by line,
> [...]
> 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.

No. Both files are read lazily (on demand). THis is how 'readFile' is 
specified. The program should work fine even with very large files. Try 
it.

Ben


More information about the Haskell-Cafe mailing list