从 IO ExitCode monad 获取字符串
我试图将作为参数给出的字符串(使用 getArgs )连接到 haskell 程序,例如: "rm " ++ filename ++ " filename2.txt"
位于 main = do
块内。
问题出在文件名的类型上,ghc 不会编译它,并给出错误。
我收到错误 无法将预期类型 [a] 与推断类型 IO ExitCode 匹配
我们尝试运行的代码是:
args <- getArgs
let inputfname = head args
system "rm -f "++ inputfname ++ " functions.txt"
I'm trying to concatenate a string given as an argument (using getArgs
) to the haskell program, e.g.:"rm " ++ filename ++ " filename2.txt"
which is inside a main = do
block.
The problem is with the type of filename, and ghc won't compile it, giving an error.
I get an error Couldn't match expected type [a] against inferred type IO ExitCode
the code we're trying to run is:
args <- getArgs
let inputfname = head args
system "rm -f "++ inputfname ++ " functions.txt"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要
$
:或括号:
否则您将尝试运行此:
它会失败,因为
++
想要[a]
(在本例中String
),但获取IO ExitCode
(来自system
)。You need
$
:Or parentheses:
Otherwise you’re trying to run this:
It fails because
++
wants[a]
(in this caseString
) but getsIO ExitCode
(fromsystem
).问题是函数应用程序的优先级高于
(++)
运算符,因此它会解析为您的意思是
或只是
The problem is that function application has higher precedence than the
(++)
operator, so it parses aswhile what you meant was
or simply
以下代码有效:
其他评论者解释了原因。
The following code works:
The reasons were explained by other commenters.