Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Map module
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Fred Smith <fsmith@m...>
Subject: RE: [Caml-list] Map module

The question is: Why isn't this function tail-recursive?
>
> 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
>
>

I believe the answer is the try ... with.  The pointer to the exception
handler is part of each recursive call and cannot be popped off the stack
until its recursive child returns.  Hence the function is not
tail-recursive.  Putting the recursive definition inside the try ... with
should solve your problem.

Good luck.

-Fred

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