From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA05875; Thu, 5 Jun 2003 20:15:45 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA05935 for ; Thu, 5 Jun 2003 20:15:42 +0200 (MET DST) Received: from smtp2.mathworks.com (smtp.mathworks.com [144.212.95.12]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h55IFfH25639 for ; Thu, 5 Jun 2003 20:15:41 +0200 (MET DST) Received: from mail-vif.mathworks.com (fred-ce0.mathworks.com [144.212.95.18]) by smtp2.mathworks.com (8.11.7/8.11.7) with ESMTP id h55IFdp03628; Thu, 5 Jun 2003 14:15:39 -0400 (EDT) Received: from smithf (smithf.dhcp.mathworks.com [144.212.115.83]) by mail-vif.mathworks.com (8.11.7/8.11.7) with SMTP id h55IFda08853; Thu, 5 Jun 2003 14:15:39 -0400 (EDT) From: "Fred Smith" To: "Alessandro Baretta" , "Brian Hurt" , "Ocaml" Subject: RE: [Caml-list] Map module Date: Thu, 5 Jun 2003 14:15:39 -0400 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0) X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Importance: Normal In-Reply-To: <3EDF8598.6070804@baretta.com> X-Spam: no; 0.00; fsmith:01 mathworks:01 caml-list:01 cap:99 scanf:01 sscanf:01 prov:99 printf:01 -fred:01 handler:01 rec:01 exception:02 stack:02 module:03 pointer:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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