caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alessandro Baretta <alex@baretta.com>
To: Richard <rich@lithinos.com>, Ocaml <caml-list@inria.fr>
Subject: Re: [Caml-list] Reading from a stream until '\000' - will this approach work?
Date: Sun, 15 Sep 2002 19:28:09 +0200	[thread overview]
Message-ID: <3D84C329.4030809@baretta.com> (raw)
In-Reply-To: <200209131825.g8DIPqd8010033@lithinos.com>



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


      reply	other threads:[~2002-09-15 17:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-13 18:25 Richard
2002-09-15 17:28 ` Alessandro Baretta [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3D84C329.4030809@baretta.com \
    --to=alex@baretta.com \
    --cc=caml-list@inria.fr \
    --cc=rich@lithinos.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).