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

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Pierre Weis <weis@p...>
Subject:
>   let rec deepen ( elts_so_far, wgt_so_far, undecided_elts ) =
>     try (
>       match undecided_elts with
>       | [] -> 
>         let new_path = next_path ( elts_so_far, wgt_so_far, undecided_elts ) 
>         in deepen new_path
>       | elt :: elts ->
>         let new_wgt = wgt_so_far +. wgt_fn elt in
>         if new_wgt < desired_wgt then
>           deepen ( ( ( elt, true ) :: elts_so_far ), new_wgt, elts )
>         else if new_wgt = desired_wgt then
>           let _ = print_fn ( ( elt, true ) :: elts_so_far ) in
>           deepen ( ( ( elt, false ) :: elts_so_far ), wgt_so_far, elts )
>         else (* new_wgt > desired_wgt *)
>           let new_path = next_path ( elts_so_far, wgt_so_far, undecided_elts ) 
>           in deepen new_path
>     ) with Done -> ()
>   in deepen ( [], 0., elts_sorted_by_wgt )

Each time deepen is invoked you open up a new exception-catching block 
with try, and deepen is called recursively *within* this block. In 
order to get a tail recursive function you have to call deepen from 
outside of the block. E.g., you could use something along

  let rec deepen ( elts_so_far, wgt_so_far, undecided_elts ) =
    match (try (
             match undecided_elts with
               | [] -> Some (next_path (...))
               | (elt:elts) -> Some (...))
           ) with Done -> None) with
      Some path -> deepen path
      None -> ()

Regards
Wolfgang


--
Wolfgang Lux				  Phone: +49-251-83-38263
Institut fuer Wirtschaftinformatik	    FAX: +49-251-83-38259
Universitaet Muenster		      Email: wlux@uni-muenster.de