
Hi folks, since I always get great answers here, I have another question. I'm currently parsing a (positive) integer using 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'. Thanks in advance Tim

Tim,
On Sun, Nov 21, 2010 at 7:27 AM, Tim Baumgartner
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

Where is the Parser monad coming from? If its Parsec, a "seasoned Haskeller" would avoid read (which is re-parsing already parsed data) and use the integer parser from Parsec's Token parser module.[*] [*] The one caveat is that the number parsers in this module follow Haskell's lexical rules - you might not always want this.

Hi Stephen,
yes, it's Parsec, but I write this parser only for learning the
language, so I don't care if I rewrite existing code. But thanks for
the advice regarding performance. Patrick's answer was perfect for me
and I will get used to
<$> = fmap
soon.
Tim
2010/11/21 Stephen Tetley
Where is the Parser monad coming from?
If its Parsec, a "seasoned Haskeller" would avoid read (which is re-parsing already parsed data) and use the integer parser from Parsec's Token parser module.[*]
[*] The one caveat is that the number parsers in this module follow Haskell's lexical rules - you might not always want this. _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

I've found that the token library imposes conventions on whitespace
that make it difficult to use in many cases.
On Sun, Nov 21, 2010 at 11:19 PM, Tim Baumgartner
Hi Stephen,
yes, it's Parsec, but I write this parser only for learning the language, so I don't care if I rewrite existing code. But thanks for the advice regarding performance. Patrick's answer was perfect for me and I will get used to
<$> = fmap
soon.
Tim
2010/11/21 Stephen Tetley
: Where is the Parser monad coming from?
If its Parsec, a "seasoned Haskeller" would avoid read (which is re-parsing already parsed data) and use the integer parser from Parsec's Token parser module.[*]
[*] The one caveat is that the number parsers in this module follow Haskell's lexical rules - you might not always want this. _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 22 November 2010 02:59, Lyndon Maydwell
I've found that the token library imposes conventions on whitespace that make it difficult to use in many cases.
True - as it can't be made to differentiate between newline and other whitespace it does make writing line oriented parsers difficult. I tend to write my own cut down version of the token modules at that point.
participants (4)
-
Lyndon Maydwell
-
Patrick LeBoutillier
-
Stephen Tetley
-
Tim Baumgartner