From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA05290 for caml-red; Thu, 22 Jun 2000 19:15:44 +0200 (MET DST) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id HAA24976 for ; Thu, 22 Jun 2000 07:20:42 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.10.0/8.10.0) with ESMTP id e5M5KdP04599 for ; Thu, 22 Jun 2000 07:20:40 +0200 (MET DST) Received: from localhost (sansho.kurims.kyoto-u.ac.jp [130.54.16.90]) by kurims.kurims.kyoto-u.ac.jp (8.9.3/3.7W) with ESMTP id OAA14817; Thu, 22 Jun 2000 14:20:11 +0900 (JST) To: prevost@maya.com Cc: caml-list@inria.fr Subject: Re: Labels and operators In-Reply-To: Your message of "19 Jun 2000 00:14:02 -0400" <87bt0y71f9.fsf@isil.localdomain> References: <87bt0y71f9.fsf@isil.localdomain> X-Mailer: Mew version 1.93 on Emacs 20.5 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20000622141957S.garrigue@kurims.kyoto-u.ac.jp> Date: Thu, 22 Jun 2000 14:19:57 +0900 From: Jacques Garrigue X-Dispatcher: imput version 980905(IM100) Sender: weis@pauillac.inria.fr From: John Prevost > A friend of mine recently said "if ML had regexp stuff that was as > convenient as Perl, I'd switch to it for everything", and mentioned =~ > as something he specifically wanted. So, as I was walking home > tonight, I thought "hey, I bet I could make some little operators for > the PCRE library and show him!" > > But, it also occurred to me that you want to use the nice labelled > optional argument stuff, and I wasn't sure you could do that with > operators. Here's what I've discovered. > The only solution I can think of is something like: > > # let re ?iflags ?flags ?rex ?pos pat = (iflags, flags, rex, Some pat, pos) > val re : > ?iflags:'a -> ?flags:'b -> ?rex:'c -> ?pos:'d -> 'e -> > 'a option * 'b option * 'c option * 'e option * 'd option = > > # let (=~) s (iflags,flags,rex,pat,pos) = > pmatch ?iflags ?flags ?rex ?pat ?pos;; > val ( =~ ) : > string -> > Pcre.irflag option * Pcre.rflag list option * Pcre.regexp option * > string option * int option -> bool = > # "foo" =~ re "f";; > - : bool = true > # "foo" =~ re "f" ~pos:1;; > - : bool = false > > Which, well, works, but seems kind of nasty. In fact you have a more general solution than that. What you are trying to do here is just reverse application: # let (>>) x f = f x;; val ( >> ) : 'a -> ('a -> 'b) -> 'b = # let sub ?(pos=0) ?len s = let len = match len with None -> String.length s - pos | Some x -> x in String.sub s ~pos ~len;; val sub : ?pos:int -> ?len:int -> string -> string = # "hello" >> sub ~pos:3;; - : string = "lo" # "hello" >> sub ~len:3;; - : string = "hel" And, with Pcre (artificial since I don't have it installed), # "foo" >>pmatch "f";; - : bool = true # "foo" >>pmatch "f" ~pos:1;; - : bool = false That's not exactly the Perl operator, but it feels a lot like it. > Well, okay, I think it might be reasonable to change the syntax to > allow this syntax: > > > > since the labelled args could not in any way shape or form be thought > to go with either expr1 or expr2. This would lead to things like: > > # "foo" =~ ~pos:1 "f";; > - : bool = false > > being possible. Don't know whether it's a great idea, though. This is technically possible, but this might be confusing: itself may be a complex expression without parenthesis. # "foo" =~ ~pos:1 String.sub "foo" 0 1 ^ "bar";; You would need a bit of habit to parse that. Jacques