Does Java lack recursion?

Submitted by metaperl on Mon, 01/23/2006 - 8:34pm.

In the same article Joel Spolsky seems to imply that it is impossible to do recursion in Java.

I find that hard to believe...

Submitted by Cale Gibbard on Tue, 01/24/2006 - 2:23pm.

It's not that Java lacks recursion, it's just that it lacks higher order functions, which are necessary to give a full treatment of recursion. If you don't learn about at least (the equivalent of) foldr/foldl, it's hard to say that you've really studied recursion. Also, Java gives you lots of imperative ways to avoid learning about recursion, and as far as I know, doesn't perform tail recursion optimisation, which means that those imperative ways are actually artificially favourable. If you're always using 'for' or 'while' and never writing things recursively or abstracting that recursion into an appropriate higher-order function, you're missing out.

Lots of people come to university already knowing how to write basic loops and program in some imperative language. If you don't force them to think a little differently and write things recursively, they largely won't. The benefits of writing things that way aren't obvious until you've done it for a while.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.