 All right, how are we doing? You hear me? Hear me? Yeah, good. Okay, welcome to computer science 61a, the best computer science class in the world. And I'm allowed to say that because it's not because of me, it's because of this book which is called structure and interpretation of computer programs, which uncoincidentally is also the name of the course that you're taking. Best computer science book ever written, you're going to learn an awful lot. We're using a programming language called scheme. I want to emphasize right at the beginning that this is not a course about scheme. In fact, you're going to learn all of scheme in the next hour. And then we can talk about big ideas. We're using a particular implementation of scheme called STK. Here it is. I should have got ready. I had a time here. Okay, and we can type stuff into it. And the way it works is you ask it a question, it tells you the answer. So I say six. It says six. I say plus six eight. It says 14. You'll notice in passing that this is not the way you were taught to write addition problems. In grade school. The reason that scheme uses this notation is it can use it uniformly. So in math, there's infix notation with the operator in the middle for some things. And then there's then there's prefix notation for other things. And then there's post fix notation. And then around the operator, or the operand, I mean notation for other things. So this is not very uniform. And also hardly any of these work very well if there's more than two operands. So we standardize on having the operator first. And that means I can say what's the sum of six and eight and two and ninety-nine and four thousand, and it'll work. Okay, what do you think? Poor. Yeah, what do you think? Here's some errors and some zeros. It's actually zero. Times one. Because that's the identity element for the multiplication operator. So it sort of makes sense mathematically to do that. Some other operators don't like doing that. So it's not every function that have variable numbers of arguments. You can't call division with no arguments. How about if I just say plus, not in parentheses? Error. Nope. I know that looks like it might be an error message, but it isn't. This is the way that STK represents functions. So the value of the symbol plus sign is a function. The same as, you know, you always learn that, you know, sign is a function. So it's plus. Okay. Good. What if I would like to have an expression whose value is the plus symbol? I can do that as follows. I say quote, is the bottom showing up? Yes, great. Quote plus sign. I don't know if you can see clearly back there in the back, but it's actually a single quote, otherwise known as an apostrophe. And quote followed by any word, the value of that is just that word. So if I say hello, I get an error. So I'm asking what's the value of the symbol hello and there isn't a value, but if I say quote hello, I get back hello. Okay. Question so far? Great. We can take the result of one function and use it as an argument to another function. So I can say add up three times seven and ten times ten. Okay, that's called composition of functions. You learned about it in seventh or eighth grade. And it turns out to be such a powerful mechanism for organizing computations that it's basically all we need. So you now know, we're like 15, no, we're five minutes into the class. You've now learned about 90% of the scheme language. Okay. Namely, parenthesis, function, argument, argument, argument, argument, however many there are, closed parenthesis means call this function with these argument values. Okay. And what scheme does is first it evaluates the argument expressions. So it evaluates, for example, times three seven gets 21. It evaluates times ten ten gets a hundred and then it calls finally the plus function with those values. Okay, so first evaluate the arguments then call the function with the actual argument values. That's it. That's the language. Well, almost. First of all, not all functions are functions of numbers. I can say something like first of quote hello and get H or last of quote hello get O or but first of quote hello. Congratulations on not laughing. Get hello or but last they wouldn't let me do this if we were teaching in Texas. We can abbreviate these things by the way, but first BF and so on. Okay, so those are functions. There's a constructor function called word. There's my favorite example. Now in here combine them and it just sort of butts them up together, makes a new word. That's the two concatenated. There's also sentence which you can abbreviate SE and it makes something we haven't seen before which is a sentence which is a bunch of words in parentheses. Okay. Now notice this is something scheme is printing out as a result this sentence. If I were to type in parenthesis now here close parenthesis that would not mean I want the sentence now here. It would mean please call the function named now with the argument named here. Okay, if I actually want a sentence I quote it the same as you can quote a word. Okay. First but first last but last applied to sentences carves out pieces that are words. So but first of all but the first word, okay. And these can be composed also so I can say first of but first of get, oops, oh, sorry, I think I'll go too long. There we go. So first of but first. What does that do? Well, but first of a hard day's night is hard day's night and first of hard day's night is hard. All right. How about first of first, oops, I keep doing it. First of first of but first of She loves you. What am I gonna get? L, yeah. First letter of the first word of all but the first word. Oh, she loves you. Okay. Great. Can you guys listen and hand and pass out handouts at the same time without reading the handout? Just pass them around. If you're really smart we can do this in order of this period of end time. I'm just gonna start this halfway back. That was only the first block. All right. Tell you what. Could you take these and just pass them straight back? Until they get like four rows from the end and then people should start making them. Same for you. Okay. The first row that doesn't have handouts can start this one when it gets to you. You don't get for many. The other 10 cent team language. We can give names to things. So I can say four, five, and here's the area of a circle of radius five. Define procedures. There we go. Ah, wow. So loud. Usually by convention you just go to another line to put the actual body of the procedure, but it's only a convention. Scheme treats a new line the same way it treats a space. It's just a separator. So now I've defined a procedure. By the way, notice when you say define what you get back is as the return value is the name that you're defining. So up here I defined pi and it said pi and here I'm defining square and it says square. So what does this mean? It means when you call square with an argument like if I say square of plus two three, what happens first? Plus two three. First thing we do is evaluate the sub expressions to get the argument values. We're also, by the way, evaluating this sub expression whose value is the function we're calling. So we get the value five. Good. And now in the body of square, which is this expression here, wherever we see an x, we put in five. And so we get times five five and that's 25. Okay. So that's the rule of evaluation for scheme, almost always. There are a few exceptions. One of the exceptions is define itself. Because when I say define pi, at the time I say this, pi has no value. So if this used the ordinary procedure calling rule of first evaluate the argument expressions and then call the function, we get an error on this call to define. Unbound variable pi. And the same here. Square x. Well if I haven't defined square yet, this doesn't mean anything. And this, although it looks as if it means something, it actually doesn't until we substitute some value for these x's, right? So define is what's called a special form. Actually, technically the whole expression that you type is called a special form. Define is called the keyword, which is the name of a special form. And what's special about it is that it doesn't do this thing of first evaluate the arguments, etc. Actually, it depends which kind of define and you'll learn why this is in a week when we look under the rug here, under the hood of this. But if I say define hello to be plus two three, the value of hello is five, right? It's not plus two three. This got evaluated. If this were inside parentheses, then we'd be defining a function and then nothing would be evaluated until we call the function, okay? Great. There are, oh maybe a dozen. They say in teacher school you're supposed to say any phone that rings in my class is mine, but I'll let you off the hook this time. Oh, yeah, special forms. There's about a dozen of them. Define is the first one that you're learning and we'll see a couple more as this week progresses. Okay, let me take a couple of minutes to talk about administrative things. What you need in the way of materials for this course, you need the handout that I hope you just got. Who doesn't have a handout? Okay, anybody who has more than one handout, send them that way to the top right. You're right. And here they come. Great. So you need a handout. You need another handout that you're going to get in a minute. You need a computer account form and I'll talk about that in a second. Those are the things we give you and then there's some things you have to buy. The most important one is the textbook. You can buy it used if you want. If you do, make sure it's the second edition, not the first edition. Second is that for a long time, so it probably is, but check. In my opinion, you're going to want to keep this book forever and sleep with it under your pillow. But you know, there is a used market because I guess not everybody agrees with me. There's also a course reader. It comes in two volumes, a thin volume one and a thick volume two. The reason it comes in two volumes is that you have to get a brand-new this semester's volume one for nine dollars and forty-one cents. But if you find a used volume two, that's okay. Volume two is the stuff that doesn't change from semester to semester. You need the volume one because it has all of your assignments for the entire semester are in here. All the labs, all the homeworks, all the projects are in here. So that's important. What's in the big fat one? One thing is my lecture notes. So my goal is that you shouldn't have to write stuff down in here. And that's because I vaguely remember being a student and I can't think and write at the same time and I want you to be thinking in here. Okay, so you get my lecture notes. You can read along. If you know, I seem to be saying something that isn't quite in here or a clarification. You can scribble it in the margin, but mostly you should be listening and thinking and not writing. Another important thing that's in here is the scheme reference manual. So if you have a question about what's the order of the arguments to such and such, you can look it up. Other really important things in the big volume is some sample exams. So you can get an idea of what the exams are like. There are way too many exams in this course. We give three midterms and a final. I keep trying to cut back and students always insist that we have to do this, which sounds kind of masochistic. But the reason is the fewer exams there are, the more it matters to your grade if you totally mess up one problem. So students like to have a lot of problems to sort of smooth out their errors in the midterms. So that means that the TAs and I are pretty much constantly either writing an exam or grading an exam. Which is the only bad thing about teaching in this class. That and arguing with students of bad grades. That's another bad part. Okay, let me hand out the other handout actually. It's only one page. This is the last-minute update. So here you go, pass those around. Pass those around. What's in the last-minute update Mainly is information about which TA is teaching which section when and where they meet and the all-important which sections have rooms. So if you are on the waiting list for this class or if you're an extension student and want me to sign you into the class The answer is we have enough spaces for everyone. Okay, everyone will get in. Provided that you are willing to meet at a section time that has openings. Okay, so you have to meet us halfway about this. If you absolutely can't meet any of the section times that have openings you can attempt to make a trade with someone else. And there's two ways to do that. One is to just sort of show up at the section you're trying to get into and ask the TA if you can just yell out Hey, can anybody take section such and such that I'm enrolled in? But you should enroll in a section, even if you don't like the time, enroll in one of the open sections and that will get you into the class. If you want me to sign a form because you're an extension student, I am not going to sign your form until the TA signs it first saying that you're in a lab and discussion section. Okay? So I want to make sure that everybody's really in a section. It matters a lot. The lab and discussion sections, you probably all figured this out already, are linked. So if you're in lab section 15, you're in discussion section 115. They go together. And it's the same TA and the same students and that's good because you're going to be working in small groups and you really get to know your partners. So a week from now, your TA is going to organize you into groups of four and you are really going to get to know this group of four very well because you're going to collaborate on some projects. You're going to collaborate on some exam questions. There will be a couple of group questions where your whole group gets together. We got the answer to something. I really recommend that you study together. And please do your best to make sure that nobody in your group gets lost and despairs and drops out. Okay? Try to support your group members. Why? Because your entire group gets bonus points if the lowest scoring person on an exam from your group gains five points in the next exam. Okay? So if you can take your sort of weakest link in your group and support that person to really do better, you get two bonus points. If that person drops out, you don't. Okay? So this is a reward for working together. Having said that, there are limits to your working together. You're going to be hearing this from every single instructor this week, right? You've probably already heard it six times. Don't cheat. I think that some of what people tell you about this is nonsense. For example, people will tell you that you're hurting your fellow students by cheating. That would be true if this course were graded on a curve, but it's not. Now, I'm grading on a curve as evil because it makes you compete with each other instead of cooperating. So you are not hurting anybody else by doing well in the class. Another thing that I've heard people say that isn't true is that you are going to harm the reputation of the University of California if you cheat. Now, come on. Every three or four years some football player rapes a townie at a fraternity party and that's terrible, but the reputation of the University of California is pretty good. So that's not why you shouldn't cheat. Here's why you shouldn't cheat. You guys right now are constructing the person you're going to be for the rest of your life. Human behavior is mostly a matter of habits. People talk as if you make big decisions all the time about what to do, but that's not true. Almost all the time you just do what you're in the habit of doing. If you get in the habit of cutting corners this early in your career, you know, how are you going to make it through the harder upper division classes? Then what are you going to do when you actually get a job and the person next to you isn't doing the same thing you're doing? You're not going to be able to look over somebody's shoulder, but you are going to be able to find ways to cut corners and I don't want to fly in an airplane that was programmed by somebody who cheated in this class. So really, and furthermore, what's the best thing that can come out of cheating? You condemn yourself to a life of doing something you don't know how to do and don't like doing. Okay, so don't cheat. If you do, you're really hurting yourself. And the thing to do instead, the way to avoid cheating, in this class, I don't believe we've ever had the kind of conspiracy you read about in the newspapers where somebody breaks into the professor's office, you know, and blah, blah. That doesn't happen. What happens is people panic at the last minute and cheat. The way to avoid that is if you don't understand something, don't think, I'm going to wait till next week and see if it gets better. Don't think, I don't want to bother the poor professor. It's what I'm here for. Come to my office hours. Come to your TA's office hours. The very minute you don't understand something, okay? Good, so don't cheat. Other administrative things. If you're in section 20 and 120, I really apologize for the fact that the lab and discussion times are half an hour offset from each other. It's a long story, but it was the best we could do. And the CSUA help sessions that you were told about right at the beginning are at the very end of this one page last minute handout. Okay, that's it. All of your other administrative questions are answered in this handout. So, I would prefer not to have administrative questions today. You can ask administrative questions on Friday. If the answer is in this handout, you owe me a dollar. Okay? I don't feel that way about course content questions. If you ask the very same question that somebody asked a minute ago, because you didn't understand the answer, that's fine. Okay? Don't refrain from asking questions because you think it's a stupid question. Stupid questions are the best ones. Because if you have a stupid question, then probably 50 other people in this room have the same question. Whereas if you ask, I'm going to prove how smart I am question, then, you know, you won't make any friends. All right, now let's get back to work. Yeah. Oh, okay, are there last minute handouts of pile of them somewhere? On the back. Okay, hands up if you don't have one. Okay, so people who have the big pile, I'll pass it for some of it that way and some of it down here. Okay, we should have just about enough, I hope. If not, we'll make more. Oh, let me write this down. One administrative thing that I should have put in the handout, but I'm not sure I did. Jenny is the staff support person for this class, among other things. And if you're missing a handout or something, you know, you need to reschedule an exam. She's the one to see. Okay, back to work. Oh, yeah. So, okay, here's your vocabulary lesson for today. This thing is called a formal parameter. You don't have to write it down. It's in my lecture notes. Just understand it. So the formal parameter is the name for an argument to a function. All right. This thing here is the body. Okay, those things are part of the definition of a function. This is the actual argument expression. And then a sort of invisible thing is the actual argument value, which is five. Okay. So there are three things that can loosely be called the argument to the function. There's x, which is its formal parameter. There's plus two three, which is its actual argument expression, and there's five, which is its actual argument value. Almost all the time, either it's obvious which one I mean, or I kind of mean all of them together, and I'll just say the argument. Or the input, even. When it matters, those are the names, and we'll try to be clear about that. Okay. Oh, yeah. About asking questions. They, because they're webcasting this and everything, they're shining bright lights in my eyes, and so I might not be able to see your hand in the air, and if that's the case, wave it around. This is, this is your physiology lesson for today. Your eye sees moving things a lot better than stationary things. So I really can see your hand better if it's moving. If that doesn't work, yell. Okay. So here's a typical function that you might define. So I can say plural computer, here it is. Plural book, plural, boy, I noticed I'm quoting the words that I'm using as inputs. Because quote computer is the actual argument expression, and computer is the actual argument value, or book or boy. Okay. Well, there's a problem with this. It doesn't quite work. That's the wrong answer. Right. So I'm going to fix it. Oh, one little comment before I go on. I used WD as the formal parameter. Why didn't I just use word? It's not just because I'm a lazy typist. It's because in the body of plural, I'm using a primitive function called word W-O-R-D. And it has to have a different name from this. Okay. A name can only mean one thing at a time. So that's why you'll see me as wooded for words and sent for sentences as formal parameters. Okay. So if what, what's different about fly? It ends in a Y. Equal, question mark. Equal question mark is a built-in predicate function. A predicate is a function that returns the value true or false. Okay. It's not a rule built into the language that predicates have to end in question mark. It's a convention that they almost always do. Okay. It just helps you read programs more easily. Although it actually in one way makes it harder to repurpose. You have to say if equal or something. So it's like Chinese where the tone matters. So if the last letter of wood is equal to quote Y, last of word is a procedure call, but the Y, I actually want the letter Y itself. So it's quoted. So if those are equal then I'm going to return word all but the last letter of my argument word and then IE S. Otherwise, same as before word would quote us. So book still works, fly works, but I've broken boy. You're going to fix that in the lab. Okay. If is a special form. Yes. Okay. Question is there's no distinction between a character and a string with only one character in it. That's right. These are my crossed fingers here. If you read the reference manual, you'll see that there is. But for our purposes there are neither characters nor strings. There are words and there are sentences. Okay. Yeah. Hush. A string is a bunch of characters in those other languages. Yes. Is there a function that gives you a character in the middle of a word? Well, yeah. We found the second character, remember, by taking first of but first. So you can make compositions of those to do that. There's also something made up of that. Let's see. I think this way. No, it works this way. No. Whoa. That's interesting. Maybe I'm wrong. Maybe there isn't one. For words. Good question. Oh, there's item. Let's try that. Item for quote computer. Yeah, there we go. Item. Oh, yeah. Thank you for reminding me. All the stuff about words and sentences. This is why I had my fingers crossed in your question. All the stuff about words and sentences is stuff that we added to scheme here at Berkeley for your benefit in this class. In sort of ordinary scheme, like in the reference manual, there's no such thing as words and sentences. They deal with text in different ways. This way is better. It's easier. In a few weeks, we'll sort of see what underlies it. But for now, it's much, much easier to think about it this way. The idea of words and sentences came from a language called logo. Anybody ever programmed in logo? Huh, a few people. Used to be more than that. You're going to get to know it very well toward the end of the semester when you write a logo interpreter. But anyway, we've imported this idea of words and sentences from there, and it just makes life easier. Okay. Moving on. Oh, yeah. Let me do this. Whoops. Yeah. Uh-huh. His question is, can I write it my own little reader that lets you leave out the apostrophe? You don't want to. Because sometimes you really do want to know what's the value of this as a symbol. Or what's the value of this procedure call? So you really want to make it. It's not just a stupid syntactic distinction. It's the difference between program and data, basically. And it really matters. So I wouldn't think that way if I were you. Okay, so here's a little Pig Latin program. Who speaks Pig Latin? Oh my god, what are they teaching school these days? All right. Pig Latin, when I was a kid, was a secret language that kids use to keep secrets from their parents. I guess these days it's a language that parents use to keep secrets from their kids. And the way it works is to translate a word to Pig Latin, you take all the leading consonants, move them to the end, and add a y. So Pig, whoops. Pigle scheme, for example, is Emskay. So we take the S-C-H, everything up to the first vowel, up to but not including. Move it to the end and add a y. Okay. Now, how does this program accomplish that? Let's take a look. It says if P.L. Dunn question mark of word. In this example, we're using a lot of helper functions. Maybe more than you really need. If I were writing this for my own purposes, I'm not sure I would have something called P.L. Dunn question mark. I might just put this expression up in there. But for purposes of teaching how this works, we have a predicate function called P.L. Dunn that takes a word and it asks the question. Is the first letter of this word a vowel? And we take first of word and then we ask vowel, true or false. Now, how does vowel work? That's not built in. But member question mark is built in. So we're saying is this letter a member of A-E-I-O-U. If so, then it's a vowel and so I take the word that I was given and I add a y at the end. Otherwise, here's the fun part. I take all but the first letter and then I stick the first letter after that. So if I'm given scheme, I turn it into jeans, moving the S to the end. But that's not the answer I want. So what I do is I ask for pig Latin of that. Okay, so the entire expression here is pig all word, first of word, first of word. And that will compute pig Latin of jeans, which will compute pig Latin of hemsk, which will compute pig Latin of hemsk, which will say, aha, now it starts with a vowel, I'll stick a y at the end. There was a hand up. Yes. Okay, the question was instead of a member here, can I use equal question mark? That's where you mean? No, you can't. That letter E, for example, is that letter E equal to this sentence? No, it sure isn't. For one thing, it's a word, not a sentence. And for another thing, it's just E, not eww. So no, member is what you want. Okay. Yes. Oh, why did I use letter as the argument? Down here? Oh, I used letter here because I used it here. Letter is the formal parameter to proceed your vowel question mark. Okay, so when we call vowel question mark, I say like, for example, vowel question mark of S, we're going to substitute S for the formal parameter in this expression. Okay. So that's why. Scheme doesn't care what word you use as a formal parameter. As long as it's not the name of a primitive or something that you need, you can use any word you want, but you use a word that helps you understand. So here, my argument is a whole word of, you know, many letters. Here, the argument is always going to be a single letter, so I'm reminding myself of that by the name that I picked. Okay? Yes. Ah, what if I use the word rhythm? I would be in trouble because this has a pretty simplistic sense of what a vowel is. That's true. It's very complicated to figure out whether y is a vowel or not, so I didn't do it for purposes of demonstration here. But let me get to the important point about this. This is procedure pigle, because we only have a couple of minutes. This is procedure pigle, and here it is calling itself. How can that work? This is the idea of recursion, which is a function calling itself. Recursive programming is a prerequisite for this class. If this is new to you, a procedure that calls itself, maybe you should take CS10 before you take CS61A. Okay? That's the prerequisite for 61A. We assume that this is all old hat to you. If it's a special form, it has to be because if we evaluated this recursive call before we called if, then even when we got to the base case, to the word that starts with a vowel, we would still try to do the recursive call over and over and over again. If, being a special form, it has its own evaluation rule, which is it starts by evaluating its first argument expression. That has to be true or false. If it's true, we evaluate the second one and forget about the third one. If it's false, we evaluate the third one and forget about the second one. Okay, so that's if is a special form. And let's see, we have one minute left. Who doesn't have a big handout? Who doesn't have a little handout? Oh, geez. Okay. Anybody who has extra handout, don't go yet. Very rude. Anybody who has extra handouts, please pass them down to the front and people who don't have them will pick them up. Also, you guys should be listening and not leaving. This might affect you. If you are enrolled in one of the sections that has not had a scheduled lab yet, that would be sections 20 and 21. Whose lab meets, oh, sorry, most important thing. This week only, both section meetings, the one that already happened and the one that would ordinarily be a discussion, are meeting in the lab. So this afternoon or tomorrow or Friday morning, you're meeting in the lab. If you're in sections 20 or 21, then your TA doesn't have your account forms because I just printed some up. So people in those sections only, I just have enough, should come up and get, I'm going to leave them not on the stage because they're going to rotate it, but here, an account form. If you're in some other section, your TA has your account forms. If you're on the waiting list, but you're going to be in section 20 or 21, then hang around and we'll see if it works. Okay, see you Friday. Thank you.