# Infix expressions

Donald Bruce Stewart dons at cse.unsw.edu.au
Wed Mar 15 07:25:34 EST 2006

```simonpj:
> I often wish that cool tricks like this could be collected on the
> Haskell web site.  Now that it's a wiki, anyone could do that.

Yes, this is _exactly_ the kind of thing to add to the Idioms
page of the wiki, here:

So if anyone knows of an interesting Haskell trick, and wants to write

We should take advantage of the fact we have a lot of good authors in
the community to document all the interesting things that we come up
with :)

-- Don

>
> Simon
>
> | -----Original Message-----
> | oleg at pobox.com
> | Sent: 15 March 2006 04:34
> | Subject: Infix expressions
> |
> |
> | Doaitse Swierstra wrote:
> | > In Haskell we write `f` in order to infixify the identifier f. In
> ABC
> | > the stuff between backquotes is not limited to an identifier, but
> any
> | > expression may occur there. This would allow one to write e.g.
> | >
> | >   xs `zipWith (+)` ys
> |
> | Chung-chieh Shan and Dylan Thurston showed the Haskell98 solution for
> | exactly the same example, in their article `Infix expressions',
> | back in 2002:
> |
> |
> | For ease of reference, here's their elegant solution:
> |
> | > infixr 0 -:, :-
> | > data Infix f y = f :- y
> | > x -:f:- y = x `f` y
> | >
> | > main = print \$ [1,2,3] -: zipWith (+) :- [4,5,6]
> |
> |
> | For completeness, here's the `dual':
> |
> | > infixr 5 -!
> | > (-!) = flip (\$)
> | > infixr 5 !-
> | > (!-) = (\$)
> | >
> | > add2 x y = x + y
> | > add3 x y z = x + y + z
> | > add4 x y z u = x + y + z + u
> | >
> | > testa1 = 1 -! add2 !- 3 + 4
> | > testa2 = 1 -! add3 1 !- 3 + 4
> | > testa3 = 1 - 2 -! add4 1  5 !- 3 * 4
> |
> | All code is Haskell98.
> | _______________________________________________