Possible Duplicate:Resources for improving your comprehension of recursion? I am a programmer with 2 years experience, and sometimes think I could solve a specific problem with recursion, but in most cases I fail miserably.I am asking your advice about resources for learning, refreshing and deepening one's knowledge about recursion, preferably:helping to identify when you could use recursion.practical examples (applied to contemporary programming languages).a reference handbook.source code.
Good resources for learning about recursion
junior programmer;recursion
Spotting when to use recursion is actually quite easy - the key concept is noticing that part of the problem is a smaller version of the whole problem. Examples:Functions on lists can be written using recursion if you see that you can achieve the result by doing something with the first element of the list and then calling the function recursively on the rest of the list. Here the smaller version of the whole problem is the rest of the list excluding the first element. Functions operating on binary trees can often use recursion by recursively calling the function on the left half of the tree and on the right half of the tree and combining the results in some way. Here the smaller version of the whole problem are the left and right sub-trees.Mathematical functions defined recursively (e.g. a function to generate the fibonacci numbers F(n) = F(n-1) + F(n-2) ) often have a simple recursive implementation that mirrors the mathematical definition. Here the smaller version of the whole problem are the two previous numbers in the fibonacci sequence.If you really want to learn about recursion, then picking up a functional language is a good idea - recursion is a natural programming style in functional languages and you'll be steered away from falling back on imperative solutions. I'd suggest a Lisp-1 (e.g. Scheme or Clojure) in particular: some of the recursive functions defined on lists are things of beauty.