# Lists and Recursion

Recursion -- functions calling themselves
1. We have seen functions that call other functions
2. Functions can also call themselves -- this allows repeated computations to take places, which is the real power of any programming language -- doing the same thing over and over
3. Example: Multiply defined by using repeated addition, then defined as N*M = N + (N-1)*M
4. Note that * appears on both sides -- this is a recursive definition -- its uses itself to define itself
5. The problem is when do you stop? For the multiplication example, we stop when (N-1) is 0
6. So, the recursive definition of multiplication is really
7. N*M is if N=1 then M else N + (N-1)*M

8. Hint: that's how you would write it in ML, too!
9. The stopping point is the base case (here, where N=1), the call to itself is the recursive case
10. The recursive case must always make sure the problem is getting smaller -- i.e., approaching the base case.
Lists -- a new data structure
1. ML has a data type called lists, which is exactly what it sounds like, a list of values
2. Unlike a tuple, a list has to have values of all the same type. You can have a list of real numbers, a list of strings, but not a list of reals and strings mixed together
3. Also unlike a tuple, which is a fixed length, a list can be as long or as short as it needs to be
4. A list is formed by using square brackets with element values separated by commas
1. [ 1, 5, 3, 1, 7, 2, 3 ] is an int list
5. Lists are very powerful. Now we can really start programming! We have ways to store as much data as we have (with lists), and a way to do repeated computation (with recursion)
1. Until now, our functions were basically like calculators
6. Operations on lists -- hd(List) gives you the first element of the list, and tl(List) gives you all the rest of the list except the first element, and null(List) returns true if a list is empty, and false otherwise. The :: or the cons operator lets you construct bigger lists from smaller ones.
7. That's really all you need to process lists, but there's also length(List), which returns an integer that is the length of the list, and List.nth(List,N:int) which returns the Nth element of a list, where the first element is at 0
some function definitions.