1

如何通过仅键入一次来同时从 2 个 .txt 文件中执行 2 func(减法和求和)来获取 l?可以使用任何更高阶的功能吗?感谢您的帮助。

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"
4

1 回答 1

1

使用mapM

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

请注意,这只是按顺序读取两个文本文件;无论如何,您可能不希望在这里并发。

于 2013-10-25T15:23:57.063 回答