管理IO monad [英] Managing the IO monad
问题描述
我正在学习一些Haskell(请原谅新手错误) -
这个例程出错了。我对do& < - 语法是它们从monad中提取非Monad类型。这样的理解是有缺陷的:这里有什么正确的理解?
exister :: String - > Bool
exister path = do $ b $ fileexist< - doesFileExist path
direxist< - doesDirectoryExist path
return fileexist || direxist
错误
ghc -o joiner joiner.hs
joiner.hs:53:2:
无法与推测类型'Bool'$ b匹配预期类型'Bool' $ b在'(||)'的第一个参数中,即`return fileexist'
在表达式中:return fileexist || direxist
在表达式中:
do {fileexist< - doesFileExist path;
direxist< - doesDirectoryExist路径;
返回fileexist ||第一个问题:行<$ c $ <$ c $>
(return fileexist)|| direxist
,并且不能将 m Bool
作为 ||
的第一个参数传递。 。将其更改为 return(fileexist || direxist)
。第二个问题:您声明返回类型 exister
是 Bool
,但编译器推断它必须是 IO Bool
。修理它。 ( do
和< -
语法可让您提取 a
值来自 ma
值,但前提是您承诺返回 ma
值。) 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 || direxist
is parsed as (return fileexist) || direxist
, and you can’t pass m Bool
as the first argument of ||
. Change it to return (fileexist || direxist)
.
Second problem: You claim the return type of exister
is Bool
, but the compiler infers that it must be IO Bool
. Fix it. (The do
and <-
syntax let you extract the a
value from an m a
value, but only if you promise to return an m a
value.)
这篇关于管理IO monad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!