我正在尝试实现一个将字符串转换为 Maybe Ints 列表的函数,例如readInts "1 2 42 foo" = [Just 1,Just 2,Just 42,Nothing]
.
我的第一个方法是:
readInts (s::String) = do {
ws <- words s;
return (map (readMaybe::(String -> Maybe Int)) ws)
}
这导致了以下错误:
lab_monad.hs:20:52:
Couldn't match type ‘Char’ with ‘[Char]’
Expected type: [String]
Actual type: String
In the second argument of ‘map’, namely ‘ws’
In the first argument of ‘return’, namely
‘(map (readMaybe :: String -> Maybe Int) ws)’
Failed, modules loaded: none.
我接下来尝试(并工作)的是:
readInts (s::String) = do {
let ws = (words s) in do
return (map (readMaybe::(String -> Maybe Int)) ws)
}
我的问题是,words s
显然是 type [String]
。为什么解释器说它是一个String
?我对<-
运营商不了解什么?