无法匹配预期类型“Bool”;类型为“m Bool”;

发布于 2024-12-11 10:27:07 字数 865 浏览 1 评论 0原文

两个模块 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月蹉跎了容颜 2024-12-18 10:27:07

您似乎对return感到困惑。它不是像其他编程语言那样返回值的关键字。在 Haskell 中,return 是一个将纯值提升为一元值的函数(例如,将 Int 提升为 IO Int)。您不能将其用于非单子代码。

isSortedUp x y z = if x>y && y>z then True else False

另外,您可以简单地编写 foo,而不是编写 if foo then True else False

isSortedUp x y z = x>y && y>z

您的 main 函数也可以使用模式稍微简化匹配以及布尔值上的 print 打印 "True""False" 的事实。

main = do
  (a:b:c:_) <- getArgs
  print (isSortedUp a b c || isSortedDown a b c)

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. an Int to an IO Int). You don't use it for non-monadic code.

isSortedUp x y z = if x>y && y>z then True else False

Also, instead of writing if foo then True else False, you can simply write foo:

isSortedUp x y z = x>y && y>z

Your main function can also be simplified a little using pattern matching and the fact that print on booleans prints "True" or "False".

main = do
  (a:b:c:_) <- getArgs
  print (isSortedUp a b c || isSortedDown a b c)
淡淡離愁欲言轉身 2024-12-18 10:27:07

我不认为您的函数中需要“返回”。由于 isSorted 函数不会返回到 monad,它们只是评估函数,因此不需要包装(这就是 return 的作用)。您还可以通过一些解构来简化 let 语句。

我建议尝试:

import System.Environment

isSortedUp x y z = x>y && y>z
isSortedDown x y z = x<y && y<z

main = do
  args<-getArgs
  let (a:b:c:xs) = args
  if ((isSortedUp a b c) || (isSortedDown a b c)) then putStrLn "True" else putStrLn "False"

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:

import System.Environment

isSortedUp x y z = x>y && y>z
isSortedDown x y z = x<y && y<z

main = do
  args<-getArgs
  let (a:b:c:xs) = args
  if ((isSortedUp a b c) || (isSortedDown a b c)) then putStrLn "True" else putStrLn "False"
零度° 2024-12-18 10:27:07

不要在 isSorted 函数中调用 return 函数。

Don't call the return function in your isSorted functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文