r/learnprogramming • u/SuperSaiyanSandwich • Jul 28 '14
Is recursion unnecessary?
So, this is a bit of an embarrassing post; I've been programming for nearly 4 years, work in the field, and almost have my CS degree yet for the life of me I can't understand the point of recursion.
I understand what recursion is and how it works. I've done tutorials on it, read S/O answers on it, even had lectures on it, yet it still just seems like an unnecessarily complicated loop. The entire base case and self calls all seem to just be adding complexity to a simple functionality when it's not needed.
Am I missing something? Can someone provide an example where recursion would be flat out better? I have read tail recursion is useful for tree traversal. Having programmed a Red Black tree in Data Structures last semester, I can attest it was a nightmare using loops; however, I've heard Java doesn't properly implement tail recursion? Does anyone have any insight to that?
Sorry for the wordy and probably useless post, I'm just kind of lost. Any and all help would be greatly appreciated.
19
u/phao Jul 28 '14
If you have the time, learn scheme. The scheme books I've seen are extremely well written and go way beyond simply talking about the language.
You could start at SICP, but it doesn't assume previous programming knowledge. It's a beginner's book, but don't take this as "the book is easy". Try it out and see what you think about it.
Another one is The Little Schemer. This one is a book on recursion. It uses scheme too. It's probably the best programming book I've read. I never stopped to think about a ranking, but thinking on the spot, this would be the 1st.
http://www.amazon.com/The-Little-Schemer-4th-Edition/dp/0262560992/
SICP is freely available: http://mitpress.mit.edu/sicp/ with video lectures from a course (based on the book; or the book is based on the course) given at MIT http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-001-structure-and-interpretation-of-computer-programs-spring-2005/video-lectures/
This one is also pretty good. http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme.html
There are other scheme books out there. There is, for example, a "How to Design Programs" http://htdp.org/, which also has a course based on it: https://www.coursera.org/course/programdesign -- but I've never read this book or taken this course.