* (no subject)
@ 2000-10-27 6:34 Pierre Weis
0 siblings, 0 replies; only message in thread
From: Pierre Weis @ 2000-10-27 6:34 UTC (permalink / raw)
> 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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2000-10-27 6:34 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-10-27 6:34 Pierre Weis
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox