[Haskell-beginners] flatten comma operator

Arlen Cuss a at unnali.com
Wed Jun 6 08:42:52 CEST 2012


By the way, is the excerpt from RWH involving liftA2 the chapter on using Parsec? If so, this may be the code snippet you refer to:

-- file: ch16/FormApp.hs a_pair :: CharParser () (String, Maybe String) a_pair = liftA2 (,) (many1 a_char) (optionMaybe (char '=' *> many a_char))

In this case, liftA2 is promoting the (,) operation to work with the two operations in the CharParser applicative functor.

(,) is of type "a -> b -> (a,b)", so without lifting, we'd end up with something like "(CharParser () String, CharParser () Maybe String)" (just a guess here).

liftA2 produces a new applicative functor action which computes each of (many1 a_char) and (optionMaybe (char '=' *> many a_char)), then gives the pure results to (,). 


On Wednesday, 6 June 2012 at 4:36 PM, Arlen Cuss wrote:

> If (,) is a function that takes two elements and returns the 2-tuple, have you considered something like (,,)? :)
> 
> 
> 
> On Wednesday, 6 June 2012 at 4:33 PM, Kees Bleijenberg wrote:
> 
> > In 'Real World Haskell' I found code like LiftA2 (,) ....
> > Looks odd. But after some experimenting in winghci I found that (,) 1 2 is valid code and is equal to (1,2).
> > Now I wonder how to create (1,2,3). I think you need a join or a flatten function or ...? Join doesn't work?
> > 
> > Kees
> > 
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org (mailto:Beginners at haskell.org)
> > http://www.haskell.org/mailman/listinfo/beginners
> 






More information about the Beginners mailing list