Jeremy Lindblom
January 09, 2014
480

# Recursion: Making Big Problems Smaller

Recursion is a key concept in programming, and can be used to simplify code and create elegant solutions. It's also has a reputation of being a little hard to learn. I will show you that it is not as difficult as you think, and that you can take a formulaic approach to writing recursive functions. Doing so will allow you to solve big problems, by dividing them up into smaller, easier ones.

January 09, 2014

## Transcript

2. ### – some sarcastic jerkface “ “ To understand recursion, you

must understand recursion.
3. ### Why Should You Care? 1.  You’re Here! – You’re already

here, so I’m guessing you are interested. 2.  Academics – Recursion is a key concept in computer science and mathematics. 3.  Career – Technical interviews often contain questions where recursive solutions are a good answer.
4. ### Why Should You Care? 4.  Open Source – A survey

of 14 major PHP projects yielded more than 300 examples of recursive functions. 5.  Other Languages – Some languages in the functional programming paradigm rely solely on recursion for loops. 6.  Parallelization – Recursive functions are stateless and can be parallelized easier.
5. ### Takeaways ① Using recursion (when appropriate) can result in shorter, simpler

code. ② Recursion allows you to solve the whole problem, by only solving part of the problem. Recursion: Making Big Problems Smaller
6. ### Hello! I'm @jeremeamia PHP Software Engineer at Co-author of the

AWS SDK for PHP Co-organizer of the Seattle PHP User Group Zend Education Advisory Board for 5.5 exam

9. ### O RLY? K, THX! And yes, I do see what

you did there.

17. ### PHP Hypertext Preprocessor PHP Hypertext Preprocessor Hypertext Preprocessor PHP Hypertext

Preprocessor Hypertext Preprocessor Hypertext Preprocessor

actually)
19. ### Factorial — n!   20!  =  20  ×  19  ×

18  …  ×  2  ×  1   6!  =  720   5!  =  120   4!  =    24   3!  =      6
20. ### Factorial — n!   20!  =  20  ×  19  ×

18  …  ×  2  ×  1

22. ### Factorial — n!   20!  =  20  ×  19!

Hey look! Recursion!

25. ### Recursive Function Anatomy •  Base Case(s) •  Recursive Call(s) •

Problem Size Reduction

30. ### Anything you can do with recursion, you can also do

without recursion. FYI

32. ### Iterative solutions typically are faster and require less memory than

recursive solutions. FYI
33. ### Iteration vs. Recursion Iterative Factorial Benchmark: ~4.25  µs   Result

w/ large input: INF
34. ### Iteration vs. Recursion Iterative Factorial Benchmark: ~4.25  µs   Result

w/ large input: INF   Recursive Factorial Benchmark: ~10.50  µs   Result w/ large input: Seg  fault:  11
35. ### Iteration vs. Recursion Iterative Factorial Benchmark: ~4.25  µs   Result

w/ large input: INF   Recursive Factorial Benchmark: ~10.50  µs   Result w/ large input: Seg  fault:  11   Yep, that's a stack overflow

45. ### Iteration vs. Recursion Iterative Traversal Benchmark: ~54  µs   Recursive

Traversal Benchmark: ~39  µs   w00t!  FTW!
46. ### Making Big Problems Smaller ① Using recursion (when appropriate) can result

in shorter, simpler code. (that is sometimes faster, too) vs. Iterative (20 LOC) Recursive (9 LOC)
47. ### Making Big Problems Smaller ② Recursion allows you to solve the

whole problem, by only solving part of the problem. This logic only prints 1 node!
48. ### Making Big Problems Smaller ② Recursion allows you to solve the

whole problem, by only solving part of the problem. =
49. ### Recursion is… The Adjustable Crescent Wrench. No matter what the

problem (nut/bolt) size is, you perform the same action.

51. ### Alright, I'm impressed now. Recursion is pretty cool, but… Do

you have any practical examples?
52. ### General Examples •  Mathematical and geometric Series •  Tree/graph traversal

– DOM/XML/array traversing/building – Directory structure traversing (-r) – Parsing computer languages – Resolving dependency graphs (DIC) •  “Divide-and-conquer” algorithms

54. ### Recursive Methods in Your Phavorite Projects •  ZF2: 50+ •

Symfony: 75+ •  Laravel 4: 8+ •  Guzzle 3: 8+ •  Wordpress: 100+ •  Drupal 8: 25+ •  Joomla: 50+ •  Monolog: 2 •  Twig: 10+ •  SwiftMailer: 7+ •  PHPParser: 10+ •  AWS PHP SDK: 1+ •  Doctrine: 7+ •  CodeIgniter: 35+

58. ### Takeaways ① Using recursion (when appropriate) can result in shorter, simpler

code. ② Recursion allows you to solve the whole problem, by only solving part of the problem. Recursion: Making Big Problems Smaller