Wan*_*uta 4 haskell typeclass ghc maybe foldable
Maybe列表的Hackage文档可折叠为Maybe的类型类之一.它还列出了以下功能:
null :: Maybe a -> Bool
Run Code Online (Sandbox Code Playgroud)
它甚至链接到这个函数的实现(来自Foldable):
null :: t a -> Bool
null = foldr (\_ _ -> False) True
Run Code Online (Sandbox Code Playgroud)
......这似乎很合理.它也有效:如果我import qualified Data.Foldable,我可以使用foldrMaybe值.
但是,当我尝试调用nullMaybe时,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
Run Code Online (Sandbox Code Playgroud)
我知道有isJust,我只是想知道如何调用null任何可折叠的功能.
事实证明,我运行的是旧版本的GHC(我的操作系统的默认版本),而文档是针对最新版本的(当然).
至少在GHC 7.10.2中null,您从Prelude中获得的内容支持Foldables(如Maybe),而无需导入任何内容:
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
Prelude> :t null
null :: Foldable t => t a -> Bool
Run Code Online (Sandbox Code Playgroud)