The notion of "contains a value" is not written to the Monad class. In the end, Monad gives you something for the sequence ( >>= ) of actions (linking the previous result). And MonadPlus gives you a way to “shorten” the calculation using mzero (look at the laws and mplus ).
However, the ability to contain one or more values usually goes hand in hand with the ability to dump all of these values into something. Indeed, Data.Foldable contains a function, also called null :
> import qualified Data.Foldable as F > > showAndTell :: (Show (ta), Foldable t) => ta -> IO () > showAndTell k = > putStrLn $ " F.null (" ++ show k ++ ") is " ++ show (F.null k) > main = do > putStrLn "Using F.null on 'empty' things:" > showAndTell $ (Left "Error" :: Either String Int) > showAndTell $ (Nothing :: Maybe Integer) > showAndTell $ ([] :: [Double]) > > putStrLn "" > putStrLn "Using F.null on 'filled' things:" > showAndTell $ (Right 123 :: Either String Integer) > showAndTell $ (Just 123 :: Maybe Integer) > showAndTell $ ([1,2,3] :: [Int])
Result:
Using F.null on 'empty' things: F.null (Left "Error") is True F.null (Nothing) is True F.null ([]) is True Using F.null on 'filled' things: F.null (Right 123) is False F.null (Just 123) is False F.null ([1,2,3]) is False
So you are looking for the Foldable part, not the Monad . This will only work on GHC 7.10 or higher, since Data.Foldable.null is entered into the 4.8.0.0 database. If you are stuck in an old version of GHC, you can use
> isEmpty :: F.Foldable t => ta -> Bool > isEmpty = F.foldr (\_ _ -> False) True
Zeta
source share