Skip to content

Instantly share code, notes, and snippets.

@arialdomartini
Created June 20, 2024 13:49
Show Gist options
  • Save arialdomartini/6d1d28c45cb562a8b7ca98ea5fd44485 to your computer and use it in GitHub Desktop.
Save arialdomartini/6d1d28c45cb562a8b7ca98ea5fd44485 to your computer and use it in GitHub Desktop.
MonoidEither
import Data.Monoid
-- questo importa Monoid che, semplificato, definisce lo zero e la concatenazione:
class Monoid a where
mempty :: a
mappend :: a -> a -> a
--questo mi permette di definire whatever tipo come monoide
newtype MyMonoid = ...
instance Monoid MyMonoid where
mempty = MyMonoid ...
mappend a b = ...
-- Il normale Either è definito come
-- Da questo, o costruisco il mio MonoidEither
data MonoidEither l r = Left' l | Right' r
-- e combino i due
instance Monoid l => Applicative (MonoidEither l) where
pure = Right'
Left' l1 <*> Left' l2 = Left' (mappend l1 l2) -- nota che qui posso usare mappend!
Left' l <*> _ = Left' l
_ <*> Left' l = Left' l
Right' f <*> Right' r = Right' (f r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment