Test if all elements of a Foldable are the same : <p>I built a function that verifies that all elements of a foldable structure are equal.</p>

<p>Compared to a similar function on the lists, it seems to me that the more general function is disproportionately complex, but I have not been able to simplify it.</p>

<p>Do you have any suggestions?</p>

<pre><code>import Data.Monoid
import Data.Sequence as SQ
import Data.Matrix as MT

allElementsEqualL :: Eq a =&gt; [a] -&gt; Bool
allElementsEqualL [] = True
allElementsEqualL (x:ns) = all (== x) ns
-- allElementsEqualL [1,1,1] -&gt; True

allElementsEqualF :: (Foldable t, Eq a) =&gt; t a -&gt; Bool
allElementsEqualF xs = case (getFirst . foldMap (First . Just) $ xs) of
                        Nothing -&gt; True
                        Just x  -&gt; all (== x) xs

-- allElementsEqualF [1,1,1] -&gt; True

-- allElementsEqualF $ SQ.fromList [1,1,1] -&gt; True

-- allElementsEqualF $ MT.fromLists [[1,1],[1,1]] -&gt; True
</code></pre>
