Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Str.string_match raising Invalid_argument "String.sub" in gc
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Neale Pickett <neale-caml@w...>
Subject: Re: [Caml-list] Str.string_match raising Invalid_argument "String.sub" in gc
Alain Frisch writes:
> On 22 Aug 2001 neale-caml@woozle.org wrote:

>> # let rec f l =
>> let sep = Str.regexp "^[ \t\n]*\\(.+\\)" in
>> match l with
>> | [] -> []
>> | [""] -> []
>> | s :: rest -> if (Str.string_match sep s 0) then
>> let foo = print_string ("match " ^ Str.matched_group 1 s ^ "\n") in
>>             (Str.matched_group 1 s) :: (f rest)
>                                     ^^
                                    
> This is wrong; with the current OCaml implementation, the right
> operand of (::) is called first; so (Str.matched_group 1 s) is called
> after subsequent calls to Str.string_match, which is obviously
> incorrect.

Aha!  Thank you.

This makes sense, but it is certainly not obvious, especially in a
language which purports to have no side-effects.  I can't help thinking
that s should be a different string for every invocation, but clearly it
is somehow related to the initial input string.  No doubt this is a
clever optimization within OCaml which makes for drastically reduced
memory usage when processing strings, but it does make things a bit
confusing to the beginner.

I don't have any good suggestions on how else to do it, although my base
desire is to have a regexp matching function which returns a string list
of the matched groups.

> You have to use explicit sequencing (let .. in ..)

> BTW, you could call Str.regexp only once.

Yes, to avoild compiling the regexp with each function invocation,
correct?  For this example, I thought it more illustrative to have it
all contained within a function definition, but perhaps I need to become
more experienced with closures.

Merci encore!

Neale

> -- 
>   Alain Frisch


-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr