The Hackage documentation for Maybe lists Foldable as one of Maybe's typeclasses. It also lists the following function:
null :: Maybe a -> Bool
It even links to this function's implementation (from Foldable
):
null :: t a -> Bool
null = foldr (\_ _ -> False) True
...which seems rather reasonable. It works, too: I can, if I import qualified Data.Foldable
, use foldr
on Maybe values.
However, when I try to call null
on a Maybe, Haskell thinks I want to use the null designed for lists:
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
I know there is isJust
, I'm just wondering how to call a function like null
for any Foldable.
As it turns out, I was running an older version of GHC (the default version for my OS), while the documentation was for the newest version (of course).
In GHC 7.10.2 at least, the null
that you get out of Prelude supports Foldables (like Maybe) without having to import anything:
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
Prelude> :t null
null :: Foldable t => t a -> Bool
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With