【发布时间】:2015-09-08 11:35:12
【问题描述】:
Maybe 的 Hackage 文档将 Foldable 列为 Maybe 的类型类之一。它还列出了以下函数:
null :: Maybe a -> Bool
它甚至链接到这个函数的实现(来自Foldable):
null :: t a -> Bool
null = foldr (\_ _ -> False) True
...这似乎很合理。它也有效:如果我 import qualified Data.Foldable,我可以在 Maybe 值上使用 foldr。
但是,当我尝试在 Maybe 上调用 null 时,Haskell 认为我想使用为列表设计的 null:
Prelude> :t null
null :: [a] -> Bool
Prelude> null Nothing
<interactive>:3:6:
Couldn't match expected type `[a0]' with actual type `Maybe a1'
In the first argument of `null', namely `Nothing'
In the expression: null Nothing
In an equation for `it': it = null Nothing
我知道有 isJust,我只是想知道如何为任何 Foldable 调用像 null 这样的函数。
【问题讨论】:
-
您使用的是什么 GHC/base 版本?因为是的,base-4.8.x 是
t a -> Bool但nullused to be[a] -> Bool -
@Carsten 是的,就是这样 - Ubuntu Vivid 显然仍在发布 ghc 7.6.3,它于 2013 年 4 月发布并且具有旧版本的 base。
-
是的,抱歉 - 看到你迟到的答案 - 顺便说一句,你可以使用 Herbert V. Riedels PPA-Sources :D
标签: haskell ghc typeclass maybe foldable