caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Pattern matching and strings
@ 2002-10-02 14:12 Alessandro Baretta
  2002-10-02 15:02 ` Luc Maranget
                   ` (2 more replies)
  0 siblings, 3 replies; 32+ messages in thread
From: Alessandro Baretta @ 2002-10-02 14:12 UTC (permalink / raw)
  To: Ocaml

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


^ permalink raw reply	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2002-10-07  7:17 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-02 14:12 [Caml-list] Pattern matching and strings Alessandro Baretta
2002-10-02 15:02 ` Luc Maranget
2002-10-02 16:21   ` [Caml-list] Pattern matching and strings (and a mini-bug in Scanf) Alessandro Baretta
2002-10-02 17:15     ` Luc Maranget
2002-10-02 18:30       ` Alessandro Baretta
2002-10-02 18:32         ` Benjamin C. Pierce
2002-10-04 13:01       ` Florian Douetteau
2002-10-04  9:07     ` Pierre Weis
2002-10-04  9:31       ` Alessandro Baretta
2002-10-04 10:03         ` Pierre Weis
2002-10-04 10:23           ` Alessandro Baretta
2002-10-04 12:11             ` Remi VANICAT
2002-10-04 12:40               ` [Caml-list] Ocaml 3.06 Win2K OCAML/C link problem Ward Wheeler
2002-10-04 13:12               ` [Caml-list] Pattern matching and strings (and a mini-bug inScanf) Frederic van der Plancke
2002-10-04 19:31               ` [Caml-list] Pattern matching and strings (and a mini-bug in Scanf) Alessandro Baretta
2002-10-07  7:17                 ` Pierre Weis
2002-10-02 20:59 ` [Caml-list] views for strings (was: Pattern matching and strings) Chris Hecker
2002-10-02 23:09   ` [Caml-list] " Alessandro Baretta
2002-10-03  8:31 ` [Caml-list] Pattern matching and strings Sven Luther
2002-10-04 12:00   ` Andreas Rossberg
2002-10-04 14:21     ` Kontra, Gergely
2002-10-04 15:14     ` Luc Maranget
2002-10-04 19:38       ` Alessandro Baretta
2002-10-05  6:34         ` [Caml-list] Camlp4 (Was: Pattern matching and strings) Daniel de Rauglaudre
2002-10-05 12:47           ` Sven LUTHER
2002-10-05 12:42             ` Daniel de Rauglaudre
2002-10-05 13:41               ` Michel Mauny
2002-10-05 13:47                 ` Daniel de Rauglaudre
2002-10-05 14:09                   ` Michel Mauny
2002-10-05 18:13                     ` Alessandro Baretta
2002-10-05 20:30                       ` [Caml-list] Future of Camlp4 Daniel de Rauglaudre
2002-10-04 19:13     ` [Caml-list] Pattern matching and strings Sven LUTHER

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).