2015-09-08 84 views
4

Maybe列表的Hackage文档可以作为Maybe的一个typeclasses折叠。它还列出了以下功能:为什么我不能在Haskell中执行`null(Just 5)`?

null :: Maybe a -> Bool 

它甚至还列出了这一功能的实现(从Foldable):

null :: t a -> Bool 
null = foldr (\_ _ -> False) True 

...这似乎是相当合理的。它也适用:如果我import qualified Data.Foldable,我可以在Maybe值上使用foldr

然而,当我尝试调用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,我只是想知道如何调用功能如null任何折叠。

+5

什么GHC /基础版本您使用的?因为是与基础4.8.x是['TA - > Bool'](https://hackage.haskell.org/package/base-4.8.1.0/docs/Prelude.html#v:null),但['空'曾经是'[A] - > Bool'](https://hackage.haskell.org/package/base-4.7.0.1/docs/Prelude.html#v:null) – Carsten

+0

@Carsten是的,这正是它 - Ubuntu Vivid显然仍在发布[ghc 7.6.3](http://packages.ubuntu.com/vivid/ghc),该版本于2013年4月发布,并具有较旧版本的基础。 –

+1

噢,抱歉 - 看到你的答案下旬 - 顺便说一句,你可以使用[赫伯特·V. Riedels PPA来源(https://launchpad.net/~hvr/+archive/ubuntu/ghc):d – Carsten

回答

8

事实证明,我是跑GHC(默认版本为我的OS)的旧版本,而文件是为最新的版本(当然)。

在GHC 7.10.2至少,你得到了序幕null支持Foldables(如可能),而不必输入任何东西:

GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help 
Prelude> :t null 
null :: Foldable t => t a -> Bool 
+4

这是由于[燃烧桥梁建议](https://wiki.haskell.org/Foldable_Traversable_In_Prelude)。以防万一你想知道为什么'Foldable'获得了很多额外的功能,'Prelude'的类型在7.8到7.10之间变化很大。 – Zeta

3

有所谓null多种功能。你在ghci中获得的是Prelude,它是null :: [a] -> Bool。发生这种情况的原因是隐含地导入了从Prelude导出的所有内容。

要得到正确的一个,您需要import Data.Foldable (Foldable(null)) *,并为了防止出现歧义,您需要输入import Prelude hiding (null)。以这种方式显式重新导入Prelude可防止以其他方式发生的隐式导入。

*或import Data.Foldable (Foldable(..))让所有的Foldable的方法。

+0

感谢 - 但是,没有按似乎没有工作 - 我得到'Module'Data.Foldable'不导出'空' –

+0

@WanderNauta我的错误。固定。 – Dan

+0

事实证明,更新GHC足以解决问题 - 但再次感谢! –

相关问题