Infix expressions
From HaskellWiki
(Difference between revisions)
BrettGiles (Talk  contribs) m (Infix Expressions moved to Infix expressions) 
(Category:Syntax) 

(2 intermediate revisions by 2 users not shown)  
Line 1:  Line 1:  
−  <pre> 
+  ==Mail info== 
−  WORK IN PROGRESS 
+  The original header posted here: 
+  From: dons@cse.unsw.edu.au (Donald Bruce Stewart) 

+  To: Simon PeytonJones <simonpj@microsoft.com> 

+  Date: Wed, 15 Mar 2006 23:25:34 +1100 

+  Cc: haskellprime@haskell.org, oleg@pobox.com 

+  Subject: Re: Infix expressions 

−  From: dons@cse.unsw.edu.au (Donald Bruce Stewart) 
+  This refered to a variety of articles, the original was said to be: 
−  To: Simon PeytonJones <simonpj@microsoft.com> 
+  [http://www.haskell.org/pipermail/haskellcafe/2002July/003215.html haskellcafe message] 
−  Date: Wed, 15 Mar 2006 23:25:34 +1100 

−  Cc: haskellprime@haskell.org, oleg@pobox.com 

−  Subject: Re: Infix expressions 

−  simonpj: 
+  ==The solution== 
−  > I often wish that cool tricks like this could be collected on the 
+  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 web site. Now that it's a wiki, anyone could do that. 
+  <haskell> 
+  xs `zipWith (+)` ys 

+  </haskell> 

−  Yes, this is _exactly_ the kind of thing to add to the Idioms 
+  Chungchieh 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. 
−  page of the wiki, here: 
+  
−  http://www.haskell.org/haskellwiki/Category:Idioms 
+  For ease of reference, here's their elegant solution: 
−  +  <haskell> 

−  So if anyone knows of an interesting Haskell trick, and wants to write 
+  infixr 0 :, : 
−  about it, add a page! 
+  data Infix f y = f : y 
−  +  x :f: y = x `f` y 

−  We should take advantage of the fact we have a lot of good authors in 
+  main = print $ [1,2,3] : zipWith (+) : [4,5,6] 
−  the community to document all the interesting things that we come up 
+  </haskell> 
−  with 
+  
−  +  For completeness, here's the `dual': 

−   Don 
+  <haskell> 
−  +  infixl 5 ! 

−  > 
+  (!) = flip ($) 
−  > Simon 
+  infixl 5 ! 
−  > 
+  (!) = ($) 
−  >  Original Message 
+  
−  >  From: haskellprimebounces@haskell.org 
+  add2 x y = x + y 
−  > [mailto:haskellprimebounces@haskell.org] On Behalf Of 
+  add3 x y z = x + y + z 
−  >  oleg@pobox.com 
+  add4 x y z u = x + y + z + u 
−  >  Sent: 15 March 2006 04:34 
+  sub3 x y z = x + y  z 
−  >  To: doaitse@cs.uu.nl; haskellprime@haskell.org 
+  
−  >  Subject: Infix expressions 
+  testa1 = 1 ! add2 ! 3 + 4 
−  >  
+  testa2 = 1 ! add3 1 ! 3 + 4 
−  >  
+  testa3 = 1  2 ! add4 1 5 ! 3 * 4 
−  >  Doaitse Swierstra wrote: 
+   17 = (12) + (1+5) + (3*4) 
−  >  > In Haskell we write `f` in order to infixify the identifier f. In 
+  testa4 = 1  2 ! sub3 1 ! 3 * 4 
−  > ABC 
+   12 = (12) + (1)  12 
−  >  > the stuff between backquotes is not limited to an identifier, but 
+  </haskell> 
−  > any 

−  >  > expression may occur there. This would allow one to write e.g. 

−  >  > 

−  >  > xs `zipWith (+)` ys 

−  >  

−  >  Chungchieh 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/haskellcafe/2002July/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. 

−  >  _______________________________________________ 

−  : 

−  : 

−  _______________________________________________ 

−  Haskellprime mailing list 

−  Haskellprime@haskell.org 

−  http://haskell.org/mailman/listinfo/haskellprime 

−  </pre> 

−   

[[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 PeytonJones <simonpj@microsoft.com> Date: Wed, 15 Mar 2006 23:25:34 +1100 Cc: haskellprime@haskell.org, oleg@pobox.com Subject: Re: Infix expressions
This refered to a variety of articles, the original was said to be: haskellcafe message
[edit] 2 The solution
In Haskell we write`f`
xs `zipWith (+)` ys
Chungchieh 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 = (12) + (1+5) + (3*4) testa4 = 1  2 ! sub3 1 ! 3 * 4  12 = (12) + (1)  12