DEV Community

Discussion on: Why YOU should learn Recursion

Collapse
 
johncip profile image
jmc • Edited

One good reason for recursion is that some data structures are defined recursively (lists, trees) and so operations on them can be described elegantly using recursion.

FWIW, I think reading The Little Schemer helped me the most for getting the mindset (though I still struggle with it sometimes -- the easy situations become easy over time, but I'm not sure recursive thinking ever becomes truly natural). It's a fun book; I love the quick question-and-answer format.

I won't link it here, but it's very easy to find a PDF online ;)

I think it's fair to say it has a divide and conquer approach

Be advised that divide and conquer has a specific meaning w/r/t algorithms. But I think what you're getting at is that recursion is a good way of dealing with self-similarity.

E.g. a list is either empty or it's an element followed by a list, so recursive list processing "collapses" down to those two cases.

Collapse
 
softchris profile image
Chris Noring

I actually have some tree examples in here, github.com/softchris/recursion . I thought of adding those too, to the article but it would have been a lot longer.. I thought to write a follow up article on this including them :)

Collapse
 
softchris profile image
Chris Noring

Appreciate the comment. I've updated the text. Thanks for the book tip as well :)