1 00:00:05,690 --> 00:00:09,228 Now that you know how define struct works, we're going to look at how to use 2 00:00:09,228 --> 00:00:12,882 the how to design data recipe and the data driven templates recipe to design 3 00:00:12,882 --> 00:00:18,310 compound data definitions based on define struct. 4 00:00:18,310 --> 00:00:20,835 Here's a sample problem. The problem asks us to design a data 5 00:00:20,835 --> 00:00:24,410 definition to represent hockey players, including both their first and last 6 00:00:24,410 --> 00:00:27,652 names. So, I'm working with the same example we 7 00:00:27,652 --> 00:00:32,990 used to learn about define struct, but now we're doing full data definition. 8 00:00:32,990 --> 00:00:37,082 So, the key thing in this example is that when I read it, it has this property that 9 00:00:37,082 --> 00:00:41,298 it has two or more values that naturally fit together, including both their first 10 00:00:41,298 --> 00:00:46,076 and last names. Both the first and last name naturally 11 00:00:46,076 --> 00:00:51,355 fit together into a single hockey player. That's the key information property that 12 00:00:51,355 --> 00:00:55,360 we're looking for to decide if we want to have a compound data definition. 13 00:00:55,360 --> 00:00:58,944 If we look at the Design Recipes page and then go to the How to Design Data page, 14 00:00:58,944 --> 00:01:02,080 and we go to our table about formal information and what kind of data 15 00:01:02,080 --> 00:01:08,091 definition to use. It says that when there are two or more 16 00:01:08,091 --> 00:01:14,604 values that naturally belong together, we want to use a compound data definition. 17 00:01:14,604 --> 00:01:17,850 And here it talks about a data definition. 18 00:01:17,850 --> 00:01:20,580 As we did when we learned the other data definitions. 19 00:01:20,580 --> 00:01:24,310 You should go review this on your own. I'll just work through it. 20 00:01:24,310 --> 00:01:27,070 So, the first thing in a compound data definition is there's going to be a 21 00:01:27,070 --> 00:01:30,014 structure that we're going to use to pull the multiple values together into a 22 00:01:30,014 --> 00:01:37,084 single value. So, we'll say define-struct player. 23 00:01:37,084 --> 00:01:41,885 And then the list of field names, we'll call them first name and last name. 24 00:01:41,885 --> 00:01:48,997 So now, I have a mechanism for doing it, and I'm going to say player is 25 00:01:48,997 --> 00:01:58,330 make-player string string. And what is this doing? 26 00:01:58,330 --> 00:02:02,070 So, what this is doing is it's saying something is a player if it's a 27 00:02:02,070 --> 00:02:08,600 make-player, and the first field is a string, and the second field is a string. 28 00:02:08,600 --> 00:02:19,520 And we'll interpret it this way make-player fn ln is a hockey player with 29 00:02:19,520 --> 00:02:36,500 first name, fn, last name, ln or if you wanted to, you could write it this way. 30 00:02:36,500 --> 00:02:45,450 You could say, fn is the first name, ln is the last name. 31 00:02:45,450 --> 00:02:48,770 You can write this interpretation a number of different ways. 32 00:02:48,770 --> 00:02:54,722 The key thing is you have to tell me how to interpret a piece of data of this form 33 00:02:54,722 --> 00:03:00,852 so that if I happen to be reading the program. 34 00:03:00,852 --> 00:03:06,711 And I see make-player, I know that the first name is Bobby and the last name is 35 00:03:06,711 --> 00:03:10,420 Orr. So, there's my interpretation, here's an 36 00:03:10,420 --> 00:03:14,803 example. We'll stick it right here we've already 37 00:03:14,803 --> 00:03:21,602 got one. Now again, examples for simple compound 38 00:03:21,602 --> 00:03:33,970 data definitions, you don't really need very many of em in order to be clear. 39 00:03:33,970 --> 00:03:36,140 One example would probably be good enough here. 40 00:03:37,270 --> 00:03:40,246 But starting soon, we're going to see that as data definitions become more 41 00:03:40,246 --> 00:03:44,279 complicated, you start to have more examples really to make it clear. 42 00:03:44,279 --> 00:03:48,170 So let's see, I've got the structure definition. 43 00:03:48,170 --> 00:03:50,930 I've got the type comment. I've got the interpretation. 44 00:03:50,930 --> 00:03:55,590 I've got some examples. Now, I need a template. 45 00:03:55,590 --> 00:04:07,783 So, we'll say define fun for player, P. Something's going to go there. 46 00:04:07,783 --> 00:04:15,270 Template rules used. So now, I'll go over, back over to the 47 00:04:15,270 --> 00:04:20,619 design recipes and I'll go to the data driven templates recipe. 48 00:04:23,040 --> 00:04:29,100 And, if the type of data is compound, then what I'm supposed to use for the 49 00:04:29,100 --> 00:04:34,690 body is all the selectors. So I'm going to have dot, dot, dot and 50 00:04:34,690 --> 00:04:39,822 then all of the selectors. So jumping back over here to racket, it 51 00:04:39,822 --> 00:04:47,652 means in this case I would write dot, dot, dot, player fn p, player ln p. 52 00:04:47,652 --> 00:04:57,790 And the rule is, which rules was that? It's the compound data rule. 53 00:04:57,790 --> 00:05:01,658 And so, we're going to say the compound rule two fields. 54 00:05:01,658 --> 00:05:08,442 Now, over here, there's another detail that says consider the result type of 55 00:05:08,442 --> 00:05:13,262 each selector. So, what I'm going to do here is I'm 56 00:05:13,262 --> 00:05:17,070 actually going to go and write this like this. 57 00:05:17,070 --> 00:05:23,380 And I'm going to put a note here that says well what is player fn of p produce? 58 00:05:23,380 --> 00:05:27,995 Well, if this make player is actually a player, then it's first, first field is 59 00:05:27,995 --> 00:05:31,822 of type string. So that will be a string. 60 00:05:31,822 --> 00:05:39,800 And what will ln be? Well, it will also be a string. 61 00:05:39,800 --> 00:05:42,740 Let's talk about this template for just a second. 62 00:05:42,740 --> 00:05:46,692 What does this template say? Well, remember the fundamental job of a 63 00:05:46,692 --> 00:05:50,532 template is to tell you what do you have to work with, and what's the basic 64 00:05:50,532 --> 00:05:55,556 structure of the function. And what this template is doing is it's 65 00:05:55,556 --> 00:06:00,595 saying, listen if you write a function that consumes a player as an argument. 66 00:06:00,595 --> 00:06:04,485 Then, what it has to work with is all of the fields of the player. 67 00:06:04,485 --> 00:06:09,026 In this case, fn and ln. So basically, the fundamental shape of 68 00:06:09,026 --> 00:06:13,056 the function is take the player apart into its constituent field values and do 69 00:06:13,056 --> 00:06:17,455 something with them. Dot, dot, dot as usual means do 70 00:06:17,455 --> 00:06:19,830 something. Here's the first name. 71 00:06:19,830 --> 00:06:21,090 Here's the last name.