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 RAA24154; Sun, 31 Aug 2003 17:53:34 +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 RAA25218 for ; Sun, 31 Aug 2003 17:53:32 +0200 (MET DST) Received: from mz1.forethought.net (mzpi3.forethought.net [216.241.36.12]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h7VFrVf19099 for ; Sun, 31 Aug 2003 17:53:32 +0200 (MET DST) Received: from [216.241.35.41] (helo=swordfish) by mz1.forethought.net with esmtp (Exim 4.14) id 19tUWA-00075S-Fr for caml-list@pauillac.inria.fr; Sun, 31 Aug 2003 09:53:30 -0600 Received: from matt by swordfish with local (Exim 3.35 #1 (Debian)) id 19tUVu-0001NE-00 for ; Sun, 31 Aug 2003 09:53:14 -0600 Date: Sun, 31 Aug 2003 09:53:09 -0600 From: Matt Gushee To: caml-list@pauillac.inria.fr Subject: Re: [Caml-list] beginner question about tail recursion Message-ID: <20030831155309.GB3405@swordfish> Reply-To: Matt Gushee Mail-Followup-To: caml-list@pauillac.inria.fr References: <20030831063334.GA3405@swordfish> <87znhqw5pv.dlv@wanadoo.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87znhqw5pv.dlv@wanadoo.fr> User-Agent: Mutt/1.3.27i X-Loop: caml-list@inria.fr X-Spam: no; 0.00; gushee:01 gushee:01 caml-list:01 recursion:01 unmodified:01 englewood:01 manure:01 mgushee:01 havenrock:01 havenrock:01 ignores:01 --lao:01 merel:01 rec:01 rec:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sun, Aug 31, 2003 at 11:12:44AM +0200, Remi Vanicat wrote: > Matt Gushee writes: > > [...] > > > A tail-recursive way to write the function would be: > > > > let rec readlines chnl lines = > > let line, eof = > > try > > let ln = input_line chnl in > > ln, false > > with End_of_file -> > > [], true in > > if eof then lines (* result returned unmodified *) > > else readlines chnl (line :: lines) > > (* 'line :: lines' is evaluated before > > applying 'readlines' *) > > well, this code is tail-recursive, but it is not correct (it won't > compile). This one is better : Oops. You're right. Although I use this technique myself, apparently I have never done exactly what I wrote above. Sorry. > let rec readlines chnl lines = > let maybe_line = > try > let ln = input_line chnl in > Some ln Oh, I like that. Thanks. -- Matt Gushee When a nation follows the Way, Englewood, Colorado, USA Horses bear manure through mgushee@havenrock.com its fields; http://www.havenrock.com/ When a nation ignores the Way, Horses bear soldiers through its streets. --Lao Tzu (Peter Merel, trans.) ------------------- 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