无法匹配预期类型“Bool”;类型为“m Bool”;
两个模块 Up.hs 和 Down.hs
module Up (isSortedUp) where
isSortedUp x y z = if x>y && y>z then return(True) else return(False)
module Down (isSortedDown) where
isSortedDown x y z = if x<y && y<z then return(True) else return(False)
以及主程序 Main.hs
import System.Environment
import Up
import Down
main = do
args<-getArgs
let a = read(args !! 0)
let b = read(args !! 1)
let c = read(args !! 2)
if (isSortedUp a b c || isSortedDown a b c)
then putStrLn "True"
else putStrLn "False"
在编译过程中出现以下错误:
Couldn't match expected type `Bool' with actual type `m0 Bool'
In the return type of a call of `isSortedUp'
In the first argument of `(||)', namely `isSortedUp a b c '
In the expression: (isSortedUp a b c || isSortedDown a b c)
Two module Up.hs and Down.hs
module Up (isSortedUp) where
isSortedUp x y z = if x>y && y>z then return(True) else return(False)
module Down (isSortedDown) where
isSortedDown x y z = if x<y && y<z then return(True) else return(False)
And the main program Main.hs
import System.Environment
import Up
import Down
main = do
args<-getArgs
let a = read(args !! 0)
let b = read(args !! 1)
let c = read(args !! 2)
if (isSortedUp a b c || isSortedDown a b c)
then putStrLn "True"
else putStrLn "False"
During compilation I get the following error:
Couldn't match expected type `Bool' with actual type `m0 Bool'
In the return type of a call of `isSortedUp'
In the first argument of `(||)', namely `isSortedUp a b c '
In the expression: (isSortedUp a b c || isSortedDown a b c)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您似乎对
return
感到困惑。它不是像其他编程语言那样返回值的关键字。在 Haskell 中,return
是一个将纯值提升为一元值的函数(例如,将Int
提升为IO Int
)。您不能将其用于非单子代码。另外,您可以简单地编写
foo
,而不是编写if foo then True else False
:您的
main
函数也可以使用模式稍微简化匹配以及布尔值上的print
打印"True"
或"False"
的事实。You seem to be confused about
return
. It is not a keyword for returning values like in other programming languages. In Haskell,return
is a function that promotes a pure value to a monadic one (e.g. anInt
to anIO Int
). You don't use it for non-monadic code.Also, instead of writing
if foo then True else False
, you can simply writefoo
:Your
main
function can also be simplified a little using pattern matching and the fact thatprint
on booleans prints"True"
or"False"
.我不认为您的函数中需要“返回”。由于 isSorted 函数不会返回到 monad,它们只是评估函数,因此不需要包装(这就是 return 的作用)。您还可以通过一些解构来简化 let 语句。
我建议尝试:
I don't believe that you need the "return"s in your functions. As the isSorted functions don't return out to the monad, they simply evaluate the functions and therefore do not need to be wrapped up (and that's what return does). Also you can simplify your let statements with some deconstruction.
I would suggest trying:
不要在
isSorted
函数中调用return
函数。Don't call the
return
function in yourisSorted
functions.