Difference between revisions of "Infix expressions"
Jump to navigation
Jump to search
BrettGiles (talk | contribs) m (Infix Expressions moved to Infix expressions) |
BrettGiles (talk | contribs) (Reformatting) |
||
Line 1: | Line 1: | ||
+ | ==Disclaimer== |
||
− | <pre> |
||
+ | This is a '''WORK IN PROGRESS''', transcribed from an email log on haskell-prime@haskell.org. |
||
− | WORK IN PROGRESS |
||
+ | ==Mail info== |
||
− | From: dons@cse.unsw.edu.au (Donald Bruce Stewart) |
||
+ | The original header posted here: |
||
− | To: Simon Peyton-Jones <simonpj@microsoft.com> |
||
+ | From: dons@cse.unsw.edu.au (Donald Bruce Stewart) |
||
− | Date: Wed, 15 Mar 2006 23:25:34 +1100 |
||
+ | To: Simon Peyton-Jones <simonpj@microsoft.com> |
||
− | Cc: haskell-prime@haskell.org, oleg@pobox.com |
||
+ | Date: Wed, 15 Mar 2006 23:25:34 +1100 |
||
− | Subject: Re: Infix expressions |
||
+ | 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: |
||
− | simonpj: |
||
+ | [http://www.haskell.org/pipermail/haskell-cafe/2002-July/003215.html haskell-cafe message] |
||
− | > 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. |
||
+ | ==The solution== |
||
+ | In Haskell we write <hask>`f`</hask> 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. |
||
+ | <haskell> |
||
+ | xs `zipWith (+)` ys |
||
+ | </haskell> |
||
+ | 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. |
||
− | Yes, this is _exactly_ the kind of thing to add to the Idioms |
||
+ | |||
− | page of the wiki, here: |
||
+ | For ease of reference, here's their elegant solution: |
||
− | http://www.haskell.org/haskellwiki/Category:Idioms |
||
+ | <haskell> |
||
− | |||
+ | infixr 0 -:, :- |
||
− | So if anyone knows of an interesting Haskell trick, and wants to write |
||
+ | data Infix f y = f :- y |
||
− | about it, add a page! |
||
+ | x -:f:- y = x `f` y |
||
− | |||
+ | main = print $ [1,2,3] -: zipWith (+) :- [4,5,6] |
||
− | We should take advantage of the fact we have a lot of good authors in |
||
+ | </haskell> |
||
− | the community to document all the interesting things that we come up |
||
+ | |||
− | with |
||
+ | For completeness, here's the `dual': |
||
− | |||
+ | <haskell> |
||
− | -- Don |
||
+ | infixr 5 -! |
||
− | |||
+ | (-!) = flip ($) |
||
− | > |
||
+ | infixr 5 !- |
||
− | > Simon |
||
+ | (!-) = ($) |
||
− | > |
||
+ | |||
− | > | -----Original Message----- |
||
+ | add2 x y = x + y |
||
− | > | From: haskell-prime-bounces@haskell.org |
||
+ | add3 x y z = x + y + z |
||
− | > [mailto:haskell-prime-bounces@haskell.org] On Behalf Of |
||
+ | add4 x y z u = x + y + z + u |
||
− | > | oleg@pobox.com |
||
+ | |||
− | > | Sent: 15 March 2006 04:34 |
||
+ | testa1 = 1 -! add2 !- 3 + 4 |
||
− | > | To: doaitse@cs.uu.nl; haskell-prime@haskell.org |
||
+ | testa2 = 1 -! add3 1 !- 3 + 4 |
||
− | > | Subject: Infix expressions |
||
+ | testa3 = 1 - 2 -! add4 1 5 !- 3 * 4 |
||
− | > | |
||
+ | </haskell> |
||
− | > | |
||
− | > | 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: |
||
− | > | |
||
− | > http://www.haskell.org/pipermail/haskell-cafe/2002-July/003215.html |
||
− | > | |
||
− | > | 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. |
||
− | > | _______________________________________________ |
||
− | : |
||
− | : |
||
− | _______________________________________________ |
||
− | Haskell-prime mailing list |
||
− | Haskell-prime@haskell.org |
||
− | http://haskell.org/mailman/listinfo/haskell-prime |
||
− | </pre> |
||
− | ---- |
||
[[Category:Idioms]] |
[[Category:Idioms]] |
Revision as of 03:01, 21 March 2006
Disclaimer
This is a WORK IN PROGRESS, transcribed from an email log on haskell-prime@haskell.org.
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
The solution
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 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':
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