Monoidal mempty versus pattern

I tried to write a generalized function maximumsimilar to function in Prelude. My first naive approach looked like this: maximum' :: (F.Foldable a, Ord b) => a b -> Maybe b
maximum' mempty = Nothing
maximum' xs = Just $ F.foldl1 max xs

However, when I check it, it always returns Nothingregardless of the input:
> maximum' [1,2,3]
> Nothing

Now I wonder if it is possible to get an empty value of an instance of type Monoid. The validation function that I wrote works correctly:
getMempty :: (Monoid a) => a -> a
getMempty _ = mempty

> getMempty [1,2,3]
> []

I already examined these two questions, but I did not understand how the answers solve my problem:
Write the maximum monoid using Maybe in Haskell
Sample Haskell template in an empty set

How do I rewrite a function maximum'to make it work?

+5
source share
5 answers

. , , .

maximum' mempty = Nothing maximum' x = Nothing. Nothing.

:

maximum' :: (F.Foldable a, Ord b, Eq (a b), Monoid (a b)) => a b -> Maybe b
maximum' xs
  | xs == mempty = Nothing
  | otherwise    = Just $ F.foldl1 max xs

.. xs mempty. , Monoid, mempty :: a b Eq, .

, :

maximum'' :: (F.Foldable a, Ord b) => a b -> Maybe b
maximum'' xs = F.foldl max' Nothing xs
  where max' Nothing x = Just x
        max' (Just y) x = Just $ max x y
+9

( @opqdonut ). "" Maybe foldMap.

newtype Maximum a = Max { unMaximum :: Maybe a }

instance (Ord a) => Monoid (Maximum a) where
  mempty = Max Nothing
  mappend (Max Nothing) b = b
  mappend a (Max Nothing) = a
  mappend (Max (Just a)) (Max (Just b)) = Max . Just $ (max a b)

maximum' = unMaximum . F.foldMap (Max . Just)
+4

: ( ), Monoid. Maybe, , . :

import Data.Monoid (Monoid, mempty, mappend)
import qualified Data.Foldable as F

-- Either we have a maximum value, or Nothing, if the
-- set of values is empty.
newtype Maximum a = Maximum { getMaximum :: Maybe a }
    deriving (Eq, Ord, Read, Show)

instance Ord a => Monoid (Maximum a) where
    mempty                      = Maximum Nothing

    -- If one part is Nothing, just take the other one.
    -- If both have a value, take their maximum.
    (Maximum Nothing) `mappend` y    = y
    x `mappend` (Maximum Nothing)    = x
    (Maximum (Just x)) `mappend` (Maximum (Just y))
                                     = Maximum (Just $ x `max` y)


maximum' :: (F.Foldable t, Ord a) => t a -> Maximum a
maximum' = F.foldMap (Maximum . Just)
+3

, .

, , , Haskell Java: , , , , . , , , , .

- Foldable.foldr:

maximum' :: (F.Foldable a, Ord b) => a b -> Maybe b
maximum' = F.foldr step Nothing
    where step x Nothing = Just x
          step x (Just y) = Just (max x y)

, foldr Foldable: : " Foldable" " mempty. , .

+2
source

What about

maximum' :: (Monoid (t a), F.Foldable t, Ord a, Eq (t a)) => t a -> Maybe a
maximum' xs
   | xs == mempty = Nothing
   | otherwise    = Just $ F.foldl1 max xs

You lacked a guard.

In the function getEmptyyou do not need it. Just use memptyand let its type be inferred.

+1
source

All Articles