我有一个Haskell脚本的问题。我正在尝试通过做我在网上找到的问题来学习Haskell。我得到的输入是: Int -> Number of test case对于每个测试用例,S1 -> String 1,对于每个测试用例,S2 -> String 2
每个S1和S2都是一个空格分隔的数字字符串。我使用函数strToIntList
将它们转换为一个整数列表。然后,我想要处理这两个列表,并根据结果返回一个整数值。我得到了以下错误:Couldn't match type 'IO Integer' with 'Int'
在第24行,我盯着看了很久,但就是找不到原因(post结束时出现完全错误)。
如果有人能解释我为什么会出错,我将不胜感激。
这是我的脚本:
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] }
发布于 2015-02-18 12:21:26
getLine
函数位于IO
monad中,因此任何调用getLine
的函数也必须位于IO
monad中。将minOfTwo
的类型签名从Int
更改为IO Int
,这个特定的问题就会消失。
(您还需要将let c = minOfTwo
更改为c <- minOfTwo
。)
可能还有其他错误,但这是导致您的错误消息的原因。
https://stackoverflow.com/questions/28570043
复制相似问题