文字列を 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
ですか? <-
オペレーターについて何が理解できませんか?