我正在学习一些Haskell(请原谅我的新手错误)-
这个程序失败了。 我对 do 和 <- 语法的理解是它们从单子中提取非单子类型。 所以这种理解是有缺陷的:这里的正确理解是什么?
exister :: String -> Bool
exister path = do
fileexist <- doesFileExist path
direxist <- doesDirectoryExist path
return fileexist || direxist
错误
ghc -o joiner joiner.hs
joiner.hs:53:2:
Couldn't match expected type `Bool' against inferred type `m Bool'
In the first argument of `(||)', namely `return fileexist'
In the expression: return fileexist || direxist
In the expression:
do { fileexist <- doesFileExist path;
direxist <- doesDirectoryExist path;
return fileexist || direxist }
return
与大多数语言中的“返回”并不相似可能会有所帮助。如果你想要一个以数字n为参数,然后“返回”n+1的函数,你应该写成f n = n + 1
,而不是f n = return n+1
。 - Tyler