我正在尝试编写一个Haskell函数,它返回列表中的第一项。
当我使用空列表调用它时:
我收到了如下错误消息:
h [] = Nothing
h (x:xs) = x
当我使用空列表调用它时:
main = print (h [])
我收到了如下错误消息:
prog.hs:4:8:
No instance for (Show a0) arising from a use of `print'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Show Double -- Defined in `GHC.Float'
instance Show Float -- Defined in `GHC.Float'
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus 23 others
In the expression: print (h [])
In an equation for `main': main = print (h [])
当我给函数一个空列表时,希望结果为无。
[Maybe a] -> Maybe a
,很可能是因为您在非空情况下忘记了Just
。 - JB.