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 TAA15380; Sun, 15 Sep 2002 19:19:06 +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 TAA15881 for ; Sun, 15 Sep 2002 19:19:05 +0200 (MET DST) Received: from athlon.baretta.com (host155-68.pool80116.interbusiness.it [80.116.68.155]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id g8FHJ3919334 for ; Sun, 15 Sep 2002 19:19:04 +0200 (MET DST) Received: from baretta.com (localhost.localdomain [127.0.0.1]) by athlon.baretta.com (Postfix) with ESMTP id D61662724C; Sun, 15 Sep 2002 19:28:09 +0200 (CEST) Message-ID: <3D84C329.4030809@baretta.com> Date: Sun, 15 Sep 2002 19:28:09 +0200 From: Alessandro Baretta Organization: Baretta srl -- www.baretta.com User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.1) Gecko/20020826 X-Accept-Language: it, en MIME-Version: 1.0 To: Richard , Ocaml Subject: Re: [Caml-list] Reading from a stream until '\000' - will this approach work? References: <200209131825.g8DIPqd8010033@lithinos.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Richard wrote: > After thinking a bit more about it... > How about - > > let rec myStreamReader inchan = > let char = read_char inchan in > if char = '\000' then > let holderString = holderString ^ char > and break > else > myStreamReader inchan > ;; The approach is somewhat buggy, but it can be fixed. Apart from a few syntactic issues, the main semantic bug is in the declaration of the holderstring value withing a function declaration. The value identified by holderstring is local to each function call, so you would simply be allocating a slew of single-character strings on the heap, which is not what you want. Besides, another crucial bug is that myStreamReader returns a unit-type value, which is meaningless. This is how you can do it: let global_EOL = ref '\000' let myStreamReader = (* Notice this is not a func. decl. *) let holder = Buffer.create 0 in let rec read ?(reset_buffer=false) in_ch = if reset_buffer then Buffer.reset holder else (); let char = input_char in_ch in if char = !global_EOL then Buffer.contents holder else begin Buffer.add_char holder char; read ~reset_buffer:false in_ch end in read ~reset_buffer:true ;; You can drag-n-drop this into your toplevel to test it. At any rate, you should realize that this is a low-level, procedural solution. If you approach O'Caml you should attempt to learn the "Right Way(TM)" of programming: the functional way. Read the documentation of the Scanf module and learn to use that. You'll save yourself a lot of trouble with Buffer management, and I'm ready to be a whole dime that it's going to be a lot more efficient, too. > Again - I hope I'm able to make sense... You should test your code with the toplevel before submitting it to the list, insofar as possible. > Thanks for any feedback on the concept! > -Richard Have a lot of *fun*! Alex ------------------- 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