From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA10034; Wed, 2 Oct 2002 17:02:33 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 RAA10188 for ; Wed, 2 Oct 2002 17:02:32 +0200 (MET DST) Received: from beaune.inria.fr (beaune.inria.fr [128.93.8.3]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g92F2V501962 for ; Wed, 2 Oct 2002 17:02:31 +0200 (MET DST) Received: by beaune.inria.fr (8.8.8/1.1.22.3/14Sep99-0328PM) id RAA0000000423; Wed, 2 Oct 2002 17:02:30 +0200 (MET DST) From: Luc Maranget Message-Id: <200210021502.RAA0000000423@beaune.inria.fr> Subject: Re: [Caml-list] Pattern matching and strings To: alex@baretta.com (Alessandro Baretta) Date: Wed, 2 Oct 2002 17:02:30 +0200 (MET DST) Cc: caml-list@inria.fr (Ocaml) In-Reply-To: <3D9AFED2.2050400@baretta.com> from "Alessandro Baretta" at oct 02, 2002 04:12:34 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > > I have to do a little bit of pattern matching on strings. My > first instict was to write something like the following. > > let foo x = ... > let bar x = ... > ... = function > | "foo" ^ rest -> foo rest > | "bar" ^ rest -> bar rest > | _ -> raise Unrecognized > > Of course, this is not possible because (^) is an operator > rather than a constructor. Since I believe that the above > code is much more natural and idiomatic than code based on > regexps, I wonder how much compiler magic it would take to > make it work. Probably all it takes is some syntax-magic > since the above can be mapped onto the following: Hum, I am not found of such an idea. As you tell yoursef, ``(^) is an operator rather than a constructor''. Making a special case for (^) is a bad idea from the start. The special thing about a pattern is that is contains constructors, variables, (and constants) and nothing else. This restriction allows efficient compilation and lots of static checks in some uniform framework. Getting out of the uniform framework is a risk (cf. n+k patterns in Haskell eg). > > open Scanf > > ... = function x -> begin > try sscanf x "foo%[^]" foo with Scan_failure _ -> > try sscanf x "bar%[^]" bar with Scan_failure _ -> > raise Unrecognized > end > you mean ... = function x -> begin try sscanf x "foo%s" foo with Scan_failure _ -> try sscanf x "bar%s" bar with Scan_failure _ -> raise Unrecognized end > An equivalent mapping could be done with the Str library or > any other regexp library. Those libs are optional.... (at present). I agree, having some kind of fusion between ML pattern-matching and regexp-matching is an issue, there is research work on this. But this means lot of work... > > Alex --Luc ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners