lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hallo,

On 3/15/07, David Haley <dchaley@gmail.com> wrote:

I can see the case (although I'm not completely convinced) for Java not
being able to teach pointers, because a lot of the pointerness is hidden
from you. But, I am baffled as to why he thinks that Java has no
recursion in it and that one cannot teach recursion in Java. Time and
time again I've written recursive code in Java. You can even do all
kinds of interesting recursion in OOP using objects that call each other
in a hierarchy (or call the same method but on related objects; it's
still recursion). Can anybody explain why he claims that Java has no
recursion?


    Of course you can do recursion in Java, as you can in C, but it's
useless. Let's see two factorial functions:

int fact(int n)
{
  if(n == 1) {
     return 1;
  } else {
     return n * fact(n-1);
  }
}

(define (fact n)
  (define (fact-iter n acc)
     (if (= n 1)
         acc
         (fact-iter (- n 1) (* n acc))))
  (fact-iter n 1))

    Now try both with n = 1000000, and see which one blows the stack. :-)

--
-alex
http://www.ventonegro.org/