[Haskell-beginners] Traversable Instance of the Constant type

Atrudyjane atrudyjane at protonmail.com
Thu Apr 20 05:53:05 UTC 2017


Pretty sure I'm getting bogged down on something simple, but how does the traverse function actually embed the Constant type in the f context passed to it? Or is it the pure defined in the f context that's being called?

Thank you,
Andrea

newtype Constant a b = Constant { getConstant :: a } deriving (Eq, Show)

instance Functor (Constant a) where
fmap f (Constant a) = Constant a

instance Monoid a => Applicative (Constant a) where
pure a = Constant { getConstant = mempty }
Constant a <*> Constant b = Constant { getConstant = a `mappend` b }

instance Foldable (Constant a) where
foldMap f (Constant a) = mempty

instance Traversable (Constant a) where
traverse f (Constant a) = pure $ Constant a

λ> traverse (\x -> Just x) (Constant 1)
Just (Constant {getConstant = 1})

Sent with [ProtonMail](https://protonmail.com) Secure Email.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170420/1fa6e012/attachment.html>


More information about the Beginners mailing list