[Haskell-cafe] avoiding parens in postfix applicative notation

MarLinn monkleyon at gmail.com
Tue Feb 6 14:59:26 UTC 2018


> operators <$> and <*> have their precedence so that this (silly example) works without parens:
>
>    (,) <$> "foo" <*> "bar"
>
> Most of the time, I find that the function (the first argument) is the largest sub-expression, so I want to put it last. I can do this
>
>    import Control.Lens.Lens ((<&>))
>
>    "foo" <**> ( "bar" <&> (,) )
>
> but (even ignoring that this pulls in a library with a ton of dependencies) this needs parens, and I find this quite ugly when the closing parenthesis comes at the end of lambda expression that spans several lines.

Hello,

what's bad about the dead simple solution?

     foobar = makeTuple <$> "foo" <*> "bar"
       where
         makeTuple = (,) -- bonus: name as documentation

But if you insist: (,) <$> "foo" <*> "bar" is the same as (<*> "bar") . 
(<$> "foo") $ (,). But that would flip the order of the arguments. So 
maybe flip them back:

     import Control.Category ( (>>>) )

     foobar = (<$> "foo") >>> (<*> "bar")  $  (,)

Now let's extract new functions:

     a  >>>* b =      a  >>> (<*> b) ; infixl 4  >>>*
     a $>>>* b = (<$> a) >>>*     b  ; infixl 4 $>>>*


     foobar = "foo" $>>>* "bar" >>>* "baz"  $  (,,)

You might want to bike-shed these names a bit, but that sounds like the 
operators you want. Maybe name them (>$) and (>*)?

Side note: sometimes if the function is very short I feel like using 
such extra operators for "infix" applicatives as well:

     comma = "foo" <*< (,) >*> "bar" -- same as(,) <$> "foo" <*> "bar"

But I'm still not sure if that's a good idea. I've been bitten multiple 
times because of my own invented operators. What was (>>?!) again? Or 
(^>>>&)? The more I use Haskell the more I tend to solutions like that 
first dead-simple one.

Cheers,
MarLinn



More information about the Haskell-Cafe mailing list