Haskell: 无法将预期类型 'IO b' 与实际类型 bool 匹配
Haskell: Couldn't match expected type 'IO b' with actual type bool
我收到以下错误:
Couldn't match expected type `IO b' with actual type `Bool'
In the expression:
upCheck
["########", "#11xxx3#", "#xx2xx3", "#002xx3", ....] 2 [2, 3, 4]
In the expression:
do { s <- readFile "projekt-board.txt";
let content = lines s;
printTable content;
printHelp;
.... }
In an equation for `gameFunc':
gameFunc
= do { s <- readFile "projekt-board.txt";
let content = ...;
printTable content;
.... }
根据 gameFunc
的定义:
gameFunc = do
s <- readFile "projekt-board.txt"
let content = lines s
printTable content
printHelp
upCheck ["########","#11xxx3#","#xx2xx3","#002xx3","#7x2x44#"] 2 [2,3,4]
和函数upCheck
:
upCheck :: [[Char]] -> Int -> [Int] -> Bool
upCheck tableArr number posArr = do
--posArr saadakse findBlock funktsiooniga
let arv = findVIndex (findVertIndex tableArr number)
let str = tableArr !! (posArr !! 0)
if ((posArr !! 0) == 1)
then False
else if arv == (-1)
then False
else if isHorizontal str number
then False
else if (tableArr !! ((posArr !! 0)-1)) !! arv /= 'x'
then False
else True
所以基本上这里发生的是 upCheck
是一个函数,用于检查某些条件是否匹配,以及当我从 ghci 中调用它的方式与我从函数中调用它时的方式相同 gameFunc
,我会得到一个结果True
。但是,如果我从函数 gameFunc
中调用它,则会出现上述错误。知道哪里出了问题吗?
问题是您的函数 gameFunc
使用了一个 do 表达式,它是 monad(在本例中是 IO
monad)的语法糖。您可以使用 return
函数将任何 non-monadic 结果变成一元结果:
return :: Monad m => a -> m a
因此您可以将 gameFunc
重写为(见最后一行):
gameFunc = do
s <- readFile "projekt-board.txt"
let content = lines s
printTable content
printHelp
return $ upCheck ["########","#11xxx3#","#xx2xx3","#002xx3","#7x2x44#"] 2 [2,3,4]
现在 gameFunc
的类型是 IO Bool
。您可以在其他 monadic 环境中使用 gameFunc
,例如:
main = do
b <- gameFunc
print b
为了打印gameFunc
的结果。
我收到以下错误:
Couldn't match expected type `IO b' with actual type `Bool'
In the expression:
upCheck
["########", "#11xxx3#", "#xx2xx3", "#002xx3", ....] 2 [2, 3, 4]
In the expression:
do { s <- readFile "projekt-board.txt";
let content = lines s;
printTable content;
printHelp;
.... }
In an equation for `gameFunc':
gameFunc
= do { s <- readFile "projekt-board.txt";
let content = ...;
printTable content;
.... }
根据 gameFunc
的定义:
gameFunc = do
s <- readFile "projekt-board.txt"
let content = lines s
printTable content
printHelp
upCheck ["########","#11xxx3#","#xx2xx3","#002xx3","#7x2x44#"] 2 [2,3,4]
和函数upCheck
:
upCheck :: [[Char]] -> Int -> [Int] -> Bool
upCheck tableArr number posArr = do
--posArr saadakse findBlock funktsiooniga
let arv = findVIndex (findVertIndex tableArr number)
let str = tableArr !! (posArr !! 0)
if ((posArr !! 0) == 1)
then False
else if arv == (-1)
then False
else if isHorizontal str number
then False
else if (tableArr !! ((posArr !! 0)-1)) !! arv /= 'x'
then False
else True
所以基本上这里发生的是 upCheck
是一个函数,用于检查某些条件是否匹配,以及当我从 ghci 中调用它的方式与我从函数中调用它时的方式相同 gameFunc
,我会得到一个结果True
。但是,如果我从函数 gameFunc
中调用它,则会出现上述错误。知道哪里出了问题吗?
问题是您的函数 gameFunc
使用了一个 do 表达式,它是 monad(在本例中是 IO
monad)的语法糖。您可以使用 return
函数将任何 non-monadic 结果变成一元结果:
return :: Monad m => a -> m a
因此您可以将 gameFunc
重写为(见最后一行):
gameFunc = do
s <- readFile "projekt-board.txt"
let content = lines s
printTable content
printHelp
return $ upCheck ["########","#11xxx3#","#xx2xx3","#002xx3","#7x2x44#"] 2 [2,3,4]
现在 gameFunc
的类型是 IO Bool
。您可以在其他 monadic 环境中使用 gameFunc
,例如:
main = do
b <- gameFunc
print b
为了打印gameFunc
的结果。