
Hello, I have two txt file,and i want to mix the two files line by line, e.g. $ cat url1.txt url1_1.line url1_2.line $ cat url2.txt url2_1.line url2_2.line and i want this file as result: $ cat aha.txt url1_1.line url2_1.line url1_2.line url2_2.line 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: --- main = do h1 <- openFile "url1.txt" ReadMode h2 <- openFile "url2.txt" ReadMode line1 <- hGetLine h1 line2 <- hGetLine h2 print $ line1 : line2 : [] -- i don't howto do hClose h1 hClose h2 -- any ideas? thank you all. -- Sun Yi Ming