# Loops and wrapper functions | Recursion Series

## Метаданные

- **Канал:** WilliamFiset
- **YouTube:** https://www.youtube.com/watch?v=JBFFWnZIPx8
- **Дата:** 02.06.2023
- **Длительность:** 10:28
- **Просмотры:** 8,072
- **Источник:** https://ekstraktznaniy.ru/video/49238

## Описание

This video explores how to loop through a list using recursion.

Source code repository:
https://github.com/williamfiset/algorithms

Video slides:
https://github.com/williamfiset/algorithms/tree/master/slides

## Транскрипт

### Introduction []

foreign welcome back this is William and today we're going to look at how to iterate through lists using recursion looping through elements in a list is a very common operation we do in programming and it's something we want to know how to do recursively as well which is what we're going to be focusing on today

### Example [0:31]

to give a sense of how to iterate through lists using recursion we're going to be looking at a simple problem the problem today involves finding the sum of all the odd numbers in a list for the list below the sum of all the odd numbers would be 3 plus 5 plus 11 plus 7 for a total of 26. if you were to think of how you would solve this problem iteratively you would be keeping track of two things a reference to the list and the index position that you're at the same is true in solving the problem recursively we maintain a reference to both the list we wish to Loop through and the index at all times the way we're going to approach this problem is going to be by looping through the list from left to right we will start at index position 0 and stop once we reach the end of the list which is effectively our base case for the recursion let's assume we have a function called some odds which can calculate the sum of all the odd numbers in the list the function some odds would keep track of two things the index position that we're at and a reference to the list since we want to begin summing all the odd numbers starting at the front of the list we would call the sum odds function with the index position 0 and pass a reference to the list then since we want to Traverse the list from left to right we would recursively call the function incrementing the index position to the next element we keep calling the sum odds function on each element until we reach the end of the list calling the sum odds function for each index Position will allow us to check whether each element is an odd number and add it to the total eventually we would increment the index value to a value that is outside the bounds of the list at which point we want to stop the recursion and pull back to calculate the sum so the function call for some odds with an index of 5 gets a sum of zero since there is no number at index five next we would check if the number at index 4 is odd and if so add it to the total next we would check if the number at index three is odd and if so add it to the total then the same thing for index two then check if the number on index one is odd and add it to the total and finally check if the number at index 0 is odd and if so add it to the total in the end we get that the sum of all the odd numbers in the list is 15 and we can easily check that this is the correct answer by adding 5 plus 7 plus 3 which when all added together is 15.

### Pseudocode [3:23]

here's some pseudocode on how to implement the Sumas function let's have a walk through it the first thing you will notice is that there are two parameters for this function the index position that we're at and a reference to the list of numbers next up we have the base case which checks if the index position is equal to the length of the list this checks to see if we have reached the end of the list at which point we should have visited all the elements already and we can return a value of zero after that we have the body of the function which checks that the current element we're at is an odd number if the number is odd the value variable gets assigned to the value of the element in the list and it is added to the total otherwise the variable retains a value of zero when the element is even I should note that the value variable gets added to the sum in the following line and the return statement at the end and lastly we recursively call the Sumas function incrementing the index position to the next position in the list allowing us to Traverse the list from left to right in this slide I added a new wrapper function called sum odds in list which takes a list as input in a real programming language you would make the sum odds function private or internal only and let the sum odds in this function be the public facing function it's pretty typical in recursive programming to have one public user-facing function that calls another function that does the actual work to specify the initial values for the recursion in this case calling this some odds in lists function allows us to call the sum odds function but starting the recursion at index 0 to ensure that the whole list gets looped over

### Challenge [5:20]

all right so we just finished taking a look at how to take the sum of all the odd numbers in a list now I have a challenge for you guys Implement a function called Product of even numbers that finds the product of all the even numbers in the list I hope this isn't too difficult as you should be able to reuse the some odds function as a good starting point I have outlined several examples Below on what the expected output should be sure to pause this video and give it a try I'm going to reveal the solution in the next slide but I will give you guys a short moment

### Solution [6:02]

all right hopefully you guys give that a try here's a working solution for the product of Even's function first notice that I split the work into two separate functions one is product of even numbers which is the wrapper function or the public facing function and then there's F the internal private recursive function that handles Computing the answer there are several reasons why I think it's often advised to separate your recursive function into two parts first is because it allows you to change the underlying internal function arguments without changing the Declaration of the public function this is useful if your public function is called in multiple locations throughout your code base because there's no overhead to refactoring second is because it allows the public function to specify some of the default arguments to the internal function for instance the function f should always be called with an index starting at zero so it makes sense to hide this detail from whoever wants to calculate the product of all the even numbers in the list all right let's begin taking a look at the product of even numbers function we want to start by calling the internal function f to calculate the product of all the even numbers in the list by supplying a reference to the list and an initial starting index of zero afterwards once the function has calculated the final product we check to see if the answer is null and if it is it means that either the list was empty or all the numbers in the list were odd numbers so we can return 0 in both of these cases otherwise return the product moving on to the function f this is the function that actually calculates the product of all the even numbers by maintaining a running product of all the numbers we've multiplied together so far our base case is when we've reached the end of the list when this happens return null to indicate that there is no product at the moment after that recursively call the function f incrementing the index position one at a time this makes us move to the next element in the list after reaching the end of the list the recursion begins to unwind and the if statements after the function call get executed the first if statement checks if the current number is not even and if so Returns the current product at this point could either be an actual value or null If the previous numbers were also odd essentially this line simply propagates the answer of the stack once we know the current number is an even number we have to handle the case where there is no product yet in this case return the element at the current position otherwise multiply the current element and the running product together I would also like to note that you want to be mindful for integer overflow when you're multiplying several numbers together like this as the result tends to grow very quickly and basically that's how you implement multiplying the product of even numbers in the list recursively so in summary we learned how to Loop through a list recursively the main idea was that we want to maintain an index position as well as a reference to the list in the arguments of the function as we do our recursive calls

### Wrapup [9:40]

we also learned about the concept of a wrapper function to hide the internal recursive function details that don't need to be exposed separating recursive functions this way adds a layer of abstraction provides an easy way to specify default values to Kickstart the recursion and can make handling edge cases easier awesome guys that's all I've got for now thank you so much for watching and sticking to the end please like this video And subscribe for more content and as always I'll catch you in the next one
