我遇到了Haskell脚本的问题。我正在尝试通过我在网上找到的问题来学习Haskell。我得到的输入是: Int - >测试用例数 S1->每个测试用例的字符串1 S2->每个测试用例的字符串2
每个S1和S2是一个以空格分隔的数字串。我将它们转换为具有函数strToIntList
的Int列表。然后我想处理两个列表并根据结果返回一个整数值。我收到以下错误:第24行Couldn't match type 'IO Integer' with 'Int'
,我盯了很长时间但却无法找出原因(帖子末尾的完整错误)。
如果有人能解释我出错的原因,我将非常感激。
这是我的剧本:
import Data.List
import Data.List.Split
main = do
cases <- readLn
doLoop cases
toInt x = read x :: Int
strToIntList s = [read x :: Int | x <- (splitOn " " s)]
minOfTwo :: Int
minOfTwo = do
sa <- getLine
sb <- getLine
return $ minimum [1..50]
-- return $ minimum $ strToIntList sa
doLoop 0 = return ()
doLoop loopIndex = do
q <- getLine
let c = minOfTwo
print(c)
doLoop (loopIndex-1)
这是我得到的完整错误:
Couldn't match type `IO Integer' with `Int'
Expected type: IO String -> (String -> IO Integer) -> Int
Actual type: IO String -> (String -> IO Integer) -> IO Integer
In a stmt of a 'do' block: sa <- getLine
In the expression:
do { sa <- getLine;
sb <- getLine;
return $ minimum [1 .. 50] }
In an equation for `minOfTwo':
minOfTwo
= do { sa <- getLine;
sb <- getLine;
return $ minimum [1 .. 50] }
答案 0 :(得分:2)
getLine
函数位于IO
monad中,因此调用getLine
的任何函数也必须位于IO
monad中。将minOfTwo
的类型签名从Int
更改为IO Int
,该特定问题将会消失。
(您还需要将let c = minOfTwo
更改为c <- minOfTwo
。)
可能存在其他错误,但这是导致错误消息的错误。