[Haskell-beginners] mapping over newtypes

Daniel Fischer daniel.is.fischer at googlemail.com
Wed Dec 15 23:21:04 CET 2010


On Wednesday 15 December 2010 22:59:28, Stephen Tetley wrote:
> Doesn't mapBanana work if you give it this signature:
>
> mapBanana :: (Something -> Something) -> Banana -> Banana
> mapBanana f (Banana b) = Banana (f b)
>
>
> Possibly you are wanting a polymorphic Banana?
>
> newtype Banana a = Banana a
>
> mapBanana :: (a -> b) -> Banana a -> Banana b
> mapBanana f (Banana x) = Banana (f x)
>

For polymorphic bananas,

instance Functor Banana where
   fmap f (Banana x) = Banana (f x)

making it an Applicative and/or Monad might be useful too.



More information about the Beginners mailing list