Simple idea for making a function infix
[
Home
]
[ Index:
by date

by threads
]
[ Message by date: previous  next ] [ Message in thread: previous  next ] [ Thread: previous  next ]
[ Message by date: previous  next ] [ Message in thread: previous  next ] [ Thread: previous  next ]
Date:  20061113 (16:19) 
From:  Till Varoquaux <till.varoquaux@g...> 
Subject:  Re: [Camllist] Simple idea for making a function infix 
Hi, I don't really understand the point of the */ operator i your definition. It's fuy ecause I've cosiderig the same problem recetly ad came dow to this approach ( I'm redefining(@)... oe c ) On 11/13/06, Keisuke Nakano <ksk@mist.i.utokyo.ac.jp> wrote: > Hi all, > > Haskell people sometimes complain about that OCaml cannot make an > arbitrary function infix. For example, they can write (3 `min` 4) > to get the result of (min 3 4) in Haskell. Can we satisfy them > without changing OCaml's syntax? > > Here is a simple idea for making a function infix in OCaml. > I hope it will be useful for those who like Haskell's backquote > notation `function_name`. > The idea doesn't require any change of OCaml's syntax. > > We use the following two infix operators. > > let ( /* ) x y = y x > and ( */ ) x y = x y > > Then we can make an infix operator /*f*/ for a binary function f. > For example, using binary functions 'min' and 'max', we can write > > 3 /*min*/ 4 + 6 /*max*/ 8 > > to get 11 as 'min 3 4 + max 5 8'. Note that the infix operator > ( */ ) may conflict with Num.( */ ) if the Num module is loaded > and opened. You can use other definitions in a similar manner, though. > > You have to take care of the precedence. For example, > > 3 /*min*/ 4 * 6 /*max*/ 8 > > will return 18 as 'max ((min 3 4) * 6) 8'. So we should write > > (3 /*min*/ 4) * (6 /*max*/ 8) > > to get 24 as 'min 3 4 * max 6 8'. > > > The original idea was introduced in my blog a few months ago > (written in Japanese, though). At that time, I used other definitions: > > let ( < ) x y = y x > and ( > ) x y = x y > > or > > let ( @^ ) x y = y x > and ( ^@ ) x y z = x z y > > where the definition of ( ^@ ) should be given in a different way > because of the precedence of infix operaters starting with '^' or '@'. > These operators perform a different behavior because of the precedences > of operators. > > 3 <min> 4 + 6 <max> 8 (* = max (min 3 (4 + 6)) 8 => 8 *) > 3 @^min^@ 4 + 6 @^max^@ 8 (* = min 3 (max (4 + 6) 8) => 3 *) > > So you have to write > > (3 <min> 4) + (6 <max> 8) > (3 @^min^@ 4) + (6 @^max^@ 8) > > to get 11 as min 3 4 + max 5 8'. > > > Sincerely, > >  > Keisuke Nakano > Department of Mathematical Informatics, > University of Tokyo > > _______________________________________________ > Camllist mailing list. Subscription management: > http://yquem.inria.fr/cgibin/mailman/listinfo/camllist > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/camlbugs > Hi, I don't really understand the point of (*/) in your solution (maybe for aesthetic reasons?). Actually, I tried something similar very recently (and was talking about it a couple of minutes before reading your mail): ( * might interact badly with camlp4 *) let ($) f a= a f;; let plus a b= a+b;; 4 $plus 4;;