Lecture transcriptions - Programming Paradigms - 2008 - Lesson 19, Lecture notes of Programming Paradigms

Introduction to computer science. Lecture transcriptions of Programming Paradigms - Lesson 19 - Prof. Cain - Stanford University

Typology: Lecture notes

2010/2011

Uploaded on 10/20/2011

larryp
larryp 🇺🇸

4.8

(34)

352 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ProgrammingParadigms-Lecture19
Instructor (Jerry Cain):Hey, everyone. Welcome. I have a glorious set of handouts for
you today. I have a midterm solution and I have the first two handouts and a stream of a
few handouts I’m going to be giving you on our next paradigm and our next
programming language. You’ll see that I do have the midterm solution in there. I actually
distribute that, obviously, so you can check your answers, but also so that we’re
somewhat transparent in how we actually grade these things.
Turns out that these things amount to like 25 percent of your grade, so I like you to know
what criteria we’re using to consistently grade everybody so that if you see something
that is so clearly wrong in terms of the way we graded it, you can confirm that by looking
at the grading criteria and then coming to me if you have problems.
Usually when there’s a one-point error that’s not listed specifically on a criteria, that
doesn’t mean I’m gonna give it back to you. It means that you made a mistake that we
didn’t anticipate anybody making and we didn’t put it on the criteria. But if you have a
total of five points taken off for a problem out of ten, and it’s not clear why you have that
many points taken off, that’s a difference scenario, and so consult the answer key, and if
there really is disparity, come and talk to me about it.
You should come and talk to me about it if you’re worried about there being a
discrepancy because this thing does just end up counting a lot. So don’t feel shy about
coming back and asking for clarity as to why we took points off that we did.
So what I want to do today is I want to introduce a new programming language, and I
want to first illustrate a new paradigm, one that you’ve certainly not seen before unless
you’ve coded in other classes at Stanford or coded prior to Stanford. We have spent a lot
of time talking about these two paradigms, imperative. You’ll also hear me call it
procedural.
They’re not necessarily the same paradigm, but the language we’re using to illustrate
both of them is the same. We focus on C as the representative of those two paradigms.
We also have the object oriented paradigm UC++, and even though we know that CSC++
ultimately translate to the same type of assembly language that you kind of think about
the problem differently or we think about our solution to the problem differently when we
take a pure C approach versus a pure C++ approach.
The reason this is called imperative or procedural is that they’re focused around the verbs
that come up in the paragraph description of a solution. Think about what a main function
looks like in a typical C program. You declare all your data, and then you make a series
of call to these very high level functions like initialize, do it, terminate, and then return
zero or something like that. Do you understand what I mean when I say that?
Okay, so the first thing you see associated with any C statement is usually or very often
the name of a function that gets invoked to kind of take control of 10 percent of the
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Lecture transcriptions - Programming Paradigms - 2008 - Lesson 19 and more Lecture notes Programming Paradigms in PDF only on Docsity!

ProgrammingParadigms-Lecture

Instructor (Jerry Cain) :Hey, everyone. Welcome. I have a glorious set of handouts for you today. I have a midterm solution and I have the first two handouts and a stream of a few handouts I’m going to be giving you on our next paradigm and our next programming language. You’ll see that I do have the midterm solution in there. I actually distribute that, obviously, so you can check your answers, but also so that we’re somewhat transparent in how we actually grade these things.

Turns out that these things amount to like 25 percent of your grade, so I like you to know what criteria we’re using to consistently grade everybody so that if you see something that is so clearly wrong in terms of the way we graded it, you can confirm that by looking at the grading criteria and then coming to me if you have problems.

Usually when there’s a one-point error that’s not listed specifically on a criteria, that doesn’t mean I’m gonna give it back to you. It means that you made a mistake that we didn’t anticipate anybody making and we didn’t put it on the criteria. But if you have a total of five points taken off for a problem out of ten, and it’s not clear why you have that many points taken off, that’s a difference scenario, and so consult the answer key, and if there really is disparity, come and talk to me about it.

You should come and talk to me about it if you’re worried about there being a discrepancy because this thing does just end up counting a lot. So don’t feel shy about coming back and asking for clarity as to why we took points off that we did.

So what I want to do today is I want to introduce a new programming language, and I want to first illustrate a new paradigm, one that you’ve certainly not seen before unless you’ve coded in other classes at Stanford or coded prior to Stanford. We have spent a lot of time talking about these two paradigms, imperative. You’ll also hear me call it procedural.

They’re not necessarily the same paradigm, but the language we’re using to illustrate both of them is the same. We focus on C as the representative of those two paradigms. We also have the object oriented paradigm UC++, and even though we know that CSC++ ultimately translate to the same type of assembly language that you kind of think about the problem differently or we think about our solution to the problem differently when we take a pure C approach versus a pure C++ approach.

The reason this is called imperative or procedural is that they’re focused around the verbs that come up in the paragraph description of a solution. Think about what a main function looks like in a typical C program. You declare all your data, and then you make a series of call to these very high level functions like initialize, do it, terminate, and then return zero or something like that. Do you understand what I mean when I say that?

Okay, so the first thing you see associated with any C statement is usually or very often the name of a function that gets invoked to kind of take control of 10 percent of the

program or some small percent of the program just to get something done. As far as object oriented is concerned, you’re used to something like this. I’ll write the C equivalent in a second, but C++, over here you declare vector V.

You do something like vector new where you pass in the data and a few other parameters. You do things like vector insert ampersand of V, and vector sort of ampersand of V with additional arguments. Those aren’t prototypes. That just means I don’t feel like spelling out the rest of the call.

In C++, you declare a vector maybe of Nths called V, and you do something like V dot push back of 4 or maybe you do something like V dot erase of V dot begin to remove the front element. I know you haven’t dealt with that specifically. Don’t worry about the fact that you haven’t necessarily used all those methods, but clearly, in this exactly right here, you’re looking at the verbs first, okay.

It is oriented around the procedure, so I’ll just go ahead and say that it is procedure oriented whereas right here you declare this object up front. And the first thing that comes up in a statement of any particular – comes up in any one particular statement, usually the first thing you look at is the piece of data that’s being manipulated.

In this case, V is the data. It’s also called an object. Because this is up front, it looks like each statement is oriented around the object, which is why it’s called object oriented as opposed to procedurally oriented, okay. Does that make sense?

So you may think, “Well, I don’t understand how I could possibly program it in any different manner.” Well, even sequential versus concurrent programming, there’s a little bit of a paradigm shift. You have to think a little bit differently about the problems that are gonna come up when you program in a threading environment versus a non-threading environment, okay.

Usually when – this is a little bit of a caricature, but this is really how I feel. Whenever you’re coding up a normal program like programs one through four, you have this very linear way of thinking. You have a series of tasks you want to get through, and it’s almost like you’re inside the computer like typing things out one by one. But when you’re programming in Assignment 6, it’s at least not all of it has to do with execution logic. A lot of the hard stuff is like figuring out how all the threads are gonna interact.

And so you’re thinking about multiple things at a time. And I’m actually like standing up a little bit more because I actually think with the back of my head when I’m programming concurrently because I’m trying to imagine all of these different scenarios of thread interaction that I have to worry about that have nothing to do with code, right? I actually have to see all these little players in like some thought cloud and how they might be interacting and how race conditions might come into being.

And so concurrent programming and multithreading is it’s own paradigm that isn’t really tied to any one particular language. Object orientation isn’t tied to C++ any more than it’s

If I do something like this, then you know exactly what I mean when I write that down, okay. It turns out that the definition of G of X involves the definition of X where it takes this one parameter and splits it out into two parameters so it can call F. And then it incidentally adds eight to whatever is returned there. Does that make sense? Okay. And if I go so far as to do this, maybe it’s the case that H of X, Y, and Z is actually equal to F of X and Z times G of X plus Y, and that’s just the associations that are in place, okay.

Now this isn’t legal code. This is just math. What the functional paradigm approach does is it assumes you have lots of little helper functions that are interested in synthesizing one large result. So maybe it’s the case that I’m interested in the result of H where it gets a one, two, and a three.

And I happen to decompose it this way. I could actually inline the definitions of all of those things, and I could frame H in terms of just X and Y and Z, and not have any helper function calls whatsoever, right. But for reasons of decomposition, it’s usually easier to frame things in terms of helper functions, and that’s kind of what I’m doing right here.

What Scheme and what the functional paradigm tries to emphasize is that you give a collection of data to the master function that’s supposed to do everything for you. It does whatever is needed in place to synthesize the results, and that answer is returned via the return value, and that’s all you’re interested in, okay.

Maybe it’s the case when this is been one, one, and four. I have no idea what the numbers are. Maybe it returns 96. I have no idea. And I’m only interested in the 96 because that is the material product that I’m trying to get out of the program.

What a functional paradigm approach would take is that it would just say, and it would associate something like, how do I want to say this? It would do this, and it would associate it with F of XZ times G of X plus Y, or it might actually prefer to write it this way, is equal to X cubed plus Y squared plus 7 times F of X plus Y, X plus Y plus 1. And that’s plus 8, okay.

But you would actually write it this way and really expect F and G as functions to themselves return values that contribute to the synthesis of a larger result. Does that make sense to people? Okay, question in the back.

Student: Why isn’t the Y squared replaced with Z?

Instructor (Jerry Cain) :I just – I didn’t go that far. Where did I do? I’m sorry. I just messed up.

Student: Yeah.

Instructor (Jerry Cain) :Yeah, sorry. Thanks. So rather than actually trying to do this in terms of pure math, let me just give you an example of what a Scheme function looks

like. I’m not even gonna try and explain what the syntax is. You’re just gonna have to intuit what it probably does as a function.

I’ll get to the pure syntax later on, but you can kind of gander what that as a keyword is probably gonna do. And I’m just going to do this. Let’s say Celsius to Fahrenheit. It takes the temp, okay. And I just do this. I’m going from something at temperature, so what I want to do is I want to multiply it by 1.8 and add 32. This is how you would do this, okay.

Now you’re not sure what the syntax is, you can kind of see that the right numbers come up in the conversion of Celsius to Fahrenheit, okay. So I want to scale 0 degrees or 100 degree by 1.8, okay, and then actually add 32 to it. And that’s how I got 32 or 212 out of it.

So I’ll go over syntax later, but what’s really happening here is that in a Scheme environment, which is an example of a functional language, it associates this is a symbol, and the actual dash and the greater than sign forming an arrow, that’s actually a legal part of a token in Scheme. They want you to be as expressive as you could possibly be using the full alphabetic or a full [inaudible] set, pretty much the full [inaudible] set to name all of your symbols.

It’s framed in terms of this one parameter, and as a function call, it’s equated with this expression right here where whatever value of tenth is supplied replaces that right there. So if I go ahead and I type this in to the shell, to the actual Scheme environment, it’s supposed to somehow pop out a 212, and it succeeds in doing that because it takes this as a recipe, stops – there’s a template on the tenth variable, actually figures out what it would evaluate to if ten became a value – came down to 100.

As an expression, it evaluates the 212. And so this as an expression is equated with this expression, and it comes back with a 212, okay. Does that make sense to people? Okay. Don’t worry about the mechanics. Just think about the actual description of what a functional language is trying to do here.

Now let me actually just describe what the Scheme environment is like. We’re using an open source product that I happen to – well, I didn’t work on it, but I used it quite a bit a few years ago for a consulting job. It is a product called Kawaa. And I don’t want to say that it’s standard, but it happens to work fairly well and I just wanted to use it and I did because it’s free and it’s open source and I can just install it in 107 space, and then nobody has to – I don’t have to bother anybody in trying to get support for it.

When you launch this thing called Kawaa, you more or less launch an environment that functions much like the shell where you type LS and make – and CD and things like that. It just happens to not speak the batch language or the TSCH language or TCSH language. It’s actually a little bit more elaborate that something like Bash or SH or something like that where you actually go ahead and you get a prompt, and it always expects you to type in something that can be evaluated.

housed by simple [inaudible] before are now – those possessions are now occupied by things that are themselves lists that should be recursively evaluated, okay.

So whereas this evaluated to one and a two and a three much like this evaluated to a four and a hello and a false constant. These evaluate to themselves, and then they participate in the larger function evaluation that uses plus to kind of guide the computation, okay. And not surprising, you’d get a six there. That four would evaluate to a four. That four would evaluate to a four. This as an expression would evaluate to an eight.

Five would evaluate to a five. Five would evaluate to a five. This entire thing would evaluate to a ten. The overall thing would evaluate to an 80, and that’s how you get an

  1. It’s not the math that’s interesting. It’s actually the manner in which things get done, I think that’s the most informative here, okay.

So I’m willing to buy that even these are function calls, I don’t like using the word function call, okay. I mean I think function call is fine. I just don’t like to speak of the return value of a function call because that’s a very imperative procedural way of thinking about it. I like to think of this as evaluating to a 6 or an 80. Does that make sense to people? Okay. Okay.

So there’s that. It turns out that plus and asterisk are built-ins. All the mathematical operators you expect to be built-ins are, in fact, built-ins. If I’m curious as to whether or not the number four is greater than the number two, I can ask. Isn’t the case that four and two ordered that way actually to respect that greater than sign as a function. They still didn’t return a number of a string. It returns a Boolean.

This would come back with something like that right there, okay. If I did this, less than – is ten less than five? That would come back with a false, okay. If I – this is just the prompt. That doesn’t agree with that sign.

If I did something like this, it would assemble things in the way you’d expect, okay. It actually even does short circuit evaluation. So this one right here would be evaluated. This four as an expression evaluates to a four. This two evaluates to a two. This over all thing evaluates to a two. This ten evaluates to a ten. Five evaluates to a five. This overall thing fails, and it’s at this point that the conjunction that you would just expect to be in place with ends would overall evaluate to a false, okay.

Now I am assuming you’re gleaning the fact that the zero parameter or the zero position in a list the way I’m using them right here always identifies some form of functionality. There’s functionality associated with this symbol right here, okay, and it knows how to take these two arguments and produce other true or false.

The same thing can be said right there and right there, okay. But the actual symbols that are attached Old Testament functions always occupy the zero place. It has this very prefix oriented way of dealing with function calls, okay. Does that make sense?

One of the most complicated things about Assignment 7, no joke, is actually getting the parenthesis right. You’re so used to typing it at the end of a function, and then typing an open paren after it that that’s what you’ll type out, and then there’s so many parenthesis surrounding you anyway when you’re typing this stuff up that it’s very easy to miss it.

So you have to be very – and just balancing the parenthesis isn’t enough. You have to make sure that you get into this habit of just opening up a parenthesis, thinking like you have this entire list of things that help express some kind of function call, and just know that that’s the type of thing that’s really hard to get right when you write your very first function in Scheme, okay.

Now there is – there are a couple of things about lists that I want to go over before I start defining my own functions. I told you that Lisp, even though we’re doing it with Scheme, we’re really doing it with Lisp. It’s called List processing for a reason. Everything, including function calls, come in list form. The only exceptions are things like for’s and hello’s and things like that, the atoms of the data types.

But normally anything interesting is bundled in a list. We don’t really have – they do have structs in Scheme. They do have classes in our version of Scheme. We’re gonna pretend like those just don’t exist. Everything that’s an aggregate data type is just gonna be packaged as a list. And we’re gonna know that the 0th item in the list stores like the name. And the 4th – I’m sorry, the 1th slot in the list stores the GPA or the address or the phone number or something like that, okay.

What I want to do is I want to go over a few fundamental operations that are technically functions in Scheme that allow you to dissect and build up new lists. You’re not gonna always want to return a 212 or a hello or an 80. A lot of times you’re gonna want to return a list of information, or a list of lists, or a list of list of lists, or whatever happens to be needed in order to present the overall result to you.

There is a function called CAR. There’s another one called CDUR, and there’s one calls CONS. I’ll go over why they’re called this is a second. It’s not really important. It’s sort of interesting, but then it stops becoming interesting after a few seconds.

Car and CDUR are basically list dissectors, okay. If at the prompt, I type in CAR, I’ll explain what the quote is in a second, one, two, three, four, five. This returns the number one, okay. If you just think about these things as link lists, they kind of are link lists behind the scenes. Car is associated with the data that’s housed in the very first node, okay. It always is the evaluation of whatever is occupying the zero slot of a list, which is why this one comes back, okay.

If I ask for the CDUR of the very same list, it basically covers everything that the CAR does not, so it returns the rest, which in this case would be two, three, four, five, okay. Does that make sense to people?

data, okay. We might just want to present our data as these bland lists, okay, and package them in a way that we just deal with consistently, okay.

So CAR is like synonymous with first. In fact, some dialects of Lisp actually first defined as a function. Coulder is synonymous with rest. It’s like whatever you get by doing – following the next pointer behind the scenes, okay, whatever list you arrive at after the first element.

And you can use these in any clever way you want to to get to the third element or the last element or this element right here expresses a list, okay. Whatever you need to do to package – to get to your answers. You can package CAR and CDUR in some – whatever clever way you want to, okay.

Now why are they called CAR and CDUR? It’s really not a very interesting reason, but they – I mean it’s kind of interesting. There was a – the original implementation of either Scheme or Lisp, I’m not sure, was just on an architecture that had two exposed registers. One was called the address register, emphasis on the A, and one was called the data register.

And the CAR and the CDUR that were most recently dealt with the addresses that implemented them were stored in the address register and the data register, okay, and that’s where the AR and the DUR come from, address register and data register. Does that make sense? I don’t know where the C came from, something related to the letter C, I’m sure. I just don’t know, okay.

So that’s why they’re there. Our system doesn’t define any synonyms to these. Some versions of the language define first, second, third, fourth, all the way up to tenth I’ve seek, okay. But ours doesn’t, so you really have to deal with the raw CAR and CDUR calls, okay.

These two functions take lists and break them down into their constituent parts. CONS is kind of the opposite. If I, at the prompt, do this, CONS, and I say one, which evaluates to itself on the list two, three, four, five, CONS is short for construct. It actually synthesizes a new list for you, and it would return this. So CONS is always supposed to be – take two arguments.

The first argument can pretty much be anything. The second one is supposed to be a list because what more or less happens is that it takes this element right here. It pulls this – it effectively pulls this parenthesis in like it’s on a spring or something and drops the one in front. And whatever you get as a result of that is your resulting list. Does that make sense to people? Yes, no? Yeah.

Student: [Inaudible] two, three, then [inaudible] four five? So you can put one in between three and four?

Instructor (Jerry Cain) :You could, but you – I’m sorry. So tell me what you want me to write. You want a CONS call right up front? And then what?

Student: [Inaudible].

Instructor (Jerry Cain) :Write it like that?

Student: Yes. [Inaudible].

Instructor (Jerry Cain) :Four, five?

Student: Yeah.

Instructor (Jerry Cain) :Yeah. I mean they’re – actually I know what you’re trying to do now. That would not work. CONS really has to have two arguments, and the second one has to be a list, okay. If you wanted to do – let me just – in two minutes, I’ll revisit this example and at least just show you the code as to how you would assemble this.

What I do want to emphasize – let me erase this since it is syntactically a little off. I want to emphasize the fact that it’s very literal about how it takes the first piece of data and puts it into the front of the list. If I do this, CONS of one, two, three, and I try to CONS it onto the front before five, I actually will get from that another list where four, five is its CDUR, okay, but I will get this out, okay.

It’s very literal in the fact that it takes this one element, which happens to be a list one, two, three, and it kind of prepends it to the front of everything that resides in the second list. So this emphasizes a point. I haven’t formally said this yet, but lists in Scheme or any dialect of Lisp for that matter, can be heterogeneous, okay.

Right now I’ve – almost all the lists I’ve done up to this point except for one of them, I guess I erased it. All of the lists have been homogeneous in that they’ve all stored integers or they’ve all stored Booleans or strings or something like that. That isn’t a requirement.

So there are a couple of features so far about this that I think are pretty interesting. There’s very little type-checking going on, okay. There’s a little bit, but there’s not nearly as much of a compile time element to Scheme as there is in CSC++, okay. It just lets you type in whatever you type in, and it’s only as it evaluates things that if it sees a typed mismatch, because you try to say add a float or a double to a string, that it’ll say, “You know what? I can’t do that, okay.”

But it’s actually at run time when it does the required type analysis to figure out whether or not something will work out. Does that make sense? Okay.

As far as what you wanted to do, there is a way to do it. I’ll just introduce it because I can introduce a function pretty quickly. If I really wanted the list one, two, three, four, five

for forming all lists. When I talk about how CONS is implemented, you’ll understand that the empty list is kind of like the base case of a recursive call.

I should say that if you do this, that’s a no no. Now some implementations will just – whenever you try to take the CDUR of an empty list and try and remove a car that isn’t there, that some implementations will just return the empty list for you. I don’t want you to program that way.

I want you to assume that either a CAR or a CDUR levied against the empty list is actually an error, okay. And I actually am forgetting right now was Kawaa does because I never try to exploit the feature if it is one. I just assume that this is gonna be [inaudible] in there, okay. So no, it would print no, don’t do that, okay. Does that make sense?

So I dealt with every – more or less I’ve dealt with all data that’s been a constant or a list constant or something like that. That’s not the way it is. You really do define functions in Scheme as well, or else you wouldn’t be able to build scalable systems that can be parameterized in terms of arbitrary datasets.

So I already gave you an example of one function over there, but let me start even a little bit easier. If you go ahead and use the define keyword, define has its own purpose. It’s occupying the slot where you normally see pluses or times or divisions, okay. What happens next, if I just do this, add, okay, does that make sense? And I just pass an X and Y, there’s no comma separation between the arguments. The space is the delineator.

And I equate this functionality like that, okay. Then you type that in. It actually comes back and says, “Oh, I just defined add. Thank you very much, okay.” It actually prints out add, not because it evaluated to add, just because it’s the define keyword. It just wanted to remind you what function just got defined.

Now this is the very first example, and this is an obscure point, but I kind of want to revisit this a few times later on. This is the first example of any kind of Scheme expression we’ve dealt with so far that has some side effect associated with it. And the way you hear that, you may be like, “Well, why is that interesting?”

This purely synthetic approach where it takes the data and it synthesizes a return values so that the overall expression evaluates to it, it does all of that without printing to the screen or updating memory in any way that we know about. You’re not passing around raw pointers anywhere. Do you understand what I mean when I say that? Okay.

Even the lists themselves are being synthesized on your behalf. If you were trying to do the equivalent things in CSC++ you would have to declare your list data structures, okay, or worse yet, you’d have to actually define a node type, and you’d actually have to thread together link lists using Malak or New and free or delete or whatever you have to do. You’d have to manage the memory allocation by yourself.

Scheme is so much more of a higher-level language and it’s smaller and it tries to do less that it’s easier for it to do – take what it does and do it very, very well. The list, as opposed to C or even technically C++, the list is a built-in data structure that’s core to the language. So in the same way that we breed string constants and integer constants to C, you can actually express list constants. I don’t have any up here. Yeah, I do.

This right here, this knows how to build a data structure to represent the list, one, two, three, four, five behind the scenes, okay. You don’t have to manage any of that. In purely functional languages, and we’re gonna strive for this in the [inaudible] scheme we’re gonna learn, you try to program without side effect, okay.

Only to the extent necessary do you update variables by reference. I’ve certainly not done any of that yet, okay. I’ve always just relied on what it evaluated to. Technically, there’s a side effect associated with this right here in that in the global name space it associates this add keyword, okay, to be associated with this functionality right here, so that from this point on add, the way I’ve defined it, it actually is a built-in. It behaves more or less like a built-in just like CONS and CAR and CDUR and list and append all are, okay.

They really are peers. It’s almost as if there’s a map, a global map of symbols mapping to actual functions, okay, where the functions themselves are expressed as lists, and that map is prepopulated with code for CAR and CDUR and CONS. And then you can add to it programmatically by associating this keyword with this list right here, which knows how to behave like a function. Does that make sense to people?

Okay, so when I do this, add 10 and 7, it comes back with a 17 because it somehow knew how to take this 10 and the 7, crawl this list right here to figure out how to deal with the 10 and the 7 that were passed in, and whatever it evaluates to is what add evaluates to. So it’s like you equate this symbol parameterized by these two arguments with this Scheme expression, okay. Yep.

Student: Does case matter? Like why does it give you add?

Instructor (Jerry Cain) :That’s just – actually, I shouldn’t have emphasized that. Case does matter when you’re typing these things out yourself. For some reason, and this may not even be the case with Kawaa, I just remember the Scheme interpreter I used in this class forever capitalized everything for reasons that weren’t clear to me.

But you should be sensitive to case, but just because I print something out in uppercase doesn’t mean anything, okay. I like de-emphasize this. Pretend it’s just – don’t even worry about it, okay. Yep.

Student: Can you use ellipses and say like add XY and –

Instructor (Jerry Cain) :Yeah, you actually – I’ll talk about that the last day of the Scheme segment when I talk about these equivalent features to CSC++. You don’t do

hyphen, so it’s one token. And I give you this right here. You know it’s supposed to be ten, I think, yeah, ten.

And the way that the Scheme functionality is gonna realize this is it’s gonna say, “Oh, I have a non-empty list. That means I’m gonna add the CAR to whatever I get by recursively applying the same function to the CDUR.” So the ten isn’t so much a ten as it is a one plus a nine. The nine isn’t so much a nine as it is a two plus a seven. Do you understand what I mean when I say that?

Okay, here’s the implementation of this function. Define sum of – oops, sorry I did it, sum of – and I’m just gonna call it – I don’t want to call it list. I don’t want to get in the habit of naming my variables the same as built-in functions. So I’ll call it num list just like that, okay. Does that make sense?

And what I’m gonna do is I’m gonna employ a couple of built-in tests. If it’s the case that null question mark num list, then return zero. The if is exactly what you’d expect. It needs three parts to follow it, a test, an if portion, and an else portion. The else portion is technically optional, but I don’t want you to pretend – I want you to pretend it’s not optional.

Null actually comes back with true if and only if this thing evaluates the empty list. If it is empty, then trivially it’s the case of all the numbers in this empty list add up to zero. Otherwise, what I want to do is I want to equate the original sum of call with the value that you get by levying plus against the CAR of the num list and the call to sum of the CDUR of the num list, okay. The headache really is just matching all the parenthesis, okay. But conceptually, this is the recur one that’s in place to get this done.

Now you don’t have to implement this recursively, but we are at the moment, okay. And we’re always gonna opt for recursion over iteration in the Scheme segment of the course just to emphasize the functional aspects of the language. Do you understand how this is working?

It is just basic recursion, which is with the new syntax, okay. Synthesize the recursive result, get the nine back, add the one to it, and that’s what this overall thing needs to evaluate to. Does that make sense? Okay.

So you have that. As long as I feed it one, two, three, four, it doesn’t have a problem. If I feed it one, two, three, and then four is a list, it’ll actually succeed in making three recursive calls, but only when it tries to levy a plus of the four is an empty list against a zero that it’ll actually have problems, okay.

So it just does on an as needed basis the type of type analysis that is needed to confirm that the addition can be done between the CAR of the list and whatever was returned recursively, okay. Make sense? Okay.

I want to write a lot more of these come – today’s Wednesday, yeah, come Friday. I’ll write a lot more of these things. And then I’ll start talking about language constructs that are equivalent to the types of things we’ve seen in our C++ work and also in Assignment 3 and 4. Okay, have a good night.

[End of Audio]

Duration: 52 minutes