正如你们中的一些人可能知道的那样,Haskell中的类型推断错误有时会很神秘.我试图编写一个将字母字符映射到大写字母的函数,我想出了这个:
toUpper :: Char -> Char
toUpper char = maybe " " (\a -> a) isValue
where charMap = zip ['a' .. 'z'] ['A' .. 'Z']
isValue = lookup char charMap
Run Code Online (Sandbox Code Playgroud)
但它抱怨以下内容:
wordsearch.hs:2:35:
Couldn't match expected type `[Char]' against inferred type `Char'
Expected type: Maybe [Char]
Inferred type: Maybe Char
In the third argument of `maybe', namely `isValue'
In the expression: maybe " " (\ a -> a) isValue
这个错误对我来说没有意义,因为我是新手,有人可以帮忙吗?
" " :: [Char]
' ' :: Char
Run Code Online (Sandbox Code Playgroud)
你想要的maybe ' ' id isValue不是maybe " " id isValue.