Browse thread
[Caml-list] Map module
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Alessandro Baretta <alex@b...> |
| Subject: | Re: [Caml-list] Map module |
Brian Hurt wrote:
> On Thu, 5 Jun 2003, Alessandro Baretta wrote:
>
>
> Glancing at the code, it appears to be a height-balanced tree. So
> operations should use only O(log N) stack frames- call it something less
> than 32 stack frames for 80K elements. Which means either there is a bug
> in that code, or your insert routine is not tail-recursive.
>
The insert routine is actually tail recursive, but somewhere
else there is something which is not tail recursive. I have
now tried converting my code to simple read-parse-print tail
recursive functional cycle and still I get a Stack overflow.
Here's the code:
let read_cap s = Scanf.sscanf s "%2s %5[0-9]%[^\r\n]%[\r\n]"
let rec output_table input_ch =
try
let line = input_line input_ch in
let prov, cap = read_cap line (fun p c _ _ -> p, c) in
Printf.printf "%s\t%s\n" cap prov;
output_table input_ch
with
| End_of_file -> print_string ""
| Scanf.Scan_failure (_) -> output_table input_ch
***
Ok, now I'm using a "while true" cycle with exceptions. This
simply cannot overflow.
let output_table2 input_ch =
try
while true do
try
let line = input_line input_ch in
let prov, cap = read_cap line (fun p c _ _ -> p, c)
in Printf.printf "%s\t%s\n" cap prov
with Scanf.Scan_failure (_) -> ()
done
with End_of_file -> ()
And, yes, this does work. So what is wrong with the
recursive version?
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