1 00:00:06,240 --> 00:00:11,928 So far we've seen several different types of data, primitive, interval enumeration, 2 00:00:11,928 --> 00:00:15,908 itemization. But all of them have been what's called 3 00:00:15,908 --> 00:00:21,309 atomic, in each case a single data value really consists of just one value. 4 00:00:22,810 --> 00:00:25,710 But lots of times we want to represent different kinds of information, 5 00:00:25,710 --> 00:00:29,460 information where two or more values naturally belong together. 6 00:00:30,810 --> 00:00:33,660 So for example, we might want to represent the xy position of an object 7 00:00:33,660 --> 00:00:37,367 moving on the screen. Or we might want to represent the first 8 00:00:37,367 --> 00:00:42,833 and last name of a single person. Or we might want to represent the main 9 00:00:42,833 --> 00:00:47,889 supervisor and salary of an employee. In all those cases there's two or more 10 00:00:47,889 --> 00:00:52,240 pieces of information that naturally belong together. 11 00:00:52,240 --> 00:00:55,200 To do that we're going to need compound data. 12 00:00:55,200 --> 00:01:01,060 And we're going to cover compound data in three videos in a now familiar form. 13 00:01:01,060 --> 00:01:04,482 In this video we'll learn a basic mechanism called define struct that 14 00:01:04,482 --> 00:01:09,684 allows us to create compound values. In the next video, we'll learn how to 15 00:01:09,684 --> 00:01:14,651 update the how to design data recipe for compound data. 16 00:01:14,651 --> 00:01:18,743 And in the third video, we'll see that the how to design worlds recipe works 17 00:01:18,743 --> 00:01:23,165 pretty much unchanged for compound data and we'll make our most elaborate world 18 00:01:23,165 --> 00:01:29,028 program yet. What we're learning is a new kind of 19 00:01:29,028 --> 00:01:33,798 definition called define-struct. So here, I'm going to define a struct, or 20 00:01:33,798 --> 00:01:37,220 a structure, and I'm just going to call this one pos as an abbreviation for 21 00:01:37,220 --> 00:01:43,011 position, so that's the structure name. And what comes next is something called 22 00:01:43,011 --> 00:01:46,380 the field names. And in this particular structure, all say 23 00:01:46,380 --> 00:01:51,470 that the fields are x and y. So you can see I'm making a structure 24 00:01:51,470 --> 00:01:55,020 that's going to help me represent xy positions. 25 00:01:55,020 --> 00:02:00,565 Now when I write that definition, if I run it, I don't get any value back. 26 00:02:00,565 --> 00:02:04,285 because define struct is a definition just like the defines we've seen so far 27 00:02:04,285 --> 00:02:09,784 function in, constant definition. It doesn't produce a value, it just 28 00:02:09,784 --> 00:02:13,660 declares definitions. In this case, it declares four 29 00:02:13,660 --> 00:02:17,800 definitions. The first one is a thing called a 30 00:02:17,800 --> 00:02:22,661 constructor, and it works like this. I can say make pos of 3 and 6, and what 31 00:02:22,661 --> 00:02:28,601 that does is it produces a position structure for which the value of the x 32 00:02:28,601 --> 00:02:35,670 field is 3 and the value of the y field is 6. 33 00:02:36,970 --> 00:02:41,301 That's an ordinary value so I can do something like make it be the value of a 34 00:02:41,301 --> 00:02:45,920 constant like p1. And if I run that, now p1 is a constant. 35 00:02:45,920 --> 00:02:49,565 And if I go down here and I just look at p1 Dr. 36 00:02:49,565 --> 00:02:53,045 Racket's going to show it to me or, or I should say, the BSL language is going to 37 00:02:53,045 --> 00:02:56,409 show it to me in the form of a call to the instructor, make pos 3, 6 in this 38 00:02:56,409 --> 00:03:00,410 case. So that let's me put three and six 39 00:03:00,410 --> 00:03:04,270 together into a pause structure. How do I then take them back apart? 40 00:03:04,270 --> 00:03:09,084 Well before I show you that let me just quickly do another example. 41 00:03:12,270 --> 00:03:14,400 But now I'll show you how they come back apart. 42 00:03:14,400 --> 00:03:18,380 The way they come back apart is that this define struct also defined two selectors. 43 00:03:18,380 --> 00:03:26,342 One of the selectors is called pos-x. And if I say pos-x of p1, then well I get 44 00:03:26,342 --> 00:03:31,077 3. Because p1 is the structure 3, is the 45 00:03:31,077 --> 00:03:36,960 make pos 3, 6. And so the x field of that is 3. 46 00:03:36,960 --> 00:03:45,478 So pos x of p1 is 3 and unsurprisingly pos y of p2, is 8. 47 00:03:45,478 --> 00:03:53,164 Okay, so this produces 3 and this produces 8. 48 00:03:53,164 --> 00:03:57,028 So make-pos is the constructor, pos-x and pos-y are the selectors, and there's one 49 00:03:57,028 --> 00:04:02,280 more function that defines struct defines for me, a thing called the predicate. 50 00:04:04,420 --> 00:04:06,773 It in this case is called pos question mark. 51 00:04:06,773 --> 00:04:11,250 And if I say pos question mark of p1, I get true. 52 00:04:11,250 --> 00:04:19,125 And if I say pos question mark of some value that isn't produced by make-pos, 53 00:04:19,125 --> 00:04:27,430 like hello, so, that give me false. So, this is true and that's false. 54 00:04:27,430 --> 00:04:36,929 So just to review quickly, the way to form a define struct is open peren, 55 00:04:36,929 --> 00:04:44,300 define struct. Some name, which is going to be the name 56 00:04:44,300 --> 00:04:48,380 of the structure, a parenthesis, and then some number of other names, one or more, 57 00:04:48,380 --> 00:04:55,253 that's the name of the fields. That definition sets up a constructor, 58 00:04:55,253 --> 00:05:00,382 which is always called make, dash, the structure name. 59 00:05:00,382 --> 00:05:04,414 It sets up as many selectors as there are fields, which are always called the 60 00:05:04,414 --> 00:05:09,855 structure name, dash, the field name. And it sets up a predicate, which is 61 00:05:09,855 --> 00:05:13,920 always called, the structure name, question mark. 62 00:05:13,920 --> 00:05:15,990 So that's the basic define struct mechanism. 63 00:05:17,030 --> 00:05:22,020 In the next video, we're going to look at how to design compound data definitions. 64 00:05:22,020 --> 00:05:24,220 In other words, data definitions for compound data. 65 00:05:24,220 --> 00:05:29,052 And they're going to use define struct as a supporting mechanism.