
Hello Haskellers, I defined newtype Banana = Banana Something When I work with it, I sometimes use a function mapBanana f (Banana b) = Banana (f b) which is of course discarded by the compiler. Is this usual practice or are there any better solutions? Regards Tim

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)

I'll provide more details. Banana is actually a (shopping) cart:
newtype Cart = Cart (Map.Map Product Amount)
mapCart f (Cart m) = Cart (f m)
I was too lazy to write down the signature of mapCart, since the type
of the cart content might evolve.
Now I have some state:
type Shopping a = State Cart a
and I update it:
buy :: Amount -> Product -> Shopping ()
buy amount product =
modify . mapCart $ Map.insertWith (+) product amount
and the last line is the place where I use mapCart.
This works as expected but I wanted to be sure that I do it "the right way".
2010/12/15 Stephen Tetley
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)
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Wednesday 15 December 2010 23:15:51, Tim Baumgartner wrote:
I'll provide more details. Banana is actually a (shopping) cart:
newtype Cart = Cart (Map.Map Product Amount)
mapCart f (Cart m) = Cart (f m)
I was too lazy to write down the signature of mapCart, since the type of the cart content might evolve.
Now I have some state: type Shopping a = State Cart a
and I update it: buy :: Amount -> Product -> Shopping () buy amount product = modify . mapCart $ Map.insertWith (+) product amount
and the last line is the place where I use mapCart. This works as expected but I wanted to be sure that I do it "the right way".
Yes, it's the right way. You should keep an eye on modify though, it may be too lazy and build large thunks in the state if you do it a lot.

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.

On Wed, Dec 15, 2010 at 10:37:02PM +0100, Tim Baumgartner wrote:
Hello Haskellers,
I defined
newtype Banana = Banana Something
When I work with it, I sometimes use a function
mapBanana f (Banana b) = Banana (f b)
which is of course discarded by the compiler. Is this usual practice or are there any better solutions?
This is the usual practice. Think of it as part of the (small) price we pay to have nice type inference. You may also be interested in reading about Conal Elliott's notion of "semantic editor combinators" [1], of which your 'mapBanana' is an example. -Brent [1] http://conal.net/blog/posts/semantic-editor-combinators/
participants (4)
-
Brent Yorgey
-
Daniel Fischer
-
Stephen Tetley
-
Tim Baumgartner