Docsity

Functions as Data: Using Functions as Arguments and Recursion Introduction, Study notes of Programming Abstractions

Toggle sidebar
bg1
ProgrammingAbstractions-Lecture07
Instructor (Julie Zelenski):That it felt it was too long, I asked too much of you, and it
decided the best thing I could do for you was just to throw a page out of the list. So you
will notice the handout that we gave out on Wednesday, it goes straight from Page 4 to
Page 7 I think or something like that. It doesn’t really quite make sense when you read it
because it’s in the middle of talking about one problem, and then suddenly it’s talking
about something else. We photocopied the missing page and it’s in the lobby on the way
out. You can also just get it from the web. The PDF that’s up there’s complete. You can
print the middle page out by yourself or just grab the whole PDF to take a look at, but
you do wanna have that handy when you start working on the assignment because it will
be quite mysterious if you try to read it as is with that missing page.
What are we going on to talk about? I’m gonna talk about functions as data a little bit and
client callbacks, and hopefully a little bit of introduction to recursion. We won’t get very
far in recursion today, just enough to get some terminology and see a couple simple
examples, but recursion is gonna be sort of the entirety of next week, so we’ll have lots of
time to soak this in and become masters of it. And that reading that matches with that is
basically Chapter 4, 5, and 6 in order, which is all the recursion chapters in the text.
As you can probably tell, I’m still working on getting rid of my strep. Technically, I’m
not contagious. Apparently, once you’ve been on antibiotics for 24 hours, you’re no
longer spreading the germs, but I’d just as soon not want you to hang out with me today
more than you need to. I’m not sure it’s good for you or for me for that matter, but I will
be actually in my office for a while after class, so if you were hoping to come see me on
Wednesday, and you couldn’t because I canceled my hours or you just have something
on your mind, feel free to walk back with me and talk a little bit today. Anything
administratively? Way in the back?
Student:[Inaudible] that handout [inaudible]?
Instructor (Julie Zelenski):Oh, it will. He’s asking about solutions to Section 2, and
we’re – I bet you Jason could have it up before the end of the – he could put it up right
now. How about that? We try to hold them back until actually after people have had
section, just because it helps to kind of increase the mystery, keep the suspense, but then
sometimes we forget to kind of patch together. So if we ever do that, send us an e-mail.
Jason’s actually your section go-to guy actually, so if you actually have questions about
the section materials, he is the most on top of that, more so than I am. Anything else?
Okay, so thank you for coming out in the rain. I appreciate you trudging over here and
risking your health and life. So I’m gonna do a little diversion here to explain this idea of
functions as data before I come back to fixing the problem we had kind of hit upon at the
very end of the last lecture about set. So what I’m just gonna show you is a little bit about
the design of something in C++ that is gonna helpful in solving the problem we’d run
into. What I’m gonna show you here is two pieces of code that plot a single value
function across the number line. So in this case, the function is that it’s the top one is
applying the sine function, the sine wave as it varies across, and then the square root
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Functions as Data: Using Functions as Arguments and Recursion Introduction and more Study notes Programming Abstractions in PDF only on Docsity!

ProgrammingAbstractions-Lecture

Instructor (Julie Zelenski) :That it felt it was too long, I asked too much of you, and it decided the best thing I could do for you was just to throw a page out of the list. So you will notice the handout that we gave out on Wednesday, it goes straight from Page 4 to Page 7 I think or something like that. It doesn’t really quite make sense when you read it because it’s in the middle of talking about one problem, and then suddenly it’s talking about something else. We photocopied the missing page and it’s in the lobby on the way out. You can also just get it from the web. The PDF that’s up there’s complete. You can print the middle page out by yourself or just grab the whole PDF to take a look at, but you do wanna have that handy when you start working on the assignment because it will be quite mysterious if you try to read it as is with that missing page.

What are we going on to talk about? I’m gonna talk about functions as data a little bit and client callbacks, and hopefully a little bit of introduction to recursion. We won’t get very far in recursion today, just enough to get some terminology and see a couple simple examples, but recursion is gonna be sort of the entirety of next week, so we’ll have lots of time to soak this in and become masters of it. And that reading that matches with that is basically Chapter 4, 5, and 6 in order, which is all the recursion chapters in the text.

As you can probably tell, I’m still working on getting rid of my strep. Technically, I’m not contagious. Apparently, once you’ve been on antibiotics for 24 hours, you’re no longer spreading the germs, but I’d just as soon not want you to hang out with me today more than you need to. I’m not sure it’s good for you or for me for that matter, but I will be actually in my office for a while after class, so if you were hoping to come see me on Wednesday, and you couldn’t because I canceled my hours or you just have something on your mind, feel free to walk back with me and talk a little bit today. Anything administratively? Way in the back?

Student: [Inaudible] that handout [inaudible]?

Instructor (Julie Zelenski) :Oh, it will. He’s asking about solutions to Section 2, and we’re – I bet you Jason could have it up before the end of the – he could put it up right now. How about that? We try to hold them back until actually after people have had section, just because it helps to kind of increase the mystery, keep the suspense, but then sometimes we forget to kind of patch together. So if we ever do that, send us an e-mail. Jason’s actually your section go-to guy actually, so if you actually have questions about the section materials, he is the most on top of that, more so than I am. Anything else? Okay, so thank you for coming out in the rain. I appreciate you trudging over here and risking your health and life. So I’m gonna do a little diversion here to explain this idea of functions as data before I come back to fixing the problem we had kind of hit upon at the very end of the last lecture about set. So what I’m just gonna show you is a little bit about the design of something in C++ that is gonna helpful in solving the problem we’d run into. What I’m gonna show you here is two pieces of code that plot a single value function across the number line. So in this case, the function is that it’s the top one is applying the sine function, the sine wave as it varies across, and then the square root

function which goes up and has a sloping curve to it, and that both of these have exactly the same behaviors.

They’re designed to kind of go into the graphics window, and they’re just using a kind of a very simple kind of hand moving strategy. It starts on a particular location based on what the value of sine is here, and then over a particular interval at 0.1 of one tenth of an inch, it plots the next point and connects a line between them. So it does a simple kind of line approximation of what that function looks like over the interval you asked it to plot. The same code is being used here for plotting the square root. And the thing to note and I tried to highlight by putting it in blue here was that every other part of the code is exactly the same except for those two calls where I need to know what’s the value of square root starting at this particular X. So starting at Value 1, what is sine of one? What is square root of one? As I work my way across the interval, what’s a square root of 1.1, 1.2, 1. and sine of those same values? And so everything about the code is functionally identical. It’s kind of frustrating to look at it, though, and realize if I wanted to plot a bunch of other things – I also wanna be able to plot the cosine function, or some other function of my own creation – across this interval that I keep having to copy and paste this code and duplicate it.

And so one of the things that hopefully your 106A and prior experiences really heightened your attention to is if I have the same piece of code in multiple places, I ought to be able to unify it. I ought to be able to make there be a plot function that can handle both sine and square root without actually having to distinguish it by copy and pasting, so I wanna unify these two. And so there is – the mechanism that we’re gonna use kinda follows naturally if you don’t let yourself get too tripped up by what it means. Just imagine that the parameter for example going into the function right now are the start and the stop, the interval from X is one to five. What we’d like to do is further parameterize the function. We’d like to add a third argument to it, which is to say and when you’re ready to know what function you’re plotting, here’s the one to use. I’d like you to plot sine over the interval start to stop. I’d like you to plot square root over that. So we added the third argument, which was the function we wanted to invoke there. Then we would be able to unify this down to where we had one generic plot function.

The good news is that this does actually have support features in the C++ language that let us do this. The syntax for it is a little bit unusual, and if you think about it too much, it can actually make your head hurt a little bit, think about what it’s doing. But you can actually use a function, a function’s name and the code that is associated with that name, as a piece of data that not just – you think of the code as we’re calling this function, and we’re moving these things around, and executing things. The things that we tend to be executing and operating on you think of as being integers, and strings, and characters, and files. But you can also extend your notion of what’s data to include the code you wrote as part of the possibilities. So in this case, I’ve added that third argument that I wanted to plot, and this syntax here that’s a little bit unusual to you, and I’ll kind of identify it, is that the name of the parameter is actually FN. Its name is enclosed in parentheses there. And then to the right would be a list of the arguments or the prototype information about this function, and on the left is its return value. So what this says is you

Student: [Inaudible] Java?

Instructor (Julie Zelenski) :So Java doesn’t really have a similar mechanism that looks like this. C does, so C++ inherits it from C. There are other ways you try to accomplish this in Java. It tries to support the same functionality in the end, but it uses a pretty different approach than a functions as data approach. [Inaudible]?

Student: Can you pass like a method, like an operator?

Instructor (Julie Zelenski) :So typically not. This syntax that’s being used here is for a free function, a function that’s kind of out in the global namespace, that level. There is a different syntax for passing a method, which is a little bit more messy, and we won’t tend to need it, so I won’t go there with you, but it does exist. It just as it stands does not want a method. It wants a function. So a method meaning member function of a class. Okay, so let me – that was kind of just to set the groundwork for the problem we were trying to solve in set, which was set is holding this collection of elements that the client has stuffed in there. It’s a generic templative class, so it doesn’t have any preconceived notion about what’s being stored. Are the strings? Are they student structures? Are they integers? And that in order to perform its operations efficiently, it actually is using this notion of ordering, keeping them in an order so that it can iterate an order. It can quickly find on the basis of using order to quickly decide where something has to be and if it’s present in the collection.

So how does it know how to compare something that’s of unknown type? Well, what it does is it has a default strategy. It makes an assumption that if I used equals equals and less than that would tell me kinda where to go. And so it’s got this idea that it wants to know. We’ll give it two things. Are they the same thing? In which case, it uses kinda the zero to show the ordering between them. If one precedes the other, it wants to have some negative number that says well this one precedes it, or some positive number if it follows it. So it applies this operation to the [inaudible] that are there, and for strings, and ints, and doubles, and characters, this works perfectly well. And so that’s how without us going out of our way, we can have sets of things that respond to the built in relational operators without any special effort as a client. But what we can get into trouble with right is when equals equals less than don’t make sense for type.

So let me just go actually type some code, and I’ll show you. I have it on the slide, but I’m gonna – wow, this chair suddenly got really short. [Inaudible] fix that. Okay. We’ll go over here because I think it’s better just to see it really happening, so I’m gonna ignore this piece of code because it’s not what I wanted. But if I make some student T structure, and it’s got the first and last name, and it’s got the ID number, and maybe that’s all I want for now – that if down in my main – can’t find my main. There it is. I’m gonna need that piece of code later, so I’m gonna leave it there. If I make a set, and I say I’d like a set of students – my class – and I do this. And so I feel like I haven’t gotten [inaudible]. I made this structure. I say I’d like to make a set of students, that each student is in the class exactly once and I don’t need any duplicates, and I go through the process of trying to compile this, it’s gonna give me some complaints. And its complaint, which is a little

bit hard to see up here, is there’s no match for operator equals equals in one, equals equals two, and operator less than in one equals two.

So it’s kind of showing me another piece of code that’s kind of a hidden piece of code I haven’t actually seen directly, which is this operator compare function. And that is the one that the set is using, as it has this idea of what’s the way it should compare two things. It says well I have some of this operator compare that works generically on any type of things using equals equals and less than. And if I click up here on the instantiate from here, it’s gonna help me to understand what caused this problem. The problem was caused by trying to create a set who was holding student T. And so this gives you a little bit of an insight on how the template operations are handled by the compiler, that I have built this whole set class, and it depends on there being equals equals and less than working for the type.

The fact that it didn’t work for student T wasn’t a cause for alarm until I actually tried to instantiate it. So at the point where I said I’d like to make a set holding student T, the first point where the compiler actually goes through the process of generating a whole set class, the set angle brackets student T, filling in all the details, kinda working it all out, making the add, and contains, and whatever operations, and then in the process of those, those things are making calls that try to take student T objects and compare them using less than and equals equals. And that causes that code to fail. So the code that’s really failing is kind of somewhere in the class library, but it’s failing because the things that we’re passing through and instantiating for don’t work with that setup. So it’s something we’ve gotta fix, we’ve gotta do something about. Let me go back here and say what am I gonna do. Well, so there’s my error message. Same one, right? Saying yeah, you can’t do that with a [inaudible].

Well, what we do is we use this notion of functions as data to work out a solution for this problem that if you think about kinda what’s going on, the set actually knows everything about how to store things. It’s a very fancy efficient structure that says given your things, keeps them in order, and it manages to update, and insert, and search that thing very efficiently. But it doesn’t know given any two random things how to compare them other than this assumption it was making about less than and equals equals being a way to tell. If it wants to have sort of a more sophisticated handling of that, what it needs to do is cooperate with the client – that the implementer of the set can’t do it all. So there’s these two programmers that need to work in harmony. So what the set does is it allows for the client to specify by providing a function.

It says well when I need to compare two things, how about you give me the name of a function that when given two elements will return to me their ordering, this integer zero, negative, positive that tells me how to put them in place. And so the set kind of writes all of its operations in terms of well there’s some function I can call, this function that will compare two things. If they don’t specify one, I’ll use this default one that maps them to the relationals, but if they do give me one, I’ll just ask them to do the comparison. And so then as its doing its searching and inserting and whatnot, it’s calling back. We call that calling back to the client. So the client writes a function. If I wanna put student Ts into a

the default version of it looks. It’s actually written as a template itself that given two things it just turns around and asks the built in operators to help us out with that. And that is the name that’s being used if I open the set and you look at its constructor call. I had said that I would come back and tell you about what this was that the argument going into the set constructor is one parameter whose name is CMP function that takes two elem type things – so here’s the one in the example of the two argument prototype – returns an int, and then it uses a default assignment for that of operator compare, the one we just looked at, so that if you don’t specify it, it goes through and generates the standard comparison function for the type, which for built-ins will work fine, but for user defined things is gonna create compiler errors.

So you can also choose if you don’t like the default ordering works. So for example, if you wanted to build a set of strings that was case insensitive – so the default string handling would be to use equals equals and less than, which actually does care about case. It doesn’t think that capital [inaudible] is the same as lower case. If you wanted it to consider them the same, you could supply your own. A compare case insensitively function took two strings, converted their case, and then compared them. And then when you establish a set of strings, instead of letting the default argument take over, go ahead and use your case insensitive compare, and then now you have a set of strings that operates case insensitively. So you can change the ordering, adapt the ordering, whatever you like for the primitives, as well as supply the necessary one for the things the built-ins don’t have properties for. So then that’s that piece of code right there.

All right. So does that make sense? Well, now you know kind of the whole range of things that are available in the class library. All right, so we saw the four sequential containers, the vector, stack, queue, and the grid that you kinda indexed ordering, and kinda allowed you to throw things in and get them back out. We went through the map, which is the sort of fancy heavy lifter that does that key value lookup, and then we’ve seen the set, which does kind of aggregate collection management, and very efficient operations for kind of searching, retrieving, ordering, joining with other kind of sets, and stuff that also has a lot of high utility. I wanna do one quick little program with you before I start recursion just because I think it’s kinda cool is to talk a little about this idea of like once you have these ADTs, you can solve a lot of cool problems, and that’s certainly what this week’s assignment is about. It’s like well here are these tasks that if you didn’t have – So ADTs, just a reminder – I say this word as though everybody knows exactly what it means – is just the idea of an abstract data type. So an abstract data type is a data type that you think of in terms of what it provides as an abstraction. So a queue is this FIFO line, and how it works internally, what it’s implemented as, we’re not worried about it at all. We’re only worried about the abstraction of enqueue and dequeue, and it coming first in first out.

So we talk about ADTs, we say once we have a queue, a stack, or a vector, we know what those things do, what a mathematical set is about. We build on top of that. We write code that leverages those ADTs to do cool things without having to also manage the low level details of where the memory came from for these things, how they grow, how they search, how they store and organize the data. You just get to do real cool things.

So you probably got a little taste of that at the end of 106A when you get to use the array list and the hashmap to do things. This set kinda just expands out to fill out some other niches where you can do a lot of really cool things because you have these things around to build on. So one of the things that happens a lot is you tend to do layered things, and you’ll see a little bit of this in the assignment you’re doing this week where it’s not just a set of something, it’s a set of a map of something, or a vector of queues, a map of set. So I gave you a couple of examples here of the things that might be useful.

Like if you think of what a smoothie is, it’s a set of things mixed together, some yogurt, and some different fruits, some wheatgrass, whatever it is you have in it. And that the menu for a smoothie shop is really just a bunch of those sets, so each set of ingredients is a particular smoothie they have, and then the set of all those sets is the menu that they post up on the board you can come in and order. The compiler tends to use a map to keep track of all the variables that are in scope. As you declare variables, it adds them to the map so that when you later use that name, it knows where to find it. Well, it also has to manage though not just one map, but the idea is as you enter and exit scopes, there is this layering of open scopes. So you have some open scope here. You go into a for loop where you open another scope. You add some new variables that when you look it actually shadows then at the nearest definition, so if you had two variables of the same, it needs to look at the one that’s closest. And then when you exit that scope, it needs those variables to go away and no longer be accessible. So one model for that could be very much a stack of maps where each of those maps represents the scope that’s active, a set of variable names, and maybe their types and information is stored in that map.

And then you stack them up. As you open a scope, you push on a new empty map. You put things into it, and then maybe you enter another scope. You push on another new empty map. You stick things into it, but as you exit and hit those closing braces, you pop those things from the stack to get to this previous environment you were in. So let me do a little program with you. I just have this idea of how this would work, and we’ll see if I can code something up. So I have – let’s go back over here. I’m going to – this is the piece of code that just reads words, so that’s a fine piece of code to have here. I have a file here – let me open it up for you – that just contains the contents of the Official Scrabble Players’ Dictionary, Edition 2. It’s got a lot of words it. It’s pretty long. It’s still loading. Let’s go back and do something else while it’s loading.

It happened to have about 120,000 words I think is what it would be right now. So it’s busy loading, and I have this question for you. There are certain words that are anagrams of each other. The word cheap can be anagrammed into the word peach, things like that. And so I am curious for the Official Scrabble Players’ Dictionary what – so if you imagine that some words can be anagrammed a couple times, five or six different words just on how you can rearrange the letters. I’m curious to know what the largest anagram cluster is in the Official Scrabble Players’ Dictionary.

So I’d like to know across all 127,000 words that they form little clusters, and I’d like to find out what’s the biggest of those clusters. Okay. That’s my goal. So here’s what I’ve got going. I’ve got something that’s gonna read them one by one. So let’s brainstorm for

smallest is gonna small index – we’ll call it min index. That’s a better name for it. Min index equals I, and then I’m gonna look through the string, and I’m gonna find the smallest letter that’s there and move it to the front. That’s basically gonna be my strategy. I’ve got the wrong place to start, though. I’m gonna look from I to the one.

So if S sub J is less than S sub min index, so it is a smaller letter, then the min index gets to be J. So far, what I’ve done is I’ve run this loop that kind of starts in this case on the very first iteration and says the first character in Slot 0, that must be the min. And then it looks from there to the end. Is there anything smaller? Whenever it find anything smaller, it updates the min index, so when it’s done after that second loop has fully operated, then min index will point to the smallest alphabetically character in the string that we have. Then I’m gonna swap it to the front. So S sub I with S sub min index, and I’ll write a swap because swap is pretty easy to write. See about how we do this, okay. So if I – I like how this works, and then let me stop and say right here, now is a good time to test this thing. I just wrote signature. It probably works, but it potentially could not. And I’m just gonna show you a little bit of how I write code. This is good to know. As I say, I put some code in here that I plan on throwing away. Enter word – and then I throw it through my function. I think I called it signature, didn’t I? Signature S – so the idea being if it doesn’t work, I wanna find out sooner rather than later. It doesn’t like my use of get line. Is that because it’s not included? Yes, it’s not. So let’s go get the right header files. This is half the battle sometimes is figuring out what headers are needed to make your compiler happy.

So now it’s up here at enter word, and I say what does cheap come out as? It goes into the debugger. That’s good. So it wasn’t right. Now we’re gonna get to find out what does it not like about that. It says – did we forget to return something? Let’s go look at our code. So it’s complaining about – oh yeah, I can see where we’re gonna get in trouble here. It’s complaining that the return – it was saying that I’m trying to print something and it looks like garbage, that what I’m trying to print didn’t make sense at all. I could say well that’s funny. Let’s go look at signature.

Student: [Inaudible] pass the [inaudible].

Instructor (Julie Zelenski) :That’s probably a good idea. We ought to fix that while we’re there. Let me leave that bug in for a minute because I’m gonna fix my first bug, and then I’ll come back to that one. So it turns out what it’s really complaining about though is it has to do with – I said well what does signature return. Somehow, what’s being returned by signature is being interpreted as total crap when it got back to main, and there’s a very good reason for that because I never returned anything. So maybe if I had been less cavalier about the fact that it was giving me a warning over here that said control reaches the end of non-void function, but I was being lazy and I didn’t look – was that I didn’t pay attention to the fact.

So let’s leave my other bug in that you’ve already pointed out because this is exactly what it’s like when you’re doing it. You enter words and you say cheap, and it says cheap, and you’re like no. And then you’re like about how about an. Hey look, that’s in

alphabetical order. And then you spend all your time thinking about words for a while. Well, tux. That’s in alphabetical order. It seems to work. You could do that for a while, but then you’re like this whole cheap, not good. Okay, so I come back here and my friend who’s already one step ahead of me has pointed out that my swap is missing the all important pass by reference that as it is it what swapping the copies that got passed to the function, but of course nothing was happening back here in signature land. So if I fix that, and I come back in here, I’m gonna feel better about this. Oh, my gosh. And even tux still works. And if I say banana, I should get a bunch of As, a bunch of B and an N, so there’s various cases to test out if you have multiple letters, and if you have letters that are the same letters like apple so that it doesn’t lose your duplicates, and it seems to come out right. So given this, the word peach should come down to the same signature as cheap, and so that seems to indicate we’re on the path toward building the thing we wanted to build. So I have this read file thing that I have left over from last time that reads each word, and I wanna change my vector into a map of set of string. What capitalization do you not like?

Student: [Inaudible].

Instructor (Julie Zelenski) :Yeah. So it turns out this file happens to all be lower case, but there’s no harm in doing this. That way even if they weren’t, it’ll take care of that problem for us if we wanted it to.

Student: [Inaudible].

Instructor (Julie Zelenski) :I want lower case. Oh yeah, I do. Well, your case. You guys are so picky. All right. Here’s my deal. I’m gonna take my map, and this is gonna be a line of code that’s gonna make your head spin. Just go with it. This is the do all craziness. Okay. So I’ve got this map, and what I wanna do is under the signature of this word, I want to look up the set of strings that’s associated with it and tack this one in. And the add in this case with the set, I know that’s gonna do non-duplication. In fact, the file doesn’t contain duplicates, but if it did, I certainly don’t want it to record it twice anyway, so I might as well do this. Now this form of this is a heavy lifter for a small piece of code. The signature then went and converted into the ACEHP form. I used that as the key into the table. If it was already there, it’s gonna retrieve me an existing set that I’m gonna just go ahead and add a word onto. If it wasn’t there, the behavior for the brackets is to create kind of a new empty value for that. So it’ll use the key and create a default value for type.

Well, the default value for set of string – so if you just create a set without any other information in the default constructor will always create you a nice clean empty set. So in fact, it will get me exactly what I want which is to put it in there with an empty set that I will immediately add the word into. So after I do this, I should have this fully populated map. And then I’m gonna do this just as a little test. I’m gonna say num words when I’m done to feel a little bit confident about what got put in there. How about I call it – that’s a good idea. It’s so fussy. C++ never does what you want. I think I called this thing OSPD2.txt that has the words in it. And then I need to declare the variable that I’m

Instructor (Julie Zelenski) :Yeah. So it doesn’t know anything about everything else that was [inaudible], but in fact that’s why it’s [inaudible]. I know the dictionary has about 127,000 words. It turns out they form about 112 unique signatures, and so there’s actually another 20,000 words that are clung onto some existing signature. That’s the number of unique signatures across the dictionary, not the number of words, so that’s probably the wrong name to call it.

Student: For the M signature word thing where [inaudible] of the default just to create a new stack, that works as well for vectors [inaudible]?

Instructor (Julie Zelenski) :Yeah. It works for anything if you were just to declare it on the stack and the right thing happened, so vectors, set, maps. All those things do. But the primitive types like int and double, it doesn’t. So it would work for string. String is an empty string.

So for some of the fancier, more modern types tend to actually know how to just default construct themselves into a good state, but the primitives don’t do that. So if you were having a map of ints and you wanted to have them start at zero, you need to really start them at zero. You can call just M sub this. It would be garbage, and it would just be operating with garbage from that way forward. All right. Well, we’re good. What I’m gonna give you is the eight minute discussion of recursion that whets your appetite for the things we’re gonna be doing next week.

So recursion is one of those things I think that when you haven’t yet had a chance to explore it first hand and other people tell you about it, it has sort of an awe inspiring sort of mystery, some fear, and whatnot. So first off, I wanna kinda shake that fear off. It is a little bit hard to wrap your head around the first time you see it, but we’re gonna have a whole week’s worth of time to spend on it, so we’re gonna try to give you a lot of different ways to think about it, and different problems to see to kinda help you do it. And I think once you do get your head around it, it turns out then you’ll discover how infinitely powerful it is, that there is kind of a simple idea in it that once you kinda fully get your head around, you can explore and solve lots of different problems using this just one technique again and again.

So in itself, it’s a little bit mysterious at first glance, but then once you kind of master it, you’ll be amazed at the kind of neat things you can do with it. So it is certainly what I’d consider an indispensable tool in a programmer’s tool kit. The kind of problems you can solve using the techniques you have so far is fundamentally limited. And part of what we need to do in this class is expose you to these new ways of solving harder, more sophisticated problems that the old techniques don’t work for. One of the cool things about recursion is it actually lends very simple, elegant, short solutions to problems that at first glance seem completely unsolvable. That if you can formulate a structure for [inaudible], you will discover that the code is not long to write. It’s not tedious to write. The tricky part is to figure out how to express it, so it’s more of a thinking puzzle than it is a coding puzzle. I certainly like thinking puzzles as much as coding puzzles if not more.

The general sort of idea is that you are going to try to solve a problem – instead of sort of breaking it down into component tasks like if I need to make dinner, I need to go to the store and buy things, and I need to come home, and chop them, and get the recipe. You think of what your standard decomposition is all about – breaking down your tasks into A, B, and C, and D, and then you add them all together to get the whole task done. Recursion has this kind of very different way of thinking about the problem, which is like well if I needed to get Task A done, and I had Task A prime, which was somehow a lot like the task I was trying to solve, but it somehow was a little bit simpler, a little bit easier, a little bit more manageable than the one I started out to solve, and if I had that solution – so if somehow I could delegate it off to some minion who works for me, and then I could use that to solve my problem, then my job would be made much easier by using that result of solving a similar problem that’s a little bit [inaudible].

Okay, that seems a little bit wacky. Let me give you sort of an example of how this might work. So your standard problem, I said yeah, it’s like you do these dissimilar sub problems. Let’s imagine I had this goal where I wanted to survey the Stanford student body. I don’t want just like a haphazard most of the people involved. Let’s say I really wanted to get input from every single person on campus whether they think having cardinal as your mascot is a ridiculous choice or not. So let’s imagine I really wanna hear from all 10,000 students. Now I can stand out in White Plaza with a big note pad and try to accost people and sort of work my way down the list. And then I’d be there for eons and never solve my problem. Instead, what I decide to do is I say well I’m gonna recruit some people to help me because I’m lazy as we’ve already established, and I would like to get some other people to join in my quest to answer these burning questions and to solve the survey.

So what I do is I round up ten people let’s say, and I say would you help me, and I decide to divide the campus into kind of ten partitions. And I say if you could survey all the people whose names begin with A, B, and C, that would really help. And if you could do [inaudible] Gs, and if you would do – and if I divide up the alphabet that way, give each of them two or three letters, and I say if you would go get the data, it’d be really easy for me to do my job then. If I just took all their data [inaudible]. Well, being the kind of lazy person that I am, it’s likely that the ten people I recruit would have similar lazy qualities because lazy people hang out with other lazy people. And so the person who was in charge of A, B, C, the first thing they do is turn around and find ten more friends, and then they divide it up and say could you do the AA through AM and so on. If they divide it into these pools of one tenth of what they were responsible for, and say you can go get the information from these people, and if they did the same thing – so if everybody along the road. We started with 10,000. Now each person had 1,000 to survey. They asked their friend to do 100. Their friend asked ten people to do ten. And then at some point, the person who has ten says well I just need to ask these ten people. Once I get their data, we don’t need to do anything further.

So at some point the problem becomes so small, so simple, even though it was kind of the same problem all along. I just reproduced the same problem we had, but in a slightly more tractable form, but then I divided it around. Divide and conquer sometimes they call