Haskell大写字符映射 - 令人困惑的错误

时间:2010-01-07 20:01:30

标签: haskell

正如你们中的一些人所知,Haskell中的类型推断错误有时会很神秘。我试图编写一个将字母字符映射到大写字母的函数,我想出了这个:

toUpper :: Char -> Char
toUpper char = maybe " " (\a -> a) isValue
    where charMap = zip ['a' .. 'z'] ['A' .. 'Z']
          isValue = lookup char charMap

但它抱怨以下内容:

    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

错误对我来说没有意义,因为我是新手,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:8)

" " :: [Char]
' ' :: Char

您需要maybe ' ' id isValue,而不是maybe " " id isValue