当我在函数的类型签名中明确提到类型时isQuestion
,GHCi 完美地编译它:
isQuestion :: [Char] -> Maybe Bool
isQuestion [] = Nothing
isQuestion xs = Just (last xs == '?')
但是,当我转向“通用”代码时,它不起作用:
isQuestion :: [a] -> Maybe b
isQuestion [] = Nothing
isQuestion xs = Just (last xs == '?')
因为我收到以下错误:
<interactive>:138:17: error:
* Couldn't match type `b' with `Bool'
`b' is a rigid type variable bound by
the type signature for:
isQuestion :: forall a b. [a] -> Maybe b
at <interactive>:136:1-28
Expected type: Maybe b
Actual type: Maybe Bool
* In the expression: Just (last xs == '?')
In an equation for `isQuestion':
isQuestion xs = Just (last xs == '?')
* Relevant bindings include
isQuestion :: [a] -> Maybe b (bound at <interactive>:137:1)