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: Alain Frisch <frisch@c...>
Subject: Re: [Caml-list] Str.string_match raising Invalid_argument "String.sub" in gc
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.

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

BTW, you could call Str.regexp only once.

-- 
  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