gpt4 book ai didi

haskell - 无法将预期类型 'Bool' 与类型 'm Bool' 匹配

转载 作者:行者123 更新时间:2023-12-02 13:37:14 25 4
gpt4 key购买 nike

两个模块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)

最佳答案

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

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 函数还可以使用模式匹配进行一些简化,事实上 bool 值上的 print 会打印 "True"“错误”

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

关于haskell - 无法将预期类型 'Bool' 与类型 'm Bool' 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7856567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com