Version française
Home     About     Download     Resources     Contact us    
Browse thread
Snd question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alain Frisch <Alain.Frisch@i...>
Subject: Re: [Caml-list] Snd question
Richard Jones wrote:
> On Tue, Aug 16, 2005 at 05:34:38PM +0100, Jon Harrop wrote:
> 
>>Currently, you cannot match [|1; ...|] in OCaml.
> 
> 
> Yes!  Or, "prefix" ^ str.

This seems like a good place to insert a shameless plug. Thank you. In 
OCamlDuce (the extension of OCaml with XML types and patterns), you can 
indeed match on string prefixes:

# type t = {{ "OCaml" | "OCamlDuce" | "Other" }};;
type t = {{t}}
# fun (x : t) -> match x with {{ [ 'O' x::PCDATA ] }} -> x;;
- : {{t}} -> {{[ 'ther' | 'Caml' | 'CamlDuce' ]}} = <fun>

You can actually match on more complex regular expressions. E.g., to 
extract all the uppercase ASCII characters:

# fun (x : t) -> match x with {{ [ (x::'A'--'Z' | _)* ] }} -> x;;
- : {{t}} -> {{[ 'OC' | 'OCD' | 'O' ]}} = <fun>

or to split a string:

# let f (x : {{String}}) : {{String}} * {{String}} =
   match x with {{ [ x::PCDATA '.' y::PCDATA ] }} -> (x,y)
              | {{_}} -> raise Not_found;;
val f : {{String}} -> {{String}} * {{String}} = <fun>



-- Alain