List Info

Thread: "ocaml_beginners"::[] reading line (imperatively) from file more elegantly?




"ocaml_beginners"::[] reading line (imperatively) from file more elegantly?
user name
2006-06-12 10:25:18

Oliver Bandel wrote:
> 
> Hello,
> 
> can this:
> 
> let read_lines_from_file fname =
>   let lines = ref [] in
>     let ichan = open_in fname in
>     try
>       while true
>       do
>         lines := (input_line ichan) :: !lines
>       done; !lines
>     with End_of_file -> close_in ichan; List.rev
!lines
> 
> be done more elegantly?
> 
> When doing it recursively/functional it looks better,
> but how to do it imperatively and elegant?
> 
> I do not like the "done; !lines", because
it's cheating the
> type system - the value will not be given as return
value.

You can replace "!lines" with "assert
false". Not only it will not cheat the type system
(that is, no more than the OCaml designers indend it to be
cheated), it will also raise a welcome assertion failure if
ever your loop ends the "normal" way. (In case
you accidentally modify your code to that effect.)

> I want to be honest to my compiler  

I'd say you want your code to be honest to your design, a
good idea indeed...

Dmitry Bely wrote:
> Why not
> 
> let rec load t ch =
>   try load ((input_line ch)::t) ch
>   with End_of_file -> List.rev t
> 

...because it is not tail-recursive (calls within a
"try" section are not tail-calls). So that it
will break if the file contains very many lines.

Frédéric



------------------------ Yahoo! Groups Sponsor
--------------------~--> 
Get to your groups with one click. Know instantly when new
email arrives
http://us.click.yahoo.com/.7bhrC/MGxNAA/yQLSAA/saFolB/TM

------------------------------------------------------------
--------~-> 

Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] reading line (imperatively) from file more elegantly?
user name
2006-06-14 16:31:17
On Mon, Jun 12, 2006 at 10:25:18AM +0000, Frederic van der
Plancke wrote:
> 
> 
> Oliver Bandel wrote:
> > 
> > Hello,
> > 
> > can this:
> > 
> > let read_lines_from_file fname =
> >   let lines = ref [] in
> >     let ichan = open_in fname in
> >     try
> >       while true
> >       do
> >         lines := (input_line ichan) :: !lines
> >       done; !lines
> >     with End_of_file -> close_in ichan;
List.rev !lines
> > 
> > be done more elegantly?
> > 
> > When doing it recursively/functional it looks
better,
> > but how to do it imperatively and elegant?
> > 
> > I do not like the "done; !lines",
because it's cheating the
> > type system - the value will not be given as
return value.
> 
> You can replace "!lines" with "assert
false". Not only it will not cheat the type system
(that is, no more than the OCaml designers indend it to be
cheated), it will also raise a welcome assertion failure if
ever your loop ends the "normal" way. (In case
you accidentally modify your code to that effect.)

Well, I would not rewrite such a function, but this kind of
paranoid
programming is fine (if save code has highest priority).

Normally I don't use assert,
but I will think about this assert-idea, at least for more
complex
code.


Thanks for your hints.

Ciao,
   Oliver

P.S.: And thanks to all otehrs who has answered.

P.P.S.: To one question: "Why not functional",
        I want to answer with a question: "Why not
imperative?!",
        but don't want to make an endless-thread out of
this.
        IMHO the fine in OCaml is, that it offers many
possibilities,
        and one can use the one, one thinks is good.

        Another question (or statement?): why not using
Extlib?
        => Because I want to narrow the necessary
libraries to be
        installed, and Extlib is not part of the
OCaml-distribution,
        and the function to read in the lines of code is
quickly written.

__END__


------------------------ Yahoo! Groups Sponsor
--------------------~--> 
Get to your groups with one click. Know instantly when new
email arrives
http://us.click.yahoo.com/.7bhrC/MGxNAA/yQLSAA/saFolB/TM

------------------------------------------------------------
--------~-> 

Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] reading line (imperatively) from file more elegantly?
user name
2006-06-14 17:08:00
On Wed, Jun 14, 2006 at 06:31:17PM +0200, Oliver Bandel
wrote:
> P.P.S.: To one question: "Why not
functional",
>         I want to answer with a question: "Why
not imperative?!",

I think it's perfectly good to write this code
imperatively.

>         Another question (or statement?): why not using
Extlib?
>         => Because I want to narrow the necessary
libraries to be
>         installed, and Extlib is not part of the
OCaml-distribution,
>         and the function to read in the lines of code
is quickly written.

I would almost consider Extlib to be "standard".
 I don't think any of
our programs can now function without it.  If you have a
decent
packaging/installation system (ie. Debian or GODI) then you
should
have no problem installing these sorts of basic packages.

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


------------------------ Yahoo! Groups Sponsor
--------------------~--> 
You can search right from your browser? It's easy and it's
free.  See how.
http://us.click.yahoo.com/_7bhrC/NGxNAA/yQLSAA/saFolB/TM

------------------------------------------------------------
--------~-> 

Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



"ocaml_beginners"::[] reading line (imperatively) from file more elegantly?
user name
2006-06-14 20:36:18
On Wednesday 14 June 2006 18:08, Richard Jones wrote:
> On Wed, Jun 14, 2006 at 06:31:17PM +0200, Oliver Bandel
wrote:
> > P.P.S.: To one question: "Why not
functional",
> >         I want to answer with a question:
"Why not imperative?!",
>
> I think it's perfectly good to write this code
imperatively.

I'd write it functionally but factor out the HOF of folding
over the input 
lines:

# let rec fold_input_lines f accu ch =
    match try Some (f accu (input_line ch)) with End_of_file
-> None with
    | Some accu -> fold_input_lines f accu' ch
    | None -> accu;; 
val fold_input_lines : ('a -> string -> 'a) ->
'a -> in_channel -> 'a = <fun>

To use Rich's example:

# let () =
    let ch = open_in "/etc/motd" in
    let f n s = Printf.printf "%d: %s\n" n s;
n+1 in
    ignore (fold_input_lines f 1 ch);
    close_in ch;;

I think that's equivalent to Extlib's use of
"enum".

You could also use another HOF to construct an iteri from a
left fold:

# let iteri_of_fold fold f c = ignore (fold (fun n x -> f
n x; n+1) 0 c);;
val iteri_of_fold :
  ((int -> 'a -> int) -> int -> 'b -> 'c)
-> (int -> 'a -> 'd) -> 'b -> unit =
  <fun>

> >         Another question (or statement?): why not
using Extlib?
> >         => Because I want to narrow the
necessary libraries to be
> >         installed, and Extlib is not part of the
OCaml-distribution,
> >         and the function to read in the lines of
code is quickly written.
>
> I would almost consider Extlib to be
"standard".

None of the people that I work with use it. We all use our
own "stdlib2". I'll 
release it ASAP.

> I don't think any of 
> our programs can now function without it.  If you have
a decent
> packaging/installation system (ie. Debian or GODI) then
you should
> have no problem installing these sorts of basic
packages.

True. I am totally intolerant of Obj use. I rewrote
XML-Light to avoid its 
(two or so) uses of Obj and Extlib has 45 calls to the Obj
module. I've also 
heard horror stories of it causing segfaults in long-running
programs and I 
use OCaml because I value reliability above performance.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scient
ists


------------------------ Yahoo! Groups Sponsor
--------------------~--> 
You can search right from your browser? It's easy and it's
free.  See how.
http://us.click.yahoo.com/_7bhrC/NGxNAA/yQLSAA/saFolB/TM

------------------------------------------------------------
--------~-> 

Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 


"ocaml_beginners"::[] reading line (imperatively) from file more elegantly?
user name
2006-06-15 14:34:23
On Wed, Jun 14, 2006 at 09:36:18PM +0100, Jon Harrop wrote:
> True. I am totally intolerant of Obj use. I rewrote
XML-Light to avoid its 
> (two or so) uses of Obj and Extlib has 45 calls to the
Obj module. I've also 
> heard horror stories of it causing segfaults in
long-running programs and I 
> use OCaml because I value reliability above
performance.

Is that Obj or Extlib causing segfaults?  We have lots of
long-running, memory intensive programs using Extlib and
haven't seen
this problem, but that may be because we don't exercise
some
problematic piece of code.

It's also fair to say that the use of Obj in Extlib is
confined to
certain areas, particularly DynArray (where it would be
impossible to
avoid) and Std.dump (which I wrote and which similarly
requires it).
But the really useful parts of Extlib (eg. ExtString,
ExtList, Option, ...)
avoid it completely.

I'd like to see Xavier Leroy turn his expert attention to
DynArray,
but it's unlikely to happen.

Rich.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


------------------------ Yahoo! Groups Sponsor
--------------------~--> 
See what's inside the new Yahoo! Groups email.
http://us.click.yahoo.com/2pRQfA/bOaOAA/yQLSAA/saFolB/TM

------------------------------------------------------------
--------~-> 

Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors'
one) can be found at http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid
flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http:/
/groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.c
om/info/terms/
 



[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )