# Infix expressions

### From HaskellWiki

(Difference between revisions)

BrettGiles (Talk | contribs) (Reformatting) |
(Category:Syntax) |
||

(One intermediate revision by one user not shown) | |||

Line 1: | Line 1: | ||

− | ==Disclaimer== |
||

− | This is a '''WORK IN PROGRESS''', transcribed from an email log on haskell-prime@haskell.org. |
||

− | |||

==Mail info== |
==Mail info== |
||

The original header posted here: |
The original header posted here: |
||

Line 31: | Line 28: | ||

For completeness, here's the `dual': |
For completeness, here's the `dual': |
||

<haskell> |
<haskell> |
||

− | infixr 5 -! |
+ | infixl 5 -! |

− | (-!) = flip ($) |
+ | (-!) = flip ($) |

− | infixr 5 !- |
+ | infixl 5 !- |

− | (!-) = ($) |
+ | (!-) = ($) |

− | add2 x y = x + y |
+ | add2 x y = x + y |

− | add3 x y z = x + y + z |
+ | add3 x y z = x + y + z |

− | add4 x y z u = x + y + z + u |
+ | add4 x y z u = x + y + z + u |

+ | sub3 x y z = x + y - z |
||

− | testa1 = 1 -! add2 !- 3 + 4 |
+ | testa1 = 1 -! add2 !- 3 + 4 |

− | testa2 = 1 -! add3 1 !- 3 + 4 |
+ | testa2 = 1 -! add3 1 !- 3 + 4 |

− | testa3 = 1 - 2 -! add4 1 5 !- 3 * 4 |
+ | testa3 = 1 - 2 -! add4 1 5 !- 3 * 4 |

+ | -- 17 = (1-2) + (1+5) + (3*4) |
||

+ | testa4 = 1 - 2 -! sub3 1 !- 3 * 4 |
||

+ | -- -12 = (1-2) + (1) - 12 |
||

</haskell> |
</haskell> |
||

[[Category:Idioms]] |
[[Category:Idioms]] |
||

+ | [[Category:Syntax]] |

## Latest revision as of 09:44, 19 December 2007

## [edit] 1 Mail info

The original header posted here:

From: dons@cse.unsw.edu.au (Donald Bruce Stewart) To: Simon Peyton-Jones <simonpj@microsoft.com> Date: Wed, 15 Mar 2006 23:25:34 +1100 Cc: haskell-prime@haskell.org, oleg@pobox.com Subject: Re: Infix expressions

This refered to a variety of articles, the original was said to be: haskell-cafe message

## [edit] 2 The solution

In Haskell we write`f`

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 in the article referenced above.

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':

infixl 5 -! (-!) = flip ($) infixl 5 !- (!-) = ($) add2 x y = x + y add3 x y z = x + y + z add4 x y z u = x + y + z + u sub3 x y z = x + y - z testa1 = 1 -! add2 !- 3 + 4 testa2 = 1 -! add3 1 !- 3 + 4 testa3 = 1 - 2 -! add4 1 5 !- 3 * 4 -- 17 = (1-2) + (1+5) + (3*4) testa4 = 1 - 2 -! sub3 1 !- 3 * 4 -- -12 = (1-2) + (1) - 12