1 00:00:00,077 --> 00:00:06,140 [BLANK_AUDIO]. 2 00:00:06,140 --> 00:00:10,170 In this first video on abstraction, we're going to start by going all the way back 3 00:00:10,170 --> 00:00:13,766 to the beginning of the course, to remember the original problem that 4 00:00:13,766 --> 00:00:19,465 functions with parameters solved for us. Then, we're going to see that we can 5 00:00:19,465 --> 00:00:22,942 solve a similar problem in our most recent functions, functions that are 6 00:00:22,942 --> 00:00:28,200 very, very similar in large part because they're based on the same template. 7 00:00:29,480 --> 00:00:32,330 We're going to be able to pull out the common part of the function into an 8 00:00:32,330 --> 00:00:35,580 abstract function, and that's going to make the functions we started writing in 9 00:00:35,580 --> 00:00:40,980 the first place much, much shorter. Okay. 10 00:00:40,980 --> 00:00:44,068 This is going to be fun. I'm in Parameterization Starter and I'm 11 00:00:44,068 --> 00:00:48,155 going to go all the way back to the very beginning of the course, to some of the 12 00:00:48,155 --> 00:00:52,850 very first kinds of expressions that we wrote. 13 00:00:52,850 --> 00:00:56,535 Back at the beginning, we learned how to write an expression like this first one, 14 00:00:56,535 --> 00:01:00,319 which computes the area of a circle of diameter four. 15 00:01:02,010 --> 00:01:05,110 And of course, if you do that for the area of a circle of diameter 4, you could 16 00:01:05,110 --> 00:01:08,520 also do it for the area of a circle of diameter 6. 17 00:01:10,120 --> 00:01:13,406 And then, pretty much immediately, we started to think, wow, this is going to 18 00:01:13,406 --> 00:01:17,837 be cumbersome if every time we want to do something slightly different. 19 00:01:17,837 --> 00:01:24,560 We have to write a whole new expression. And then we learned about functions. 20 00:01:24,560 --> 00:01:28,838 And the idea that because we have two expressions here that vary only in one 21 00:01:28,838 --> 00:01:33,323 position, they vary only in the position where the 4 and the 6 appears, we could 22 00:01:33,323 --> 00:01:41,412 do the following trick. You could take one of the expressions. 23 00:01:41,412 --> 00:01:47,050 Put it up here. Wrap a function definition around it. 24 00:01:47,050 --> 00:01:51,151 Let's call it area. Pick a parameter name for the varying 25 00:01:51,151 --> 00:01:53,379 position. Let's call it r. 26 00:01:55,610 --> 00:02:01,350 [INAUDIBLE] the indentation always, and then replace the specific thing that's in 27 00:02:01,350 --> 00:02:07,516 the varying position with r. And then, these expressions can be 28 00:02:07,516 --> 00:02:13,330 replaced with the more compact, area 4, in this case. 29 00:02:13,330 --> 00:02:17,958 I'll just comment out the other one, and area 6, in that case. 30 00:02:17,958 --> 00:02:23,421 [SOUND] We run it, we get the same values as before. 31 00:02:23,421 --> 00:02:28,221 And we say that this function is more abstract that the specific expressions 32 00:02:28,221 --> 00:02:33,600 because it's more general than the specific expressions. 33 00:02:33,600 --> 00:02:37,020 This does the area of a circle of diameter 4. 34 00:02:37,020 --> 00:02:41,370 This does one for 6. Those are very specific. 35 00:02:41,370 --> 00:02:44,910 This does the area of a circle with any given diameter. 36 00:02:44,910 --> 00:02:50,560 That's more general, or as I'm saying we call that more abstract. 37 00:02:50,560 --> 00:02:54,175 That was Week 1. Later, we learned how to write functions 38 00:02:54,175 --> 00:02:58,530 that operate on lists, and lots of other functions. 39 00:02:58,530 --> 00:03:02,674 And at one point, we were writing these two functions. 40 00:03:02,674 --> 00:03:07,324 The function that is going to take a list of Quidditch teams and see whether it 41 00:03:07,324 --> 00:03:11,390 contains UBC. A function that is going to take a list 42 00:03:11,390 --> 00:03:15,230 of Quidditch teams and see whether it contains McGill. 43 00:03:15,230 --> 00:03:19,744 And, of course, these functions because of the way we design them based on 44 00:03:19,744 --> 00:03:25,852 templates are very, very similar. They only differ in one position. 45 00:03:25,852 --> 00:03:31,078 They differ in their names of course, but they really only differ in one position 46 00:03:31,078 --> 00:03:39,880 where UBC and McGill are. So we can do the same thing. 47 00:03:39,880 --> 00:03:43,138 I'm going to take one of them. And in this case, I'm going to put it 48 00:03:43,138 --> 00:03:47,018 below instead of above, that's just a detail. 49 00:03:47,018 --> 00:03:50,385 [BLANK_AUDIO] I'm going to think of a more general name. 50 00:03:50,385 --> 00:03:54,550 There already is a function here. It's not just an expression, so I'm not 51 00:03:54,550 --> 00:03:58,362 going to wrap a function around it. I'm going to think of a more general 52 00:03:58,362 --> 00:04:02,714 name. I'll just call it contains. 53 00:04:02,714 --> 00:04:07,178 There's one varying position, the position that holds UBC and McGill, so I 54 00:04:07,178 --> 00:04:11,828 need a parameter for that position. I'll call it s. 55 00:04:11,828 --> 00:04:19,110 Since I renamed the function I have to rename the recursive call. 56 00:04:19,110 --> 00:04:25,187 I have to pass the parameter in the recursive call and, and, I have to use 57 00:04:25,187 --> 00:04:33,430 the parameter in the varying position. All I did there was exactly the same 58 00:04:33,430 --> 00:04:38,328 thing I did for area. And now, of course, I can go back to 59 00:04:38,328 --> 00:04:44,441 contains-ubc and contains-mcgill. And I can replace their entire body with 60 00:04:44,441 --> 00:04:51,595 just a call to this contains function. [SOUND] UBC is what's in the varying 61 00:04:51,595 --> 00:05:02,522 position. And all of this can go away. 62 00:05:02,522 --> 00:05:14,610 And I can do the same thing here. And all of this can go away. 63 00:05:14,610 --> 00:05:20,242 And when I test it, should work, those tests are passing. 64 00:05:20,242 --> 00:05:27,732 Now, I didn't really, this isn't really a complete function design, it doesn't have 65 00:05:27,732 --> 00:05:35,880 a signature, it doesn't have a purpose, it doesn't have tests. 66 00:05:35,880 --> 00:05:40,158 Because we started from working code and work systematically this way, we ended up 67 00:05:40,158 --> 00:05:43,884 with working code. This is a different way of arriving at a 68 00:05:43,884 --> 00:05:47,450 function design. A little later, when I show you how to 69 00:05:47,450 --> 00:05:51,250 get the signature purpose, and, and tests. 70 00:05:51,250 --> 00:05:54,748 What you're going to see is that this way of arriving at a working function design, 71 00:05:54,748 --> 00:05:58,150 which is called abstraction from, from examples. 72 00:05:58,150 --> 00:06:02,440 You get the function definition first, then you get the tests, then you get the 73 00:06:02,440 --> 00:06:08,100 purpose, then you get the signature. It's the opposite order. 74 00:06:08,100 --> 00:06:09,980 So, we've got the function definition for now. 75 00:06:11,260 --> 00:06:16,992 Let's go do another one. Here's two functions, squares and square 76 00:06:16,992 --> 00:06:21,050 roots. They're kind of silly. 77 00:06:21,050 --> 00:06:24,039 You might not want these two functions, but what squares does is it takes a 78 00:06:24,039 --> 00:06:28,330 listed number, and it produces a list of the squares of those numbers. 79 00:06:28,330 --> 00:06:33,910 So if you call it with (list 3 4), you get (list 9 16). 80 00:06:33,910 --> 00:06:36,410 And what square roots does is it kind of goes the other way. 81 00:06:36,410 --> 00:06:43,300 If you call it with (list 9 16), you get (list 3 4). 82 00:06:43,300 --> 00:06:45,080 Now let's look at these two functions closely. 83 00:06:45,080 --> 00:06:52,101 They're very, very similar. They differ in their names, but aside 84 00:06:52,101 --> 00:06:56,680 from that they only differ in one position. 85 00:06:56,680 --> 00:07:01,390 This position here. This one calls square in this position. 86 00:07:01,390 --> 00:07:03,443 And this one calls square root in this position. 87 00:07:03,443 --> 00:07:11,100 So let's try the same thing again. We'll make a copy of one of the two. 88 00:07:11,100 --> 00:07:18,460 We'll put it down here. Now we need to think of a more general 89 00:07:18,460 --> 00:07:23,710 name. Now for historical and mathematical and 90 00:07:23,710 --> 00:07:28,700 wonderful reasons the name I'm going to choose is map2. 91 00:07:28,700 --> 00:07:31,610 We never would've chosen map2. I know that. 92 00:07:31,610 --> 00:07:35,314 I'm choosing map2. The reason I'm calling this map2 is that 93 00:07:35,314 --> 00:07:40,228 later we'll see that ISL. Already has a built in function that does 94 00:07:40,228 --> 00:07:44,280 this. And the name of that function is map. 95 00:07:44,280 --> 00:07:46,260 And I'm not allowed to redefine a function name. 96 00:07:46,260 --> 00:07:51,980 So I'm using map 2 here. Now, I need a name for this parameter. 97 00:07:51,980 --> 00:08:00,559 For the thing that goes there or there. What's a good name for the general thing 98 00:08:00,559 --> 00:08:04,240 that those are. When it was two radii, we used r. 99 00:08:05,830 --> 00:08:09,990 When it was two strings, we used s. These are two functions. 100 00:08:09,990 --> 00:08:14,430 I'll just use fn. We could use anything you want of course. 101 00:08:14,430 --> 00:08:16,190 A parameter name can be anything you want. 102 00:08:16,190 --> 00:08:21,220 It could be strawberry. But I'm using fn. 103 00:08:21,220 --> 00:08:26,621 Now, let's see. I also have to rename the recursion, and 104 00:08:26,621 --> 00:08:34,434 I have to pass the parameter in the recursion. 105 00:08:34,434 --> 00:08:41,158 And I have to use the parameter at the point of variance. 106 00:08:41,158 --> 00:08:53,065 And then I have to take these two and replace their bodies with map2, and then 107 00:08:53,065 --> 00:09:10,619 this one is square lon. And here it's map2 and this one is sqrt 108 00:09:10,619 --> 00:09:17,153 lon. Let's try that. 109 00:09:17,153 --> 00:09:23,524 The tests passed. Some of you, especially the one's who've 110 00:09:23,524 --> 00:09:27,291 programmed in other languages. Might be going, wait a minute. 111 00:09:27,291 --> 00:09:31,261 What happened? gee. 112 00:09:31,261 --> 00:09:36,320 Here we've got a function or a function name, at least. 113 00:09:36,320 --> 00:09:42,133 Sqr and sqrt, being passed as a parameter. 114 00:09:42,133 --> 00:09:46,470 That's right. Map2 is what's called a higher order 115 00:09:46,470 --> 00:09:49,647 function. And what it means to be a higher order 116 00:09:49,647 --> 00:09:54,476 function is that it can consume other functions as its arguments. 117 00:09:54,476 --> 00:09:57,550 So that's exactly what we're doing here in squares. 118 00:09:57,550 --> 00:10:04,267 We're calling map two with two arguments. One is name the function square, and the 119 00:10:04,267 --> 00:10:09,641 other is list the numbers And square roots does a similar thing. 120 00:10:09,641 --> 00:10:14,889 The great thing in DSL ISL, the teaching languages, is that the way of writing 121 00:10:14,889 --> 00:10:21,201 this, the notation that we use to write this, is very simple. 122 00:10:21,201 --> 00:10:25,328 You can achieve something like this in almost every programming language. 123 00:10:25,328 --> 00:10:29,297 [INAUDIBLE] But there's almost no other programming language in which the 124 00:10:29,297 --> 00:10:33,108 notation for doing it is as simple as it is here. 125 00:10:33,108 --> 00:10:36,820 That's the first part of the process for producing an abstract function from 126 00:10:36,820 --> 00:10:40,645 examples. In the next video, I'm going to continue 127 00:10:40,645 --> 00:10:45,085 with how you get Signature, purpose and tests. 128 00:10:45,085 --> 00:10:49,670 But this video, we're just going to work on the actual function definition. 129 00:10:49,670 --> 00:10:54,001 I've got one more example here of 2 functions, positive only and negative 130 00:10:54,001 --> 00:10:57,995 only. And I'd like you to go ahead and do just 131 00:10:57,995 --> 00:11:02,230 what I've done here, with those two examples. 132 00:11:02,230 --> 00:11:07,315 And I want you to call your abstract function, filter two, that's filter two. 133 00:11:07,315 --> 00:11:11,310 Go ahead and do that now and I'll see you in the next video.