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 RAA14817; Tue, 9 Dec 2003 17:58:50 +0100 (MET) 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 RAA09161 for ; Tue, 9 Dec 2003 17:58:49 +0100 (MET) Received: from mailhost.ens-lyon.fr (pluvier.ens-lyon.fr [140.77.167.5]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id hB9Gwm101792 for ; Tue, 9 Dec 2003 17:58:48 +0100 (MET) Received: by mailhost.ens-lyon.fr with scanned-ok (Exim 3.35 #1 (Debian)) id 1ATlCB-0001BL-00 for ; Tue, 09 Dec 2003 17:58:47 +0100 Received: from mailhost.ens-lyon.fr ([127.0.0.1]) by localhost (pluvier [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 03811-09 for ; Tue, 9 Dec 2003 17:58:46 +0100 (CET) Received: from [140.77.128.119] (helo=ens-lyon.org) by mailhost.ens-lyon.fr with esmtp (Exim 3.35 #1 (Debian)) id 1ATlC9-00019v-00 for ; Tue, 09 Dec 2003 17:58:45 +0100 Message-ID: <3FD5FF44.3000003@ens-lyon.org> Date: Tue, 09 Dec 2003 17:58:44 +0100 From: Jean-Baptiste Rouquier User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031024 Debian/1.5-2 X-Accept-Language: en MIME-Version: 1.0 To: caml-list@inria.fr Subject: Re: [Caml-list] stream conversion References: <2111F26F-2A1D-11D8-8637-000393CB0F1E@spy.net> <87brqi8a8i.dlv@wanadoo.fr> In-Reply-To: <87brqi8a8i.dlv@wanadoo.fr> Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new-20030616-p5 (Debian) at ens-lyon.fr X-Loop: caml-list@inria.fr X-Spam: no; 0.00; ens-lyon:01 caml-list:01 char:01 camlp:01 compiles:01 foo:01 quz:99 foo:01 printf:01 printf:01 redefining:01 qux:99 char:01 redefining:01 camlp:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Dustin Sallings wrote: >I have a need to convert a ``string stream'' to a ``char stream.'' > Small is beautiful ! #load "camlp4o.cma";; let rec convert = parser | [< 'string; stream >] -> [] | [<>] -> [<>] (* That's it... However this function compiles but doesn't work.*) (* Can anybody explain the following problem: *) let foo = [< Stream.of_string "bar"; Stream.of_string "quz">] let _ = Stream.next foo (* Exception: Failure "illegal stream concatenation". *) (* So I have to implement my own (awful : skip it!) concatenation function : *) let concat s s' = let stream_next = let rec f = ref (fun _ -> try Some (Stream.next s) with Stream.Failure -> Printf.printf "redefining f\n%!"; f := (fun _ -> (try Some (Stream.next s') with Stream.Failure -> None)); !f 0) in fun x -> !f x in Stream.from stream_next (* And then slightly rewrite my function : *) let rec convert = parser | [< 'string; stream >] -> concat (Stream.of_string string) (convert stream) | [<>] -> [<>] (*let's test it*) let foo = convert (Stream.of_list ["foo"; "bar"; ""; "qux"]) let _ = Stream.next foo;; (* - : char = 'f' *) let _ = Stream.next foo;; (* - : char = 'o' *) let _ = Stream.next foo;; (* - : char = 'o' *) let _ = Stream.next foo;; (* redefining f *) (* - : char = 'b' *) let _ = Stream.next foo;; (* - : char = 'a' *) let _ = Stream.next foo;; (* - : char = 'r' *) let _ = Stream.next foo;; (* redefining f *) (* redefining f *) (* - : char = 'q' *) let _ = Stream.next foo;; (* - : char = 'u' *) let _ = Stream.next foo;; (* - : char = 'x' *) let _ = Stream.next foo;; (* redefining f *) (* Exception: Stream.Failure. *) Except the concatenation problem, I think camlp4 makes it much clearer, and it works with empty strings. Jean-Baptiste Rouquier. ------------------- 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