From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: * X-Spam-Status: No, score=1.4 required=5.0 tests=SPF_NEUTRAL autolearn=disabled version=3.1.3 Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 8FD9FBC68 for ; Mon, 13 Nov 2006 17:19:18 +0100 (CET) Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.191]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id kADGJHTn019187 for ; Mon, 13 Nov 2006 17:19:18 +0100 Received: by nf-out-0910.google.com with SMTP id m19so1948858nfc for ; Mon, 13 Nov 2006 08:19:15 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=kOHM6wPZgtGxHJ3gXX2KZwFo1NtkISO5EWF3mmnToFmAZQaCvIF92mOch7AsDGndKqhbMYyTkwtSeB6fuhyzE+0ox8A2UbhnhLAyWVXe794SZaCL0NkOYmNlJ7Lap0g3T1fI0kosCRpZGyNtBBiB0BQuL5lBIIi5Yg5Yam2RbQc= Received: by 10.82.111.8 with SMTP id j8mr63029buc.1163434755275; Mon, 13 Nov 2006 08:19:15 -0800 (PST) Received: by 10.82.153.16 with HTTP; Mon, 13 Nov 2006 08:19:15 -0800 (PST) Message-ID: <9d3ec8300611130819q7e13d5f4mb84b9fb1a2ac74ae@mail.gmail.com> Date: Mon, 13 Nov 2006 17:19:15 +0100 From: "Till Varoquaux" To: "Keisuke Nakano" Subject: Re: [Caml-list] Simple idea for making a function infix Cc: caml-list@inria.fr In-Reply-To: <1EB857FD-F706-419C-9181-D45DEA88B756@mist.i.u-tokyo.ac.jp> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <1EB857FD-F706-419C-9181-D45DEA88B756@mist.i.u-tokyo.ac.jp> X-j-chkmail-Score: MSGID : 45589B06.000 on discorde : j-chkmail score : X : 0/20 1 X-Miltered: at discorde with ID 45589B06.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; infix:01 redefining:01 u-tokyo:01 haskell:01 ocaml:01 infix:01 haskell:01 ocaml's:01 syntax:01 ocaml:01 haskell's:01 notation:01 ocaml's:01 syntax:01 beginner's:01 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 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 > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > 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;;