I’m working on learning some Haskell (please excuse the newbie error)-
This routine errors out. My understanding of the do & <- syntax is that they extract the non-Monad type from the monad. So that understanding is flawed: what’s the correct understanding here?
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 }
First problem: The line
return fileexist || direxistis parsed as(return fileexist) || direxist, and you can’t passm Boolas the first argument of||. Change it toreturn (fileexist || direxist).Second problem: You claim the return type of
existerisBool, but the compiler infers that it must beIO Bool. Fix it. (Thedoand<-syntax let you extract theavalue from anm avalue, but only if you promise to return anm avalue.)