同时从2 .txt文件中减去和求和

时间:2013-10-25 00:27:15

标签: haskell

如何通过只输入一次来获得l,以便同时从2 .txt文件执行2 func(减法和求和)?是否可以使用任何更高阶的功能?请感谢您的帮助。

transfer :: IO()
transfer = do

    k <- readFile "balance1.txt"
    b <- readFile "balance2.txt" --------read the second file------
    putStrLn "The amount that need to transfer"
    l <- getLine
    let n = read l::Int
    let a = read k::Int
    let c = read b::Int
    if ( n < a ) 
        then do
        let o = a - n
        let d = show o
            let e = n + c
        putStrLn "Your new balance is"
        putStrLn(d)
        writeFile "balance1.txt" d -----------modify 1st file--------
        writeFile "balance2.txt" e -----------modify 2nd file--------
        else do 
        putStrLn "Amount is not valid"

1 个答案:

答案 0 :(得分:1)

使用mapM

transfer = do
    [b1, b2] <- mapM readFile ["balance1.txt", "balance2.txt"]
    -- ...

请注意,这只是按顺序读取两个文本文件;无论如何,你可能不想要并发。