Version française
Home     About     Download     Resources     Contact us    
Browse thread
my stupidity and non-tail calls
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: David Brown <caml-list@d...>
Subject: my stupidity and non-tail calls
Norman Ramsey writes:
 > OK, I apologize for bothering everyone.  My `tail calls' are actually
 > sitting inside `try ... with' blocks, so I recognize that these cannot be
 > optimized since they are in the scope of different handlers.  I will
 > have to find a way to restructure my code.  A great pity, since
 > I like it the way it is... suggestions are welcome.

I wonder if this is worthy of the FAQ.  I have done the same thing as
well.  It comes kind of as a consequence of ocaml's philosophy of
using exceptions for things such as not-found, and end of file.  Doing
this cleans up a lot of code, and a few things get a little messier.

I don't know if there is a "clean" way to do it, but what I usually do
is wrap the item around an option.

e.g.
   let maybe_input_char infile =
     try
       Some (input_char infile)
     with End_of_file ->
       None
   
   then in the code...
   
      let next' = maybe_input_char infile in
      match next' with
	Some next ->
	  let () = Queue.add next q in
	  ...
	  compressLoop instr' b q csum infile
      | None ->
	  finishCompressing instr' b q

It isn't quite as clean as the exception code, but it is
tail-recursive.

David Brown