September 2015
Intermediate to advanced
250 pages
6h 40m
English
Recursion is used quite extensively in a number of algorithms, like quick sort, dynamic programming, stack-based operations…and the list goes on. Recursion is highly expressive and intuitive. Sometimes we also use recursion to avoid mutation. Let’s look at a use of recursion here. We’ll keep the problem simple so we can focus on the issues with recursion instead of dealing with problem or domain complexities.
| ProgrammingRecursions/factorial.scala | |
| Line 1 | def factorial(number: Int) : BigInt = { |
| 2 | if(number == 0) |
| 3 | 1 |
| 4 | else |
| 5 | number * factorial(number - 1) |
| 6 | } |
The factorial function receives a parameter and returns a value of 1 if the parameter is zero. Otherwise, it recursively calls itself to return a product of ...
Read now
Unlock full access