【发布时间】:2014-03-02 15:42:54
【问题描述】:
我想创建一个函数,它接受一个字符串“path”,这是一个只有一行的文件的路径,我想取这一行并检查它是否是一个正确的表达式,是否要构建一个从这个字符串中取出树,这是代码 `
loadExpression :: String -> Tree Char
loadExpression path = do
contents <- readFile path
if checkIfProper $ filter (/=' ') contents
then buildTreeFromString contents
else EmptyTree
`
但它给了我错误 "Couldn't match type IO' withTree'" 。我知道 IO 字符串与普通字符串不同,但 <- 不应该这样做吗?将 IO 字符串转换为普通字符串。如果我用"(1+2)*3" 之类的字符串调用buildTreeFromString,它可以正常工作,checkIfProper 也是如此。
整个错误是:
Couldn't match type `IO' with `Tree'
Expected type: Tree String
Actual type: IO String
In the return type of a call of `readFile'
In a stmt of a 'do' block: contents <- readFile path
【问题讨论】:
标签: string haskell types match loading