6 Apr 2010 23:36
Re: Re: Test-first an infinite loop
Rick Mugridge <rick <at> rimuresearch.com>
2010-04-06 21:36:10 GMT
2010-04-06 21:36:10 GMT
On 7/04/2010 4:51 a.m., Curtis Cooley wrote:
>
> On Mon, Apr 5, 2010 at 2:48 PM, arnaud.baillly <abailly <at> oqube.com
> <mailto:abailly%40oqube.com>> wrote:
>
> >
> >
> > Hello,
> > When I do TDD in a functional language (eg. Haskell, scala), and even in
> > Java, it is very natural to use recursion instead of iteration. So an
> > imperative statement like:
> >
> > while(true) foo();
> >
> > is written (scala-syntax here):
> >
> > def foo = {
> > // do something useful
> > foo
> > }
> >
> > I don't know Haskell, but in Java or Ruby you are not able to
> implement an
> infinite loop using recursion. You will get a stack overflow very quickly.
> I'm assuming the Haskel compiler converts tail recursion syntax into a
> loop,
> allowing the developer to think in recursion, but the hardware to
> simply use
> a loop. Or it uses fancy execution stack manipulation to avoid the stack
> overflow?
>
That's right. In Haskell and Scala the last call in a tail-recursive
function is compiled into a jump - and hence it acts as a loop. The same
technique can be used with continuations, where the next piece of
computation is passed as an argument to the function.
Cheers, Rick
>
>
> Just curious.
> --
> Curtis Cooley
> curtis.cooley <at> gmail.com <mailto:curtis.cooley%40gmail.com>
> home:http://curtiscooley.com <http://curtiscooley.com>
> blog:http://ponderingobjectorienteddesign.blogspot.com
> <http://ponderingobjectorienteddesign.blogspot.com>
> ===============
> Leadership is a potent combination of strategy and character. But if you
> must be without one, be without the strategy.
> -- H. Norman Schwarzkopf
>
> [Non-text portions of this message have been removed]
>
>
[Non-text portions of this message have been removed]
RSS Feed