[Haskell-beginners] Parsing an integer
Patrick LeBoutillier
patrick.leboutillier at gmail.com
Sun Nov 21 09:09:04 EST 2010
Tim,
On Sun, Nov 21, 2010 at 7:27 AM, Tim Baumgartner
<baumgartner.tim at googlemail.com> wrote:
>
> data Token = Number Int -- ...
> number :: Parser Token
> number = many1 digit ↠ (read >>> Number >>> return)
> -- = many1 digit ↠ return∘Number∘read
>
> But I wonder what an advanced haskeller would code instead.
> Particularly, I'd like to get rid of the 'return'.
My understanding is that to remove the return, you can lift your pure
function (Number . read) into the Parser monad using fmap or <$> from
Control.Applicative:
number' = Number . read <$> many1 digit
A bit of Sunday morning exploration yielded that you may also like the
<$$> operator, but it is not defined in the standard libraries (see
discussion here:
http://www.haskell.org/pipermail/libraries/2010-April/013403.html) :
(<$$>) :: Functor f => f a -> (a -> b) -> f b
(<$$>) = flip (<$>)
infixl 4 <$$>
number' = many1 digit <$$> Number . read
or
number' = many1 digit <$$> (read >>> Number)
Patrick
>
> Thanks in advance
> Tim
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
More information about the Beginners
mailing list