Recursion is a programming technique where a function calls itself to solve a problem by breaking it into smaller subproblems, commonly used with lists by processing the head element and recursively applying the function to the tail until reaching an empty list base case; for example, calculating list length involves adding 1 for each head element and recursively counting the tail, while summing a list adds each head value to the recursive sum of the tail.
OCaml Recursion with Lists: Length and Sum Functions
Added:hey guys Mike here in this video we're gonna be going over recursion now I know I said I'd be going over standard library functions then we're gonna switch the order of those videos we're gonna do this one first and then we're going to tackle that so we're gonna go over kirshen right now and I have my code written out right here we're just going to go through it so in the last video we were introduced to pattern matching we have two different cases we have an empty brackets meaning the list is empty and we have a head and a tail meaning that we are dealing with one element and then the rest of the list so in this video we're going to tackle doing the length of a list of function so we want to get the length of a list in this case and it does right here as I should have cleared this in this case we take our list and we want to see the length how many elements are in there so to do that let's think of an intuitive way to go about that problem right like if I have one two three what do I do well I start the front I say okay I see one element one let me count that I see the second element let me count that I see the third element let me count that one two three done the list is three blank three again life means a number of elements in the list so let's let's dive even deeper to that so say I'm a program I see a list let's think of it in terms of the head tail kind of thing I see one well that's kind of like our so in this case H is one and then T would be two and three I see one okay I want to count one I'll put a 1 there 1 element counted I don't want to move on to the rest of the list teeth alright now you're dealing with the list two three ones already counted me to be accounted for that you go to - wow that's an element I'll count that so I'll say another one so we have 1 we have 1 plus 1 is 2 so we have our count at 2 we'll go to the rest of list 3 ok 3 I see another element plus 1 so we have 3 or countess 3 and then go and then the tail would be 0 so we see 0 and what do we do well we're done we've seen empty lists no reason to count plus 0 our total length or count of elements is 1 plus 1 plus 1 which is 3 and that's our length list and that's exactly what we're implementing here so how do we accomplish this and that's through recursion right recursion is when we want to do the same operation again and again but we want to use different like input parameters so like a first we want to do the we want to do the expression 1 2 3 then we want to count how many elements are in 2 3 then we want to count how many elements elements are in 3 then we want to count how many elements are on the empty list so we just work our way down from 1 to 3 and we just count and keep on decrementing our list or cutting it shorter recursion does that it does the same operation with different inputs and the beauty of recursion is that it must so if we have a function here you'll notice that we call the same function right within the function so the recursion builds up our answer so it'll call in a function it will call itself and that's recursion so what we're gonna do is we're gonna add this record keyword for recurrent recursion and we're gonna go down and we're going to calculate our length of the list so we have our function it takes in an integer list it outputs an integer and we're gonna say begin match L with and we talked about beginning and it's good for having a good code block and when our code is very complicated begin and helps us decipher good sections of code so again let's talk about that if we encounter an empty case there's nothing there we're just gonna output 0 but if we have a head in the tail you want to add 1 and then we wanted to a recursive function now why am I doing this we're adding 1 because we see a head element right we see we see the head element that's gonna be 1 and in the first run-through and then we want to call the function again on T which is a remainder of the list so let's go through that so we're gonna start off with 1 2 3 begin match l width well it's not empty and we have a head and a tail in this case the head is going to be 1 and we're gonna have 2 & 3 is like the tail so we're gonna we're gonna add 1 like our cow sure is gonna be one so talents like this is just like me typing to keep track so our counts 1 and our tails gonna be 2 3 so what do we do we call the same function again on 2 3 so our inputs going to be 2 3 our our head tail representation is gonna be two cons 3 our count while we count 2 so now it's two and then our tail is going to be 3 same thing we're passing on 3 well it's gonna be three cons empty lists count 3 and 2 you don't know and then we'll get here T the tail is an empty list not know I don't know I'm saying when we get to the empty list right here we're gonna output 0 so we have one like so 1 plus 1 plus 1 plus 0 is our total output like it changed it together so what this is saying is thing we're gonna do 1 plus the output of this guy and then this guy will say well 1 plus 1 plus the output of that so it recursively builds up an answer so we can even in to prove that this is something to do we have this now output like a length of 7 right it'll go through each element and it'll get it'll get an answer now one thing to keep in mind is that recursion has to break down the problem you can't have something that says this right length of 1 plus length of list L really because n our list remains 1 2 3 4 6 3 2 the rest of our lives it'll never get anything done it'll never work for it and if we do this our programs just not gonna finish right it's just I don't know I think this means that yeah it'll just start barking at us honestly yeah yeah no no no it's going to give you some weird error because again you're not working down the problem you're you just you just keep on recall and calling the same function and it's gonna go nowhere the computers going to run forever and this not gonna like that so that's how we do the length of a list never we'll do the sum of the list and I think we'll call it video and the next video will jump I maybe do some more recursive examples on this maybe in the video after that then won't we'll jump in this standard I actually not gonna make any promises because I'm probably changing but let's do some of list I have no idea what this sums gonna be after we calculate okay yeah we'll figure it out yeah it's gonna take an integer list it's gonna happen an integer so what do you want to do well all you want to say all right begin match Alouette wallet empty list obviously there's nothing in there so we can just add zero we often want to have a recursive part so our tail is gonna you know want to get called so instead of adding one we can do is just add the value of our hat right so had represents an integer right so when their first call is gonna be one cons for us the list well we want to add that one so it's very straightforward simplification and it'll be 21 and indeed six ten sixteen nineteen twenty one so it's gonna go through it and add those values recursively so that's kind of in for recursion very quick introduction and we're gonna continue with lists in the next video see you guys later
Up Next

Pattern Matching with Lists | OCaml Programming Tutorial
@MichaelRyanClarkson
19.6K views•2021-06-21

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science





































![OCaml from the very beginning [1]](https://i.ytimg.com/vi/qtcG-pPg5SQ/maxresdefault.jpg)