From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA10241; Wed, 22 Aug 2001 22:41:54 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA10175 for ; Wed, 22 Aug 2001 22:41:54 +0200 (MET DST) Received: from woozle.org (woozle.org [216.39.146.229]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f7MKfq912675 for ; Wed, 22 Aug 2001 22:41:53 +0200 (MET DST) Received: from neale by woozle.org with local auth=neale (Exim 3.12 #1 (Debian)) id 15Zeox-0001k6-00 for ; Wed, 22 Aug 2001 13:41:51 -0700 To: caml-list@pauillac.inria.fr Subject: Re: [Caml-list] Str.string_match raising Invalid_argument "String.sub" in gc References: From: Neale Pickett Organization: WoozleWorks (woozle.org) X-Face: "&g4:h\nuA>dfRaRmJ5c+Mqvu!,|5@dd>:BqJ,#G:mT3`i-EF{L>6oE?di}m\;Wil(?AS(@9j"G@o-UR8(BU$)u.%I;*K9%4Vj.fO$W9-bjxPgl%|#{^W@e#1/jZ@,G:>&;JzJrBqMqomx3Z#Hg.``g5v%R[+PzjYtAa&l@EtK{R<;.,gV`5$8Go1OJB=L`R(<)U$M4YK-t;a}oA1y([AV@r$%?AJW[:_|_*r44[Gl{3@:Ff6U9XFOJxp%lZWI-d0-1l5+6aMAOAT+Ac%q@E3t|:2;lpSwi=1"gf7g{Bz+U2MI X-PGP-Key-Fingerprint: A862 F105 13EF 7FAF 4F08 78B4 9168 856B 48BF F157 Date: 22 Aug 2001 13:41:51 -0700 In-Reply-To: Message-ID: User-Agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.1 (Capitol Reef) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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