Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] ocaml and large development projects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Markus Mottl <markus@o...>
Subject: Re: [Caml-list] Reading a file
Siegfried Gonzi schrieb am Mittwoch, den 21. Mai 2003:
> Enclosed the OCaml version in question:
[snip]

There are some horrendously inefficient things in the code snippet
you sent, mostly related to lack of tail-recursion due to bad use of
exception handlers within loops.

Try this as a starting point:

---------------------------------------------------------------------------
let rec my_index_from_loop s len i c =
  if i >= len then -1
  else if String.unsafe_get s i = c then i
  else my_index_from_loop s len (i + 1) c
;;

let my_index_from s start c =
  my_index_from_loop s (String.length s) start c
;;

let split s c =
  let rec loop start acc =
    let next = my_index_from s start c in
    if next = -1 then
      let substring = String.sub s start (String.length s - start) in
      List.rev_append acc [substring]
    else
      let substring = String.sub s start (next - start) in
      loop (next + 1) (substring :: acc) in
  loop 0 []
;;

let frob userval = function
  | "n/a" | "nil" -> userval
  | s -> float_of_string s
;;

let extract_floats file del nan_proxy =
  let my_frob = frob nan_proxy in
  let rec loop acc =
    match
      try
        let line = input_line file in
        Some line
      with End_of_file -> None
    with
    | Some line -> loop (List.map my_frob (split line del) :: acc)
    | None -> List.rev acc in
  loop []
;;

let f = open_in "/home/gonzi/test.txt";;
let erg2 = extract_floats f ',' (-1.0);;
let rows = List.length erg2;;
rows;;
---------------------------------------------------------------------------

Regards,
Markus Mottl

-- 
Markus Mottl          http://www.oefai.at/~markus          markus@oefai.at

-------------------
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