Haskell Monoids

class Monoid a where  mempty :: a
mappend :: a -> a -> a
mconcat :: [a] -> a
mconcat = foldr mappend mempty
instance Monoid [a] where
mempty = []
mappend = (++)
instance Monoid Ordering where
mempty = EQ
mappend LT _ = LT
mappend GT _ = GT
mappend EQ x = x
instance Monoid a => Monoid (Maybe a) where
mempty = Nothing
mappend Nothing m = m
mappend m Nothing = m
mappend (Just m1) (Just m2) = Just (mappend m1 m2)
ghci> mappend (Just "mom")  (Just "dad" )
Just "momdad"




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

IoC DIP DI in software, what the

How to Deploy a Windows Server VM in Azure using Terraform

Setting Up a Slack Starter Kit With Native SFDC Integration

The world wide web is like a bow tie: Discovering graph structure with Neo4j


The Best Web Application Development Cost Through The Pricing Guide

Web Application Development Cost

Matrix Operations with Python NumPy-I

You Don’t Have To Be A Genius To Code

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Priyanka Mondal

Priyanka Mondal

More from Medium

Reflections on Organize for Democracy: Marquita Young

Organize for Democracy Leadership Program featured against a picture of the Capitol and the White House and silhouettes of 5 people. Blue Future logo in the lower right corner and Grassroots Dems HQ logo featured in the lower left corner

Solved: Error Wring Proxy Settings on Windows 11 (2022 Update)

I Survived My Traumatic Event of 5/14, But Three Symptoms of Social Injustice Are NEVER Cured!

Give Local Isabella sets goal of raising $100,000 for Isabella County