Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Pattern matching and strings
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Luc Maranget <luc.maranget@i...>
Subject: Re: [Caml-list] Pattern matching and strings
> 
> 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