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 LAA19636; Wed, 21 May 2003 11:11:23 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA19629 for ; Wed, 21 May 2003 11:11:22 +0200 (MET DST) Received: from fichte.ai.univie.ac.at (fichte.ai.univie.ac.at [131.130.174.156]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h4L9BLH23093 for ; Wed, 21 May 2003 11:11:21 +0200 (MET DST) Received: from fichte.ai.univie.ac.at (markus@localhost.ai.univie.ac.at [127.0.0.1]) by fichte.ai.univie.ac.at (8.12.3/8.12.3/Debian-6.3) with ESMTP id h4L9BKwf010073; Wed, 21 May 2003 11:11:20 +0200 Received: (from markus@localhost) by fichte.ai.univie.ac.at (8.12.3/8.12.3/Debian-6.3) id h4L9BKWJ010072; Wed, 21 May 2003 11:11:20 +0200 Date: Wed, 21 May 2003 11:11:20 +0200 From: Markus Mottl To: Siegfried Gonzi Cc: caml-list@inria.fr Subject: Re: [Caml-list] Reading a file Message-ID: <20030521091120.GA9455@fichte.ai.univie.ac.at> Mail-Followup-To: Siegfried Gonzi , caml-list@inria.fr References: <4.3.2.7.2.20030517225010.04b748a0@localhost> <4.3.2.7.2.20030519120753.04545700@localhost> <200305201007.17990.wolfgang.mueller2@uni-bayreuth.de> <3EC9EA84.3070404@stud.uni-graz.at> <20030520132032.GA9564@roke.freak> <3ECB189C.5090400@stud.uni-graz.at> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3ECB189C.5090400@stud.uni-graz.at> User-Agent: Mutt/1.4.1i X-Spam: no; 0.00; caml-list:01 siegfried:01 gonzi:01 mittwoch:01 21.:99 floats:01 erg:01 inefficient:01 unsafe:01 ocaml:01 schrieb:01 rec:01 match:02 mottl:02 float:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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