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 QAA25524; Thu, 12 Jun 2003 16:43:15 +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 QAA25438 for ; Thu, 12 Jun 2003 16:43:14 +0200 (MET DST) Received: from beaune.inria.fr (beaune.inria.fr [128.93.8.3]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h5CEhDH03811 for ; Thu, 12 Jun 2003 16:43:13 +0200 (MET DST) Received: by beaune.inria.fr (8.8.8/1.1.22.3/14Sep99-0328PM) id QAA0000013156; Thu, 12 Jun 2003 16:43:12 +0200 (MET DST) From: Luc Maranget Message-Id: <200306121443.QAA0000013156@beaune.inria.fr> Subject: Re: [Caml-list] How to find out if a function is tail recursive? To: steck@stecksoft.com Date: Thu, 12 Jun 2003 16:43:12 +0200 (MET DST) Cc: rich@annexia.org ('Richard Jones'), caml-list@inria.fr In-Reply-To: <000201c330ef$473ea270$0264a8c0@blimpy> from "Paul Steckler" at jun 12, 2003 10:31:14 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; caml-list:01 printf:01 tail-rec:01 --luc:01 rec:01 maranget:02 recursive:03 tail:03 argument:03 cons:03 let:04 luc:05 function:09 isn't:11 operator:12 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > > > let rec range a b = > > if a > b then [] > > else a :: range (a+1) b > > ;; > > > > let list = range 1 1000000;; > > > > Printf.printf "length = %d\n" (List.length list);; > > > > Can you tell me why this function isn't tail recursive, and > > share any useful tips on how to tell whether a function is or > > is not tail recursive? > > The recursive call to range is not the result for its > caller. Instead, the result of recursive call is > an argument to the cons (the :: operator). > > -- Paul Hence, here is a tail-recursive range function. let rec do_range a b r = if a > b then r else do_range a (b-1) (b::r) (* tail-rec call *) ;; let range a b = do_range a b [] ;; --Luc ------------------- 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