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 QAA08307; Wed, 2 Oct 2002 16:02:40 +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 QAA08258 for ; Wed, 2 Oct 2002 16:02:40 +0200 (MET DST) Received: from athlon.baretta.com (host57-68.pool80116.interbusiness.it [80.116.68.57]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g92E2d528852 for ; Wed, 2 Oct 2002 16:02:39 +0200 (MET DST) Received: from baretta.com (localhost.localdomain [127.0.0.1]) by athlon.baretta.com (Postfix) with ESMTP id 66D722739E for ; Wed, 2 Oct 2002 16:12:34 +0200 (CEST) Message-ID: <3D9AFED2.2050400@baretta.com> Date: Wed, 02 Oct 2002 16:12:34 +0200 From: Alessandro Baretta Organization: Baretta srl -- www.baretta.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020826 X-Accept-Language: it, en MIME-Version: 1.0 To: Ocaml Subject: [Caml-list] Pattern matching and strings Content-Type: text/plain; charset=us-ascii; format=flowed 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: open Scanf ... = function x -> begin try sscanf x "foo%[^]" foo with Scan_failure _ -> try sscanf x "bar%[^]" bar with Scan_failure _ -> raise Unrecognized end An equivalent mapping could be done with the Str library or any other regexp library. However, the former is much cleaner. If it could be had, I'd appreciate it. Alex ------------------- 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