Why can't I do "null (Just 5)" in Haskell?

The Hackage documentation for Maybe lists can be folded up as one of the possible types. It also contains the following function:

null :: Maybe a -> Bool

He even refers to the implementation of this function (from Foldable):

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

... which seems pretty reasonable. It also works: I can, if I import qualified Data.Foldable, use foldrMaybe for values.

However, when I try to call nullon Maybe, Haskell thinks I want to use null intended 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 what it is isJust, I'm just wondering how to call a type function nullfor any Foldable.

+4
2

, 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
+8

, null. , ghci, Prelude, null :: [a] -> Bool. , Prelude .

, import Data.Foldable (Foldable(null)) *, import Prelude hiding (null). Prelude , .

* import Data.Foldable (Foldable(..)), Foldable.

+3

Source: https://habr.com/ru/post/1606397/


All Articles