Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: mapping over newtypes (Stephen Tetley)
2. Re: mapping over newtypes (Tim Baumgartner)
3. Re: mapping over newtypes (Brent Yorgey)
4. Re: mapping over newtypes (Daniel Fischer)
5. Re: mapping over newtypes (Daniel Fischer)
----------------------------------------------------------------------
Message: 1
Date: Wed, 15 Dec 2010 21:59:28 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] mapping over newtypes
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
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)
------------------------------
Message: 2
Date: Wed, 15 Dec 2010 23:15:51 +0100
From: Tim Baumgartner <[email protected]>
Subject: Re: [Haskell-beginners] mapping over newtypes
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
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 <[email protected]>:
> 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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 3
Date: Wed, 15 Dec 2010 17:21:17 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] mapping over newtypes
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
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/
------------------------------
Message: 4
Date: Wed, 15 Dec 2010 23:21:04 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] mapping over newtypes
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
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.
------------------------------
Message: 5
Date: Wed, 15 Dec 2010 23:24:36 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] mapping over newtypes
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
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.
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 30, Issue 33
*****************************************