我正在学习一些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 fileexist || direxist行被解析为(return fileexist) || direxist,并且您不能将m Bool作为||的第一个参数传递。将其更改为return (fileexist || direxist)

第二个问题:您声称exister的返回类型是Bool,但是编译器推断它必须是IO Bool。修理它。 (do<-语法使您可以从a值中提取m a值,但前提是您答应返回m a值。)

10-08 12:48