从IO ExitCode monad获取字符串

mel*_*amy 2 io monads haskell ghc

我正在尝试将作为参数(使用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"
Run Code Online (Sandbox Code Playgroud)

Jon*_*rdy 7

你需要$:

system $ "rm -f "++ inputfname ++ " functions.txt"
Run Code Online (Sandbox Code Playgroud)

或括号:

system ("rm -f " ++ inputfname ++ " functions.txt")
Run Code Online (Sandbox Code Playgroud)

否则你试图运行这个:

(system "rm -f ") ++ inputfname ++ " functions.txt"
Run Code Online (Sandbox Code Playgroud)

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