管理 IO 单子

2023-11-24

我正在学习一些 Haskell(请原谅新手错误)-

这个例程出错了。我对 do &

exister :: String -> Bool
exister path = do
  fileexist <- doesFileExist path 
  direxist <- doesDirectoryExist path
  return fileexist || direxist 

error

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 fileexist || direxist被解析为(return fileexist) || direxist,并且你无法通过m Bool作为第一个参数||。将其更改为return (fileexist || direxist).

第二个问题:您声明的返回类型exister is Bool,但编译器推断它必须是IO Bool。修理它。 (这do and <-语法让你提取a值来自m a值,但前提是您承诺返回m a value.)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

管理 IO 单子 的相关文章

随机推荐