我正在尝试将作为参数(使用getArgs)给出的字符串连接到haskell程序,例如:
"rm " ++ filename ++ " filename2.txt"块内的main = do

问题在于文件名的类型,ghc不会编译它,从而导致错误。

我收到一个错误Couldn't match expected type [a] against inferred type IO ExitCode

我们试图运行的代码是:

args <- getArgs
let inputfname = head args
system "rm -f "++ inputfname ++ " functions.txt"

最佳答案

您需要$

system $ "rm -f "++ inputfname ++ " functions.txt"


或括号:

system ("rm -f " ++ inputfname ++ " functions.txt")


否则,您将尝试运行以下命令:

(system "rm -f ") ++ inputfname ++ " functions.txt"


它失败是因为++想要[a](在这种情况下为String),但要得到IO ExitCode(从system)。

09-26 15:10