caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Alain Frisch <frisch@clipper.ens.fr>
To: Ruchira Datta <datta@math.berkeley.edu>
Cc: caml-list@inria.fr
Subject: Re: Why Not Tail-Recursive?
Date: Thu, 26 Oct 2000 18:55:14 +0200 (MET DST)	[thread overview]
Message-ID: <Pine.GSO.4.04.10010261847440.12833-100000@clipper.ens.fr> (raw)
In-Reply-To: <20001025235738.A7286@blue1.berkeley.edu>

On Wed, 25 Oct 2000, Ruchira Datta wrote:

> The only thing I can think of is that the functions deepen and next_path
> are not actually tail-recursive as I expected them to be.  But why not?  

deepen is not tail recursive: the recursive call is followed by the
the exception handler (which must be desinstalled, even if it doesn't
catch any exception).

For instance:
let f x =
    try f x with Not_found -> ();;

will raise a Stack overflow when executed.

If you want the exception to stop the computation, install the handler
around the toplevel call of deepen.


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


-- 
  Alain Frisch



  parent reply	other threads:[~2000-10-26 18:40 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-10-26  6:57 Ruchira Datta
2000-10-26 15:37 ` bcpierce
2000-10-26 16:30 ` hubert.fauque
2000-10-26 16:55 ` Alain Frisch [this message]
2000-10-26 17:24 ` John Prevost
2000-10-26 21:09 Ruchira Datta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.GSO.4.04.10010261847440.12833-100000@clipper.ens.fr \
    --to=frisch@clipper.ens.fr \
    --cc=caml-list@inria.fr \
    --cc=datta@math.berkeley.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).