3 ms·
> You could invent a total different implementation for the list monad, for the maybe monad, etc (if you respect the laws). Could you show such a different imp
by kqr 11y ago
> You could invent a total different implementation for the list monad, for the maybe monad, etc (if you respect the laws).
Could you show such a different implementation for either list or maybe?
- jjaredsimpson 11y agonewtype HeadList a = HeadList { getHeadList :: [a] } instance Monad HeadList where return a = HeadList [a] m >>= f = HeadList $ fmap (head . getHeadList . f) (getHeadList m) This is a list instance that only keeps the head of function result, so it's basically just a map. But you could imagine putting any function that returns one result. min max avg normalize, etc
- ky3 11y ago-- const [] for a HeadList quux :: a -> HeadList a quux = HeadList . (const []) *Main> HeadList "hello... no wait" >>= quux HeadList {getHeadList = "*** Exception: Prelude.head: empty list Such breakage is verboten. This monad instance does not fly.