comp.lang.ada
 help / color / mirror / Atom feed
From: "John B. Matthews" <nospam@nospam.invalid>
Subject: Re: Idiom for tail recursion?
Date: Sun, 31 May 2009 21:01:33 -0400
Date: 2009-05-31T21:01:33-04:00	[thread overview]
Message-ID: <nospam-5F8AAB.21013231052009@mara100-84.onlink.net> (raw)
In-Reply-To: 814eff38-3e01-4f70-aeb7-48a1ae651dfc@h23g2000vbc.googlegroups.com

In article 
<814eff38-3e01-4f70-aeb7-48a1ae651dfc@h23g2000vbc.googlegroups.com>,
 jpwoodruff <jpwoodruff@gmail.com> wrote:

> To entertain myself, I'm making a program that implements the DEVS 
> specification for discrete event simulation.  The book I'm studying 
> describes a reference implementation in Scheme.
> 
> The program proceeds by passing a series of messages among objects 
> that are instances of only a couple class hierarchies.  The messages 
> cycle among the objects in (fairly convoluted) cycles of mutually 
> recursive dispatching calls.  Simulation time advances as the program 
> traverses the outermost cycles of activation.
> 
> In scheme, the functions are tail-recursive - the last call of each 
> of the respective mutually recursive procedures does the recurse.  
> This paradigm is crystal clear in that language and the 
> implementation doesn't grow the stack.

IIUC, Scheme compilers are required to optimize tail recursion using 
iteration [1], when the recursion occurs in a tail context [2].

> But in Ada it appears that I'm going to have a stack of calls that 
> won't return.  I guess the program will quit running when I raise an 
> exception for that purpose.  Or maybe it won't halt, who knows :-}.
> 
> I most definitely do not see a transform to an iterative algorithm.

If the functions are indeed tail-recursive, I'd think the 
conversion to iteration would be straightforward [3, 4].

> So the question: what idiom comes fairly close to the flavor of the 
> tail-recursion and won't blow stack?

[1]<http://en.wikipedia.org/wiki/Tail_recursion>
[2]<http://www.biglist.com/lists/dssslist/archives/199907/msg00389.html>
[3]<http://www.refactoring.com/catalog/replaceRecursionWithIteration.html>
[4]<http://rosettacode.org/wiki/Factorial_function#Ada>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>



  reply	other threads:[~2009-06-01  1:01 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-31 21:04 Idiom for tail recursion? jpwoodruff
2009-06-01  1:01 ` John B. Matthews [this message]
2009-06-01  1:32 ` Gene
2009-06-02 14:00   ` John B. Matthews
2009-06-02 13:38 ` Jean-Pierre Rosen
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox