Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
00:00:00,000 --> 00:00:02,988
1
00:00:02,988 --> 00:00:06,474
[MUSIC PLAYING]
2
00:00:06,474 --> 00:01:13,310
3
00:01:13,310 --> 00:01:15,260
DAVID J. MALAN: All right.
4
00:01:15,260 --> 00:01:19,220
This is CS50, Harvard University's introduction
5
00:01:19,220 --> 00:01:21,560
to the intellectual enterprises of computer science
6
00:01:21,560 --> 00:01:22,940
and the arts of programming.
7
00:01:22,940 --> 00:01:27,660
My name is David Malan, and I actually took this course myself, back in 1996.
8
00:01:27,660 --> 00:01:29,382
I was a sophomore at the time.
9
00:01:29,382 --> 00:01:32,090
I was actually concentrating in government, because a year prior,
10
00:01:32,090 --> 00:01:34,550
as a first year, I'd come into Harvard thinking
11
00:01:34,550 --> 00:01:40,200
that I liked history and constitutional law and similar classes in high school.
12
00:01:40,200 --> 00:01:42,320
And so when I got here, I rather gravitated
13
00:01:42,320 --> 00:01:44,180
toward that which was familiar.
14
00:01:44,180 --> 00:01:47,508
I figured, if I liked and if I were good at that particular subject
15
00:01:47,508 --> 00:01:50,300
in high school, then that's presumably who I'm supposed to be here.
16
00:01:50,300 --> 00:01:52,910
But it wasn't until sophomore year that I got up
17
00:01:52,910 --> 00:01:55,847
the nerve to step foot in the CS50 classroom,
18
00:01:55,847 --> 00:01:57,680
and even then, it was only out of curiosity.
19
00:01:57,680 --> 00:02:02,295
Like I had no intention of studying computer science of even taking CS50
20
00:02:02,295 --> 00:02:03,170
when I got to campus.
21
00:02:03,170 --> 00:02:07,400
But people were talking about it, and there was a lot of beware.
22
00:02:07,400 --> 00:02:10,070
And it was perhaps for the initiated only,
23
00:02:10,070 --> 00:02:12,560
and I didn't really ultimately what computer science was.
24
00:02:12,560 --> 00:02:15,800
But for me, the light bulb went off.
25
00:02:15,800 --> 00:02:18,260
I found that, contrary to what I'd seen in high school,
26
00:02:18,260 --> 00:02:21,230
where I saw friends of mine like programming away in the computer lab,
27
00:02:21,230 --> 00:02:24,830
heads down, antisocially just doing whatever it was they were doing,
28
00:02:24,830 --> 00:02:28,100
it really wasn't that, once I got to this particular class
29
00:02:28,100 --> 00:02:29,280
and this particular place.
30
00:02:29,280 --> 00:02:32,240
It was much more about problem solving more generally and just learning
31
00:02:32,240 --> 00:02:34,852
how to express yourself in code, in different languages.
32
00:02:34,852 --> 00:02:37,310
So that you can actually solve problems of interest to you.
33
00:02:37,310 --> 00:02:41,060
Even if you have no intention of being a computer scientist or an engineer,
34
00:02:41,060 --> 00:02:43,140
but just want to be able to solve problems,
35
00:02:43,140 --> 00:02:47,280
analyze data do interesting things, in the arts, humanities, social sciences,
36
00:02:47,280 --> 00:02:49,710
physical sciences, or really any other field.
37
00:02:49,710 --> 00:02:52,700
And indeed, this particular path led me to computer science,
38
00:02:52,700 --> 00:02:56,090
but the hope for CS50 more generally is that, indeed, you
39
00:02:56,090 --> 00:02:58,453
just find your way to applying principles
40
00:02:58,453 --> 00:03:00,620
that you'll learn over the coming months to whatever
41
00:03:00,620 --> 00:03:02,370
field is of interest to you.
42
00:03:02,370 --> 00:03:05,720
With that said, it was definitely a lot of work and not
43
00:03:05,720 --> 00:03:07,500
without its frustrations for me.
44
00:03:07,500 --> 00:03:10,940
But there was no better feeling than like banging your head proverbial
45
00:03:10,940 --> 00:03:14,180
against the wall for some number of hours, even days, trying to fix a bug,
46
00:03:14,180 --> 00:03:15,630
a mistake in your code.
47
00:03:15,630 --> 00:03:19,160
And then, oh my God, the rush of emotion of accomplishment
48
00:03:19,160 --> 00:03:22,433
of pride of exhaustion when you finally solve some problem that's
49
00:03:22,433 --> 00:03:23,600
really been weighing on you.
50
00:03:23,600 --> 00:03:26,570
It's just so incredibly gratifying but also empowering.
51
00:03:26,570 --> 00:03:28,910
Because unlike a lot of fields, like computer science
52
00:03:28,910 --> 00:03:31,200
was built by humans themselves.
53
00:03:31,200 --> 00:03:33,620
And so if a human built this, surely, you, another human,
54
00:03:33,620 --> 00:03:35,460
can understand it as well.
55
00:03:35,460 --> 00:03:38,570
And so even though there's going to be some distractions along the way,
56
00:03:38,570 --> 00:03:41,420
you're going to see what looks incredibly cryptic, if you've never
57
00:03:41,420 --> 00:03:42,440
programmed before.
58
00:03:42,440 --> 00:03:46,402
Over time and with practice, everything just starts to make more sense.
59
00:03:46,402 --> 00:03:48,110
And with time and with practice, you just
60
00:03:48,110 --> 00:03:49,950
get better at this particular field.
61
00:03:49,950 --> 00:03:53,210
And indeed, really, the key to success in programming in general
62
00:03:53,210 --> 00:03:55,530
is just to allow yourself enough time.
63
00:03:55,530 --> 00:03:57,575
And so at least, thankfully, I quickly got
64
00:03:57,575 --> 00:03:59,450
into the habit of starting early in the week,
65
00:03:59,450 --> 00:04:00,950
for instance, when writing actual code.
66
00:04:00,950 --> 00:04:01,400
Why?
67
00:04:01,400 --> 00:04:03,200
Because you're going to run up against a wall.
68
00:04:03,200 --> 00:04:04,460
You're not going to see some bug.
69
00:04:04,460 --> 00:04:06,470
Something's not going to jump out at you, and that's fine.
70
00:04:06,470 --> 00:04:09,780
That's when you call it a day, take a break, move onto something else,
71
00:04:09,780 --> 00:04:11,030
and then just come back to it.
72
00:04:11,030 --> 00:04:14,393
And that's what keeps programming fun for me, even all of these years later,
73
00:04:14,393 --> 00:04:16,310
whether it's teaching or actually applying it.
74
00:04:16,310 --> 00:04:19,753
But there's, down the road, a history of an MIT hack,
75
00:04:19,753 --> 00:04:22,170
and it looked a little something like this, in yesteryear.
76
00:04:22,170 --> 00:04:25,280
And there was a little sigh the MIT students, when they made this hack.
77
00:04:25,280 --> 00:04:27,800
On the wall it says, getting an education from MIT
78
00:04:27,800 --> 00:04:30,680
is like drinking from a fire hose, which indeed they
79
00:04:30,680 --> 00:04:33,890
have connected to what should have been otherwise just a water fountain.
80
00:04:33,890 --> 00:04:36,170
And that's going to be what it feels like, sometimes,
81
00:04:36,170 --> 00:04:39,560
not just in computer science per se, but just an unfamiliar field.
82
00:04:39,560 --> 00:04:42,740
If you're not from STEM, if you're not from CS, that's fine.
83
00:04:42,740 --> 00:04:46,370
But so much of it, ultimately, is going to be absorbed by you
84
00:04:46,370 --> 00:04:49,430
and going to be within your grasp by terms end.
85
00:04:49,430 --> 00:04:52,110
So just keep in mind, that's very much the intent,
86
00:04:52,110 --> 00:04:55,580
but you'll be amazed what you're able to create, to accomplish,
87
00:04:55,580 --> 00:04:57,440
just three or so months hence.
88
00:04:57,440 --> 00:05:01,100
Indeed, 2/3 of you, contrary to what you might think are assume,
89
00:05:01,100 --> 00:05:03,140
have never taken a CS class before.
90
00:05:03,140 --> 00:05:06,140
So it's absolutely not the case that the person to the left or the right
91
00:05:06,140 --> 00:05:07,550
surely must know more than you.
92
00:05:07,550 --> 00:05:09,462
Indeed, it's quite the opposite.
93
00:05:09,462 --> 00:05:12,170
And as you'll see in the coming weeks, as you write your own code
94
00:05:12,170 --> 00:05:14,992
and solve your own problems, what ultimately matters in this course
95
00:05:14,992 --> 00:05:17,450
is not so much where you end up relative to your classmates
96
00:05:17,450 --> 00:05:20,783
but where you end up relative to yourself when you began.
97
00:05:20,783 --> 00:05:23,450
And it really is all about that delta, whether you've programmed
98
00:05:23,450 --> 00:05:27,380
or not, just getting something out of a class like this.
99
00:05:27,380 --> 00:05:30,140
And if it does take time, and if you do feel those frustrations,
100
00:05:30,140 --> 00:05:32,973
but you simultaneously eventually feel that sense of accomplishment,
101
00:05:32,973 --> 00:05:34,520
that just means it's all working.
102
00:05:34,520 --> 00:05:38,570
And indeed, hopefully, all the more worthwhile and gratifying, ultimately,
103
00:05:38,570 --> 00:05:39,712
as a result.
104
00:05:39,712 --> 00:05:41,670
So what are we going to do in the coming weeks?
105
00:05:41,670 --> 00:05:43,550
So here we are in week zero.
106
00:05:43,550 --> 00:05:46,520
We'll soon see why computers and computer scientists start counting,
107
00:05:46,520 --> 00:05:47,480
if you will, from 0.
108
00:05:47,480 --> 00:05:51,740
But week 0, is one in which we explore computational thinking,
109
00:05:51,740 --> 00:05:54,860
thinking like a computer, and starting to clean up your thought processes.
110
00:05:54,860 --> 00:05:57,360
Getting you to think, to solve problems more methodically,
111
00:05:57,360 --> 00:05:59,360
and then ultimately, translating that into code.
112
00:05:59,360 --> 00:06:02,390
And some of you might recognize this environment here, a.k.a.
113
00:06:02,390 --> 00:06:04,520
Scratch, coincidentally also from MIT.
114
00:06:04,520 --> 00:06:06,350
You might have used it in grade school.
115
00:06:06,350 --> 00:06:09,860
We'll use it today and a little bit this weekend in the course's first homework
116
00:06:09,860 --> 00:06:11,760
assignment or problem set.
117
00:06:11,760 --> 00:06:14,040
But not so much to play around in a way that you
118
00:06:14,040 --> 00:06:15,960
might have if you did use it in yesteryears,
119
00:06:15,960 --> 00:06:18,990
but to explore ideas of computer science and programming
120
00:06:18,990 --> 00:06:22,650
that we're going to use and reuse every week hereafter as well.
121
00:06:22,650 --> 00:06:24,900
Thereafter, we're going to transition just next week
122
00:06:24,900 --> 00:06:26,800
to week one, so to speak.
123
00:06:26,800 --> 00:06:30,030
Whereby, we'll introduce you to a more traditional language, a lower level
124
00:06:30,030 --> 00:06:32,702
language, an older language called C. And in C, you're
125
00:06:32,702 --> 00:06:35,910
going to use your keyboard, not so much your mouse and pointing and clicking,
126
00:06:35,910 --> 00:06:37,680
but you're going to write code that soon is going
127
00:06:37,680 --> 00:06:39,030
to look a little something like this.
128
00:06:39,030 --> 00:06:41,040
And if you've programmed before, you can probably
129
00:06:41,040 --> 00:06:42,120
glean what this is going to do.
130
00:06:42,120 --> 00:06:45,090
If you've never programmed before, which is the case for most of you,
131
00:06:45,090 --> 00:06:47,560
this too will soon make sense.
132
00:06:47,560 --> 00:06:51,120
But this is the most canonical program that most any programmer ever writes
133
00:06:51,120 --> 00:06:54,060
called Hello, World, and indeed, that and all
134
00:06:54,060 --> 00:06:57,660
of the surrounding syntax above and below just that sentence Hello, World,
135
00:06:57,660 --> 00:06:59,430
will soon make all the more sense.
136
00:06:59,430 --> 00:07:02,250
You'll learn how to use industry-standard tools, so to speak.
137
00:07:02,250 --> 00:07:04,980
Pictured here is something called Visual Studio code, or VS Code.
138
00:07:04,980 --> 00:07:07,262
You'll use a cloud based version of it initially,
139
00:07:07,262 --> 00:07:09,720
so you don't have to suffer with any technical difficulties
140
00:07:09,720 --> 00:07:10,678
or headaches like that.
141
00:07:10,678 --> 00:07:12,900
It'll just work right off the bat, but we'll
142
00:07:12,900 --> 00:07:14,760
use that to [INAUDIBLE] others ultimately
143
00:07:14,760 --> 00:07:18,840
to then explore ideas in computer science, principles that you can apply.
144
00:07:18,840 --> 00:07:20,590
And we'll take a look underneath the hood,
145
00:07:20,590 --> 00:07:24,900
so to speak, of your computer at your memory or RAM, Random Access Memory,
146
00:07:24,900 --> 00:07:27,210
where all of the data is ultimately going to be stored.
147
00:07:27,210 --> 00:07:29,880
We'll also take a look thereafter at bugs.
148
00:07:29,880 --> 00:07:31,470
A bug is a mistake in a program.
149
00:07:31,470 --> 00:07:34,380
Here is an actual bug in an actual computer in yesteryear,
150
00:07:34,380 --> 00:07:38,190
but we'll teach you how to debug programs, find your own mistakes,
151
00:07:38,190 --> 00:07:41,040
find others' mistakes, and improve that code as well.
152
00:07:41,040 --> 00:07:44,520
We'll transition then to algorithms, step-by-step instructions
153
00:07:44,520 --> 00:07:47,340
for solving some problems, which we'll touch on today too.
154
00:07:47,340 --> 00:07:49,590
And if you picture here, this is actually
155
00:07:49,590 --> 00:07:51,120
a pretty representative problem.
156
00:07:51,120 --> 00:07:53,578
Odds are, you haven't had to deal with something like this,
157
00:07:53,578 --> 00:07:55,510
but it's representative sorting, for instance.
158
00:07:55,510 --> 00:07:58,530
If you think of each of these small bars as being a small number,
159
00:07:58,530 --> 00:08:00,810
each of the bigger bars is being a bigger number,
160
00:08:00,810 --> 00:08:05,250
you might wonder, well, how could you as a human sort all of these bars,
161
00:08:05,250 --> 00:08:08,478
like get all the short bars over here, all the big bars over there?
162
00:08:08,478 --> 00:08:11,520
Well, odds are, if you're like me, you would probably kind of eyeball it,
163
00:08:11,520 --> 00:08:13,230
and if you could physically interact, you
164
00:08:13,230 --> 00:08:15,450
might just start grabbing the smallest elements first,
165
00:08:15,450 --> 00:08:16,533
put them over on the left.
166
00:08:16,533 --> 00:08:19,330
Maybe grab the biggest elements, put them over on the right.
167
00:08:19,330 --> 00:08:20,880
But what's your algorithm there?
168
00:08:20,880 --> 00:08:24,388
Like how would you teach someone younger than you, who's never done that before,
169
00:08:24,388 --> 00:08:24,930
how to do it?
170
00:08:24,930 --> 00:08:28,740
How would you compel your Mac or PC or phone to do something like that?
171
00:08:28,740 --> 00:08:31,650
You can't just wave your hand, and say, oh, figure it out.
172
00:08:31,650 --> 00:08:32,572
Move things around.
173
00:08:32,572 --> 00:08:34,530
You have to express yourself more methodically.
174
00:08:34,530 --> 00:08:36,815
So we'll translate even ideas like this into code too.
175
00:08:36,815 --> 00:08:38,940
And that's what the Googles and others of the world
176
00:08:38,940 --> 00:08:42,780
are doing constantly, as they sort and organize the world's information.
177
00:08:42,780 --> 00:08:45,490
We'll use metaphors along the way, if it helps.
178
00:08:45,490 --> 00:08:48,990
We'll talk about your computer's memory as being like a postal address.
179
00:08:48,990 --> 00:08:53,062
Like every mailbox in the world has some form of postal address, street, city,
180
00:08:53,062 --> 00:08:55,020
state, country, and the like, and it turns out,
181
00:08:55,020 --> 00:08:57,560
that's how your Mac, your PC, and your phone also work.
182
00:08:57,560 --> 00:09:00,060
You've got a whole bunch of memory, like the picture before,
183
00:09:00,060 --> 00:09:02,730
but you can think of it really as individual mailboxes.
184
00:09:02,730 --> 00:09:05,100
And you can put anything you want in those mailboxes,
185
00:09:05,100 --> 00:09:08,725
and you can go to a mailbox to grab information that's from it.
186
00:09:08,725 --> 00:09:11,100
So at the end of the day, that's really all your computer
187
00:09:11,100 --> 00:09:12,490
is doing with information.
188
00:09:12,490 --> 00:09:15,480
It's just organizing it, not into mailboxes per se,
189
00:09:15,480 --> 00:09:18,870
but a term you probably know called bytes, for instance, instead.
190
00:09:18,870 --> 00:09:22,560
We'll talk about problems that arise even nowadays.
191
00:09:22,560 --> 00:09:24,960
In fact, most of you are familiar with your Mac,
192
00:09:24,960 --> 00:09:29,070
PC, even phone like spontaneously rebooting sometimes, crashing,
193
00:09:29,070 --> 00:09:32,520
the little annoying spinning beach ball or hourglass icon that happens.
194
00:09:32,520 --> 00:09:33,790
Like what is with that?
195
00:09:33,790 --> 00:09:36,930
Well, those are just bugs in programs that humans at Apple and Google
196
00:09:36,930 --> 00:09:40,110
and Microsoft and others, they screwed up, and they wrote buggy code.
197
00:09:40,110 --> 00:09:42,930
And your computer, when it encounters those mistakes,
198
00:09:42,930 --> 00:09:44,290
doesn't know what to do.
199
00:09:44,290 --> 00:09:47,730
And so 9 times out of 10, so to speak, it just crashes or freezes
200
00:09:47,730 --> 00:09:50,110
or the like, but that kind of stuff will make more sense.
201
00:09:50,110 --> 00:09:52,485
So even the real world will make sense, and pictured here
202
00:09:52,485 --> 00:09:55,350
are some lower level terms we'll eventually get to mid-semester.
203
00:09:55,350 --> 00:09:57,870
But generally speaking, when something is going this way,
204
00:09:57,870 --> 00:10:01,020
as per this arrow, and something is going this way, as per this arrow,
205
00:10:01,020 --> 00:10:02,440
like that does not end well.
206
00:10:02,440 --> 00:10:05,250
And that often is what happens when your computer crashes.
207
00:10:05,250 --> 00:10:08,460
Someone's using memory up here, but someone else is using memory down here,
208
00:10:08,460 --> 00:10:11,470
and then they're not really talking left hand and right hand.
209
00:10:11,470 --> 00:10:14,970
So that is just a high level overview of some of the problems we'll encounter,
210
00:10:14,970 --> 00:10:17,010
but we'll focus to on data, ultimately.
211
00:10:17,010 --> 00:10:20,530
So pictured here is something fairly technical called a hash table.
212
00:10:20,530 --> 00:10:23,735
It's an amalgam of something we're going to soon call an array and also
213
00:10:23,735 --> 00:10:25,110
something we call a link to list.
214
00:10:25,110 --> 00:10:27,330
And these are just fancy terms for describing
215
00:10:27,330 --> 00:10:31,020
how you can organize information even more flexibly
216
00:10:31,020 --> 00:10:34,440
than just putting individual values in mailboxes.
217
00:10:34,440 --> 00:10:37,900
Like how could you build structures, like actual data structures
218
00:10:37,900 --> 00:10:40,510
so to speak, two-dimensional structures at that?
219
00:10:40,510 --> 00:10:43,230
And so what you're seeing here is a glimpse, as some of you
220
00:10:43,230 --> 00:10:45,720
might have recognized, of some Harry Potter universe names,
221
00:10:45,720 --> 00:10:48,040
but they're organized somewhat alphabetically.
222
00:10:48,040 --> 00:10:50,790
And notice, that any time there's multiple people with a name that
223
00:10:50,790 --> 00:10:53,940
starts with H, like Hermione, Harry, and Hagrid, well, they
224
00:10:53,940 --> 00:10:57,270
can't all fit in that mailbox, if each of these squares along the left
225
00:10:57,270 --> 00:10:58,440
is that same mailbox.
226
00:10:58,440 --> 00:11:00,330
So you have to chain them together.
227
00:11:00,330 --> 00:11:02,490
Well, you'll learn how to do that in code.
228
00:11:02,490 --> 00:11:04,773
So that even if you get more data than you expect,
229
00:11:04,773 --> 00:11:07,440
if your business is booming, and you're some web-based business,
230
00:11:07,440 --> 00:11:09,810
how do you keep adding and adding information
231
00:11:09,810 --> 00:11:12,790
to your software to actually keep up with it?
232
00:11:12,790 --> 00:11:16,480
But this, again, is what code's going to soon look like, as soon as next week,
233
00:11:16,480 --> 00:11:20,500
in week one, this here being C, but we'll transition in a few weeks
234
00:11:20,500 --> 00:11:25,190
to a more modern, higher level language, so to speak, called Python.
235
00:11:25,190 --> 00:11:27,940
Indeed, the course very deliberately, back in my day and now this,
236
00:11:27,940 --> 00:11:31,540
introduces you first to C, which funny enough, many people don't
237
00:11:31,540 --> 00:11:33,520
tend to program in certainly every day.
238
00:11:33,520 --> 00:11:36,980
I use C, generally, September, October, November,
239
00:11:36,980 --> 00:11:38,590
December, when teaching CS50 itself.
240
00:11:38,590 --> 00:11:40,540
But it's everywhere, nonetheless.
241
00:11:40,540 --> 00:11:43,870
In fact, even today's other languages, with which you might be familiar,
242
00:11:43,870 --> 00:11:48,190
like Python and Java and yet others still, you
243
00:11:48,190 --> 00:11:50,800
see this same primitive language underneath the hood,
244
00:11:50,800 --> 00:11:52,360
because it's so darn fast.
245
00:11:52,360 --> 00:11:54,110
And as you'll learn over the coming weeks,
246
00:11:54,110 --> 00:11:57,310
it really gives you access to and an understanding of what's
247
00:11:57,310 --> 00:11:59,180
going on conceptually down here.
248
00:11:59,180 --> 00:12:01,900
So that thereafter, after CS50, when you're writing code,
249
00:12:01,900 --> 00:12:05,180
you can think at a very high level what's actually going on.
250
00:12:05,180 --> 00:12:08,710
So in fact, in just a few weeks, what looks like this in C
251
00:12:08,710 --> 00:12:11,170
is going to look instead like this in Python.
252
00:12:11,170 --> 00:12:13,930
And you'll better understand what's going on underneath the hood,
253
00:12:13,930 --> 00:12:16,540
and odds are, after this class, you'll reach for a language,
254
00:12:16,540 --> 00:12:19,540
like Python more frequently than C, but you're
255
00:12:19,540 --> 00:12:22,750
going to benefit from that bottom-up understanding thereof.
256
00:12:22,750 --> 00:12:26,680
Thereafter and towards term's end, we'll introduce you to a few other ideas,
257
00:12:26,680 --> 00:12:28,630
like where do you put large amounts of data?
258
00:12:28,630 --> 00:12:31,660
In things called databases, not things like spreadsheets, like here,
259
00:12:31,660 --> 00:12:32,690
but actual databases.
260
00:12:32,690 --> 00:12:34,690
We're using those same kinds of data structures,
261
00:12:34,690 --> 00:12:37,300
you lay things out in an interesting way in memory.
262
00:12:37,300 --> 00:12:40,000
Thereafter, we'll transition to a very familiar environment
263
00:12:40,000 --> 00:12:42,010
that you and I use every day, the web.
264
00:12:42,010 --> 00:12:45,010
Like the web has become rather the User Interface, or UI,
265
00:12:45,010 --> 00:12:48,970
that we use everywhere, on the laptops, desktops, and even mobile devices,
266
00:12:48,970 --> 00:12:49,690
nowadays.
267
00:12:49,690 --> 00:12:52,360
Well, pictured here is a language called HTML.
268
00:12:52,360 --> 00:12:53,693
It's not a programming language.
269
00:12:53,693 --> 00:12:56,443
It's a markup language, and some of you might have made home pages
270
00:12:56,443 --> 00:12:57,550
or portfolios in the past.
271
00:12:57,550 --> 00:13:01,270
But you'll understand what's going on here, but more powerfully,
272
00:13:01,270 --> 00:13:04,510
you'll understand how the computer sees that same kind of code,
273
00:13:04,510 --> 00:13:08,930
builds up a hierarchical family tree-type structure in memory.
274
00:13:08,930 --> 00:13:11,530
And then you can manipulate that tree with code
275
00:13:11,530 --> 00:13:14,800
to actually add more and more information, chat messages, anything
276
00:13:14,800 --> 00:13:16,150
on the screen that you like.
277
00:13:16,150 --> 00:13:19,000
And finally, we'll tie all of this together
278
00:13:19,000 --> 00:13:22,360
by introducing what are called frameworks and libraries,
279
00:13:22,360 --> 00:13:26,410
third-party code that makes it a lot easier to solve problems of interest
280
00:13:26,410 --> 00:13:27,140
to you.
281
00:13:27,140 --> 00:13:30,250
And so in particular, here, this is the very first web app
282
00:13:30,250 --> 00:13:33,820
that I myself made back in like 1997.
283
00:13:33,820 --> 00:13:36,910
I was part of the first-year intramural sports program,
284
00:13:36,910 --> 00:13:39,850
not as an athlete but as the programmer, and I was teaching myself
285
00:13:39,850 --> 00:13:41,380
how to build web applications.
286
00:13:41,380 --> 00:13:44,410
I only knew C and maybe a little bit of something else at the time.
287
00:13:44,410 --> 00:13:47,320
But this became, for Harvard at least, the very first website
288
00:13:47,320 --> 00:13:50,020
for the first-year intramural sports program,
289
00:13:50,020 --> 00:13:53,290
and it wasn't just a static website with links and images and the like.
290
00:13:53,290 --> 00:13:54,190
It was interactive.
291
00:13:54,190 --> 00:13:55,570
You could register for sports.
292
00:13:55,570 --> 00:13:58,750
We could input exactly who was in a tournament bracket or the like,
293
00:13:58,750 --> 00:14:01,340
and it could actually automatically keep track of this data.
294
00:14:01,340 --> 00:14:04,420
So there too, after just three months of a class like this,
295
00:14:04,420 --> 00:14:06,490
you'll go from writing quite simply this week
296
00:14:06,490 --> 00:14:08,950
and next Hello, World to building things like
297
00:14:08,950 --> 00:14:12,430
this for whether it's web, mobile, or other platforms
298
00:14:12,430 --> 00:14:13,822
as well, if you so choose.
299
00:14:13,822 --> 00:14:17,030
But we'll get you off of the course's infrastructure, by the end of the term.
300
00:14:17,030 --> 00:14:19,570
You won't be using any toy environments along the way.
301
00:14:19,570 --> 00:14:23,350
We'll empower you, ultimately, to write code after CS50, especially if this
302
00:14:23,350 --> 00:14:27,272
is the only CS class you ever take, on your own Mac or PC,
303
00:14:27,272 --> 00:14:29,980
using the same software, but not the cloud-based version thereof.
304
00:14:29,980 --> 00:14:34,060
But all of this software is itself free and can be used by you
305
00:14:34,060 --> 00:14:37,010
powerfully after the course's own end.
306
00:14:37,010 --> 00:14:40,760
But along the way, as you may know, there
307
00:14:40,760 --> 00:14:43,660
is this tradition within the class, particularly in healthy times,
308
00:14:43,660 --> 00:14:46,210
of a number of events that really brings people get together,
309
00:14:46,210 --> 00:14:48,040
not just collaboratively and academically,
310
00:14:48,040 --> 00:14:52,120
but to just solve problems and generally engage with each other as well.
311
00:14:52,120 --> 00:14:55,660
Coming up first, CS50 Puzzle Day, which is meant to be not jigsaw
312
00:14:55,660 --> 00:14:58,900
puzzles but logic puzzles that require no prior experience
313
00:14:58,900 --> 00:15:00,460
with computer science or programming.
314
00:15:00,460 --> 00:15:04,300
But it's just an opportunity to quietly work on a packet of puzzles
315
00:15:04,300 --> 00:15:07,270
with some number of friends for prizes and more.
316
00:15:07,270 --> 00:15:10,510
Later in the semester, once you tackle your final projects,
317
00:15:10,510 --> 00:15:13,810
the capstone of the course, where we don't give you a homework to write,
318
00:15:13,810 --> 00:15:15,910
you yourself come up with something to build.
319
00:15:15,910 --> 00:15:20,320
We'll get together generally around 7:00 PM in the evening,
320
00:15:20,320 --> 00:15:23,260
wrap up around 7:00 AM, if you so choose.
321
00:15:23,260 --> 00:15:26,080
And it's an evening, a 12-hour opportunity to collaborate with
322
00:15:26,080 --> 00:15:30,250
classmates on your very own final project, in a large space on campus,
323
00:15:30,250 --> 00:15:31,090
that ends--
324
00:15:31,090 --> 00:15:33,340
if you're awake with us-- at 5:00 AM.
325
00:15:33,340 --> 00:15:37,330
We can hop on some CS50 shuttles and go down the road for some pancakes at IHOP
326
00:15:37,330 --> 00:15:38,710
around 6:00.
327
00:15:38,710 --> 00:15:43,090
Of course-- of course, this is 6:00, 7:00 AM at that point,
328
00:15:43,090 --> 00:15:45,640
but it's an opportunity finally to lead into what's
329
00:15:45,640 --> 00:15:49,800
called the CS50 fair, which is an end of semester celebration, an exhibition,
330
00:15:49,800 --> 00:15:52,300
of everything that you'll accomplish over the coming months.
331
00:15:52,300 --> 00:15:56,200
And in fact, pictured here are some of your predecessors in healthy times.
332
00:15:56,200 --> 00:15:59,200
The CS50 fair allows you to come with your laptop or phone
333
00:15:59,200 --> 00:16:02,320
and exhibits of students, faculty, and staff across campus
334
00:16:02,320 --> 00:16:04,420
put together something in person and on video
335
00:16:04,420 --> 00:16:07,733
that people can delight in seeing, as you exhibit what it is you created
336
00:16:07,733 --> 00:16:10,150
and what you learned over the course of the several weeks.
337
00:16:10,150 --> 00:16:14,880
And ultimately, a chance to just share and inspire others as well.
338
00:16:14,880 --> 00:16:17,600
And you'll all walk home, ultimately, with your own I took
339
00:16:17,600 --> 00:16:20,820
CS50 T-shirts saying as much as well.
340
00:16:20,820 --> 00:16:23,820
So with that high level overview of the course,
341
00:16:23,820 --> 00:16:27,890
I propose that we begin to take a look at what computer science itself is
342
00:16:27,890 --> 00:16:31,010
and what it is we're going to be doing over the next several weeks
343
00:16:31,010 --> 00:16:32,780
at this lower level [INAUDIBLE] too.
344
00:16:32,780 --> 00:16:34,130
So what is computer science?
345
00:16:34,130 --> 00:16:34,630
Right?
346
00:16:34,630 --> 00:16:38,240
If you're maybe like me or new people like my friends in high school,
347
00:16:38,240 --> 00:16:40,310
you probably assume that it means programming.
348
00:16:40,310 --> 00:16:44,040
And that's absolutely a big part of it for a lot of people, because with code,
349
00:16:44,040 --> 00:16:46,770
you can write, you can express ideas, and solve actual problems,
350
00:16:46,770 --> 00:16:48,110
especially involving data.
351
00:16:48,110 --> 00:16:52,100
But computer science itself is really the study of information, if you will.
352
00:16:52,100 --> 00:16:55,170
How do you represent it, and how do you actually process it?
353
00:16:55,170 --> 00:16:57,290
And in that sense, computational thinking
354
00:16:57,290 --> 00:17:02,090
is just the application of ideas from computer science, a course like this,
355
00:17:02,090 --> 00:17:05,880
to problems of interest to you, again, in the arts, humanities, sciences,
356
00:17:05,880 --> 00:17:09,089
social sciences, whatever the domain of interest is to you.
357
00:17:09,089 --> 00:17:13,040
So with that, if computer science is all about information and with it
358
00:17:13,040 --> 00:17:16,770
the solving of problems, well, what does it actually mean to solve a problem?
359
00:17:16,770 --> 00:17:20,992
Let's see if we can't propose a model into which all of the lessons learned
360
00:17:20,992 --> 00:17:21,950
will ultimately follow.
361
00:17:21,950 --> 00:17:25,188
And I'd propose that this is problem solving.
362
00:17:25,188 --> 00:17:27,980
You've got some input, which is like the problem you want to solve.
363
00:17:27,980 --> 00:17:29,070
The goal is to solve it.
364
00:17:29,070 --> 00:17:31,400
So that's the so-called output, and then somewhere
365
00:17:31,400 --> 00:17:34,970
in here, the proverbial black box, is some kind of secret sauce
366
00:17:34,970 --> 00:17:36,233
that gets the work done.
367
00:17:36,233 --> 00:17:38,900
And in the coming months, we'll have to decide, well, how are we
368
00:17:38,900 --> 00:17:42,440
going to represent these inputs and outputs, and really, how do we code up?
369
00:17:42,440 --> 00:17:46,490
How do we write solutions for what it is that's solving the problem of interest
370
00:17:46,490 --> 00:17:47,310
to us?
371
00:17:47,310 --> 00:17:50,900
So when it comes to representation of information, like there's a lot of ways
372
00:17:50,900 --> 00:17:51,560
we can do this.
373
00:17:51,560 --> 00:17:53,477
And for instance, if the problem at hand quite
374
00:17:53,477 --> 00:17:56,180
simply is to take attendance at the beginning of class,
375
00:17:56,180 --> 00:17:59,340
on the first day of school, well, how could we go about doing this?
376
00:17:59,340 --> 00:18:02,480
Well, we could actually use a system called unary.
377
00:18:02,480 --> 00:18:03,420
Well, what is that?
378
00:18:03,420 --> 00:18:08,300
Well, that's a fancy way of saying 1, 2, 3, 4, 5, maybe 6, 7, 8, 9, 10.
379
00:18:08,300 --> 00:18:10,130
And I can use my digits--
380
00:18:10,130 --> 00:18:13,298
pun intended-- on my fingers to actually count everyone up.
381
00:18:13,298 --> 00:18:16,340
And eventually, you need toes and whatnots, if you have to count so high.
382
00:18:16,340 --> 00:18:19,310
But unary is a very simple system of using
383
00:18:19,310 --> 00:18:22,872
a single symbol, a human finger in this case, to just solve some problem,
384
00:18:22,872 --> 00:18:24,830
like counting the number of people in the room.
385
00:18:24,830 --> 00:18:27,950
Let's make this slightly more technical for a moment, a little more mathy.
386
00:18:27,950 --> 00:18:31,280
That's just called base-1, where the base under which you're operating
387
00:18:31,280 --> 00:18:34,700
has one digit in it, like literally a human finger, and maybe
388
00:18:34,700 --> 00:18:36,920
multiple such fingers, if you need to count higher.
389
00:18:36,920 --> 00:18:39,890
But of course, most of you, if not all of you,
390
00:18:39,890 --> 00:18:44,680
generally, vaguely know that computers use something other than unary--
391
00:18:44,680 --> 00:18:47,180
and even you and I probably don't use this that often-- they
392
00:18:47,180 --> 00:18:50,430
use what language or alphabet instead.
393
00:18:50,430 --> 00:18:50,930
Yeah.
394
00:18:50,930 --> 00:18:55,980
So binary, so binary is indeed the system that computers somehow use.
395
00:18:55,980 --> 00:19:00,410
So in this case, bi implying two, and so computers have two digits,
396
00:19:00,410 --> 00:19:01,760
it turns out, at their disposal.
397
00:19:01,760 --> 00:19:04,680
And in fact, if you've ever heard the technical term bit,
398
00:19:04,680 --> 00:19:07,490
which is like a smaller version of a byte-- more on that soon.
399
00:19:07,490 --> 00:19:10,882
Well, a binary digit is the origin of that term "Bit,"
400
00:19:10,882 --> 00:19:14,090
because if you get rid of some of the letters, and are left from binary digit
401
00:19:14,090 --> 00:19:15,965
with just B-I-T, thus is a bit.
402
00:19:15,965 --> 00:19:17,990
A bit is just a 0 and 1.
403
00:19:17,990 --> 00:19:20,750
It's two more digits than you might have on your own finger,
404
00:19:20,750 --> 00:19:23,030
and of course, it's fewer though than you and I have.
405
00:19:23,030 --> 00:19:25,820
You and I typically use, as humans, the decimal system.
406
00:19:25,820 --> 00:19:29,390
Dec meaning 10, because you and I generally use 0 through 9.
407
00:19:29,390 --> 00:19:30,800
So on the one hand--
408
00:19:30,800 --> 00:19:32,930
another pun intended-- you've got unary.
409
00:19:32,930 --> 00:19:34,640
Computers use binary.
410
00:19:34,640 --> 00:19:37,760
We humans generally think and talk in terms of decimal.
411
00:19:37,760 --> 00:19:40,370
But at the end of the day, these are fundamentally
412
00:19:40,370 --> 00:19:42,320
going to be the same thing, which is to say
413
00:19:42,320 --> 00:19:44,090
that it's all pretty accessible to us.
414
00:19:44,090 --> 00:19:47,610
Even if you're not a computer person, I daresay you're about to be.
415
00:19:47,610 --> 00:19:48,620
So what is a bit?
416
00:19:48,620 --> 00:19:51,590
Well, a bit then is a 0 or a 1.
417
00:19:51,590 --> 00:19:53,570
That is a so-called binary digit.
418
00:19:53,570 --> 00:19:56,090
But how do computers only speak in binary?
419
00:19:56,090 --> 00:19:59,360
How do they solve problems, represent information, using only binary?
420
00:19:59,360 --> 00:20:02,750
Well, at the end of the day, if they want to represent 0 and 1,
421
00:20:02,750 --> 00:20:05,300
we need to do so physically somehow.
422
00:20:05,300 --> 00:20:10,190
And I daresay that maybe the simplest way to think about a bit, a 0 or a 1,
423
00:20:10,190 --> 00:20:11,310
is like a light bulb.
424
00:20:11,310 --> 00:20:15,020
And so by human convention, let's just assume that if you were a computer,
425
00:20:15,020 --> 00:20:17,060
be it a laptop, desktop, phone, or the like,
426
00:20:17,060 --> 00:20:20,030
and you want to represent the number 0, you
427
00:20:20,030 --> 00:20:22,770
know what, you just keep the light switch off.
428
00:20:22,770 --> 00:20:24,290
You keep a light bulb off.
429
00:20:24,290 --> 00:20:26,210
If by contrast, you're that same computer,
430
00:20:26,210 --> 00:20:28,160
and you want to represent the number 1, you
431
00:20:28,160 --> 00:20:30,990
take that same switch, that same light bulb, and just turn it on.
432
00:20:30,990 --> 00:20:34,400
So a light bulb that's on represents a 1, and a light bulb that's off
433
00:20:34,400 --> 00:20:35,840
represents a 0.
434
00:20:35,840 --> 00:20:37,928
So why is this relevant to computers?
435
00:20:37,928 --> 00:20:40,970
Well, at the end of the day, you and I are charging our laptops or phones
436
00:20:40,970 --> 00:20:41,340
at night.
437
00:20:41,340 --> 00:20:44,390
So there's some physical resource being replenished there, whether you're
438
00:20:44,390 --> 00:20:46,020
on battery or some power cord.
439
00:20:46,020 --> 00:20:48,990
And so inside of a computer are just thousands,
440
00:20:48,990 --> 00:20:51,433
millions of tiny little switches, nowadays.
441
00:20:51,433 --> 00:20:53,600
You can think of them metaphorically as light bulbs,
442
00:20:53,600 --> 00:20:55,430
but they don't actually shine light.
443
00:20:55,430 --> 00:20:57,740
But there are tiny, tiny little switches,
444
00:20:57,740 --> 00:21:01,430
and those switches, if you've ever heard the term, are just called transistors.
445
00:21:01,430 --> 00:21:04,520
So like computers have millions of transistors that can either be flipped
446
00:21:04,520 --> 00:21:08,390
on to represent 1's or flipped off to represent 0's.
447
00:21:08,390 --> 00:21:12,440
And from that very simple mechanism, electricity is there,
448
00:21:12,440 --> 00:21:15,110
or it's not, a 1 or a 0.
449
00:21:15,110 --> 00:21:19,070
Computers can actually count, obviously, from 0 to 1,
450
00:21:19,070 --> 00:21:23,160
but it turns out, even higher, if they use a little more electricity as well.
451
00:21:23,160 --> 00:21:24,420
So how might I do this?
452
00:21:24,420 --> 00:21:27,740
Well, let me go ahead and propose that I just grab one of our own light bulbs
453
00:21:27,740 --> 00:21:28,500
here on stage.
454
00:21:28,500 --> 00:21:29,210
This one is off.
455
00:21:29,210 --> 00:21:32,060
So for instance, if this were miniaturized inside of your Mac, PC,
456
00:21:32,060 --> 00:21:34,517
or phone, this would be a transistor, and indeed, here's
457
00:21:34,517 --> 00:21:35,850
the little switch on the bottom.
458
00:21:35,850 --> 00:21:39,320
And if your computer wants to represent a 0, it just leaves the switch off,
459
00:21:39,320 --> 00:21:40,910
and the light is not shining.
460
00:21:40,910 --> 00:21:43,670
If you want to represent a 1, well now, I've counted as high
461
00:21:43,670 --> 00:21:45,193
as 1, because the switch is now on.
462
00:21:45,193 --> 00:21:46,610
I've grabbed a little electricity.
463
00:21:46,610 --> 00:21:48,540
I'm holding on to it inside of the computer,
464
00:21:48,540 --> 00:21:50,598
and so now I see that this is a 1.
465
00:21:50,598 --> 00:21:53,390
All right, but unfortunately, with just one switch, one light bulb,
466
00:21:53,390 --> 00:21:56,630
I can only count from 0 to 1.
467
00:21:56,630 --> 00:21:59,475
How do I count out higher, might you think, intuitively?
468
00:21:59,475 --> 00:22:00,350
AUDIENCE: [INAUDIBLE]
469
00:22:00,350 --> 00:22:01,070
DAVID J. MALAN: Say it again.
470
00:22:01,070 --> 00:22:01,430
AUDIENCE: More lightbulbs.
471
00:22:01,430 --> 00:22:02,900
DAVID J. MALAN: Yeah, so more light bulbs.
472
00:22:02,900 --> 00:22:03,650
So let me do this.
473
00:22:03,650 --> 00:22:07,760
Let me just grab something to put these on, so I can use a few of them
474
00:22:07,760 --> 00:22:08,270
at a time.
475
00:22:08,270 --> 00:22:11,720
And let me propose that here, instead of having just one light bulb,
476
00:22:11,720 --> 00:22:14,150
let me give myself maybe three in total.
477
00:22:14,150 --> 00:22:17,748
So all of them are initially off, and if you think of this in miniature form,
478
00:22:17,748 --> 00:22:20,540
in your mind's eye, this is like a computer with three transistors.
479
00:22:20,540 --> 00:22:24,860
Three switches representing now the number you and I know as 0.
480
00:22:24,860 --> 00:22:25,490
Why?
481
00:22:25,490 --> 00:22:27,150
They're just all off.
482
00:22:27,150 --> 00:22:30,450
So how does a computer go about representing the number 1?
483
00:22:30,450 --> 00:22:33,230
Well, it turns on one of these light bulbs.
484
00:22:33,230 --> 00:22:35,600
And how does the computer represent the number 2?
485
00:22:35,600 --> 00:22:39,900
Well, you might think, if I may, you just turn on a second light bulb.
486
00:22:39,900 --> 00:22:42,290
And if you might think, how does a computer represent 3?
487
00:22:42,290 --> 00:22:44,250
You just turn on the third light bulb.
488
00:22:44,250 --> 00:22:47,150
And so as such, with three bits, a computer
489
00:22:47,150 --> 00:22:51,020
would seem to be able to count from 0 on up to 1, 2, 3.
490
00:22:51,020 --> 00:22:53,480
But it turns out, if I'm a little smarter here,
491
00:22:53,480 --> 00:22:55,760
I can actually count higher than that.
492
00:22:55,760 --> 00:22:56,310
Why?
493
00:22:56,310 --> 00:22:59,060
Well, I'm just considering the combination of bulbs being on here.
494
00:22:59,060 --> 00:23:00,435
What if I do something like this?
495
00:23:00,435 --> 00:23:04,160
This is still 0, I will claim, but what if I propose now that this
496
00:23:04,160 --> 00:23:06,800
will be how a computer represents 1--
497
00:23:06,800 --> 00:23:08,840
on, off, off.
498
00:23:08,840 --> 00:23:12,680
This, though, will be how the computer represents 2.
499
00:23:12,680 --> 00:23:14,360
Notice, I didn't turn on the same two.
500
00:23:14,360 --> 00:23:16,430
I'm just turning on the one in the middle.
501
00:23:16,430 --> 00:23:19,820
This I now claim will be how a computer represents 3.
502
00:23:19,820 --> 00:23:22,190
This is going to be-- in just a second--
503
00:23:22,190 --> 00:23:26,450
how a computer represents the number we know as 4,
504
00:23:26,450 --> 00:23:28,550
and yet, I'm still only using three bulbs.
505
00:23:28,550 --> 00:23:32,600
This is going to be the number the computer represents as 5.
506
00:23:32,600 --> 00:23:36,530
This is going to be how the computer represents the number 6,
507
00:23:36,530 --> 00:23:39,030
and then lastly, it turns out, with three light bulbs,
508
00:23:39,030 --> 00:23:42,380
if you're smart about it, you can count it seems as high as 7.
509
00:23:42,380 --> 00:23:46,220
Now, even if you lost track of what I was turning on and why,
510
00:23:46,220 --> 00:23:49,070
I claim there were eight different patterns, from all of them
511
00:23:49,070 --> 00:23:50,470
off to all of them on.
512
00:23:50,470 --> 00:23:52,220
But notice that I started to permute them.
513
00:23:52,220 --> 00:23:55,220
I took into account which ones were on and which ones were off.
514
00:23:55,220 --> 00:23:59,600
Why, though, do these represent the numbers we know as 0 through 7?
515
00:23:59,600 --> 00:24:02,503
Well, let me go ahead, and maybe let's do this.
516
00:24:02,503 --> 00:24:04,670
Instead of just considering there to be light bulbs,
517
00:24:04,670 --> 00:24:09,650
let's assign some special significance to each of them, based on where it is.
518
00:24:09,650 --> 00:24:15,350
And maybe for this, could we get maybe three volunteers, three volunteer?
519
00:24:15,350 --> 00:24:16,438
OK.
520
00:24:16,438 --> 00:24:17,480
You're being volunteered.
521
00:24:17,480 --> 00:24:17,690
OK.
522
00:24:17,690 --> 00:24:18,210
Come on up.
523
00:24:18,210 --> 00:24:19,610
If you want to go over to the stage there.
524
00:24:19,610 --> 00:24:19,760
Yeah.
525
00:24:19,760 --> 00:24:22,350
You want to come on up as well, and over here as well.
526
00:24:22,350 --> 00:24:24,530
So there are some stairs on either end.
527
00:24:24,530 --> 00:24:27,440
Maybe a round of applause for our first volunteers of term.
528
00:24:27,440 --> 00:24:30,842
[APPLAUSE]
529
00:24:30,842 --> 00:24:34,050
530
00:24:34,050 --> 00:24:34,550
All right.
531
00:24:34,550 --> 00:24:37,670
So you want to be our number 1, and if you want to go ahead and stand
532
00:24:37,670 --> 00:24:38,633
roughly right here.
533
00:24:38,633 --> 00:24:40,175
How about do you want to be number 2?
534
00:24:40,175 --> 00:24:41,330
AUDIENCE: Yeah.
535
00:24:41,330 --> 00:24:43,970
DAVID J. MALAN: Come on over right to the right of here,
536
00:24:43,970 --> 00:24:46,090
and you'll be number 4, it turns out.
537
00:24:46,090 --> 00:24:48,320
If you want to come over here, on this end,
538
00:24:48,320 --> 00:24:52,460
let's give you all a moment to introduce yourselves briefly to your classmates,
539
00:24:52,460 --> 00:24:53,120
if you'd like.
540
00:24:53,120 --> 00:24:53,662
AUDIENCE: Hi.
541
00:24:53,662 --> 00:24:54,350
I'm Ellie.
542
00:24:54,350 --> 00:24:56,120
I'm a senior.
543
00:24:56,120 --> 00:24:57,440
DAVID J. MALAN: Nice to meet.
544
00:24:57,440 --> 00:24:59,840
AUDIENCE: I'm [? Rayhanna, ?] and I'm a first year.
545
00:24:59,840 --> 00:25:00,978
DAVID J. MALAN: Welcome.
546
00:25:00,978 --> 00:25:01,520
AUDIENCE: Hi.
547
00:25:01,520 --> 00:25:03,850
I'm Joseph, and I am a first year.
548
00:25:03,850 --> 00:25:04,850
DAVID J. MALAN: Welcome.
549
00:25:04,850 --> 00:25:05,350
All right.
550
00:25:05,350 --> 00:25:07,250
So so glad to have all three of you up here.
551
00:25:07,250 --> 00:25:07,940
Thank you.
552
00:25:07,940 --> 00:25:10,940
[APPLAUSE]
553
00:25:10,940 --> 00:25:14,470
Let me propose now that we'd like you three
554
00:25:14,470 --> 00:25:16,480
to represent how about the number 0.
555
00:25:16,480 --> 00:25:19,930
And I claim now that if each of you now represents a switch,
556
00:25:19,930 --> 00:25:21,790
you have fancier light bulbs now.
557
00:25:21,790 --> 00:25:22,400
One is a 1.
558
00:25:22,400 --> 00:25:22,900
One is a 2.
559
00:25:22,900 --> 00:25:25,150
One is a 4, but each of you is still just has a switch
560
00:25:25,150 --> 00:25:27,340
on the bottom, in fact, of your plastic devices.
561
00:25:27,340 --> 00:25:30,520
I claim these three volunteers are representing the number 0.
562
00:25:30,520 --> 00:25:34,240
Let me ask you all now, how might you represent the number 1?
563
00:25:34,240 --> 00:25:36,240
How should you cooperate here?
564
00:25:36,240 --> 00:25:36,740
OK.
565
00:25:36,740 --> 00:25:39,337
So we would have on, off, off, which I think
566
00:25:39,337 --> 00:25:41,170
matches what I did with my three light bulbs
567
00:25:41,170 --> 00:25:43,087
as well, if you want to go and turn yours off.
568
00:25:43,087 --> 00:25:46,910
How might you three represent the number 2?
569
00:25:46,910 --> 00:25:50,420
OK, so off, on, off now, from right to left.
570
00:25:50,420 --> 00:25:54,020
How would you three represent the number 3?
571
00:25:54,020 --> 00:25:57,043
Ah, so that's why my two light bulbs went on at the end.
572
00:25:57,043 --> 00:25:58,835
How would you three represent the number 4?
573
00:25:58,835 --> 00:26:02,620
574
00:26:02,620 --> 00:26:03,120
Perfect.
575
00:26:03,120 --> 00:26:10,357
Number 5, number 6, and number 7?
576
00:26:10,357 --> 00:26:11,690
All right, and give us one more.
577
00:26:11,690 --> 00:26:14,184
How would you represent 8?
578
00:26:14,184 --> 00:26:15,000
AUDIENCE: We can't.
579
00:26:15,000 --> 00:26:15,320
DAVID J. MALAN: OK.
580
00:26:15,320 --> 00:26:16,190
You can't.
581
00:26:16,190 --> 00:26:20,250
How about then one more volunteer, one more volunteer?
582
00:26:20,250 --> 00:26:20,750
OK.
583
00:26:20,750 --> 00:26:21,470
Come on up.
584
00:26:21,470 --> 00:26:25,350
585
00:26:25,350 --> 00:26:25,850
All right.
586
00:26:25,850 --> 00:26:26,617
What's your name?
587
00:26:26,617 --> 00:26:27,950
AUDIENCE: My name is [? Moin. ?]
588
00:26:27,950 --> 00:26:28,610
DAVID J. MALAN: If you want to say it into there.
589
00:26:28,610 --> 00:26:29,390
AUDIENCE: My name is [? Moin. ?]
590
00:26:29,390 --> 00:26:30,260
DAVID J. MALAN: All right, [? and Moin, ?] you're
591
00:26:30,260 --> 00:26:33,590
going to be number 8, and if now you all-- actually, let's make this
592
00:26:33,590 --> 00:26:37,430
how would you represent number 8, all collectively, as 4 bits
593
00:26:37,430 --> 00:26:40,150
or for switches?
594
00:26:40,150 --> 00:26:42,340
OK, 8, and now lastly, give me 15.
595
00:26:42,340 --> 00:26:46,380
596
00:26:46,380 --> 00:26:49,835
Everyone's awkwardly doing arithmetic in their head, oh, using unary.
597
00:26:49,835 --> 00:26:52,940
598
00:26:52,940 --> 00:26:53,440
Yeah.
599
00:26:53,440 --> 00:26:55,150
Is that everyone--
600
00:26:55,150 --> 00:26:55,720
Yes.
601
00:26:55,720 --> 00:26:56,290
OK.
602
00:26:56,290 --> 00:26:57,670
Round of applause.
603
00:26:57,670 --> 00:26:58,180
OK.
604
00:26:58,180 --> 00:27:00,610
Thank you all.
605
00:27:00,610 --> 00:27:03,730
If you want to leave your numbers over here,
606
00:27:03,730 --> 00:27:07,107
we have a CS50 stress ball for you, but thank you for volunteering.
607
00:27:07,107 --> 00:27:09,440
You can turn those numbers off and leave them over here.
608
00:27:09,440 --> 00:27:11,500
So thank you.
609
00:27:11,500 --> 00:27:19,160
So how do we go about-- how do we go from there to creating these patterns?
610
00:27:19,160 --> 00:27:24,340
Well, even though we still had three bits, initially, and three switches,
611
00:27:24,340 --> 00:27:28,720
later four bits and four switches, ultimately, we still
612
00:27:28,720 --> 00:27:32,860
used the same approach fundamentally to actually representing information.
613
00:27:32,860 --> 00:27:35,020
And now why were they those patterns, and why
614
00:27:35,020 --> 00:27:38,150
did I very deliberately have our volunteers line up in that way?
615
00:27:38,150 --> 00:27:40,960
Well, I wanted them using base-2, a.k.a.
616
00:27:40,960 --> 00:27:44,140
binary, but with binary there comes certain rules.
617
00:27:44,140 --> 00:27:46,420
And even if you're not familiar with binary
618
00:27:46,420 --> 00:27:49,510
beyond that it exists and relates somehow to computers,
619
00:27:49,510 --> 00:27:52,330
it's actually pretty much identical to the system you and I use
620
00:27:52,330 --> 00:27:54,790
every day, known as base 10, a.k.a.
621
00:27:54,790 --> 00:27:55,520
decimal.
622
00:27:55,520 --> 00:27:58,900
So let's consider, if you will, by rewinding to primary school for just
623
00:27:58,900 --> 00:28:00,370
a moment, like how decimal works.
624
00:28:00,370 --> 00:28:03,820
And you'll see that even if you're not a computer person, you actually are.
625
00:28:03,820 --> 00:28:06,410
You just have to tweak your mental model ever so slightly.
626
00:28:06,410 --> 00:28:10,330
So here is the number that you're probably viewing as 123,
627
00:28:10,330 --> 00:28:11,510
but why is that?
628
00:28:11,510 --> 00:28:13,240
Well, it's not really 123.
629
00:28:13,240 --> 00:28:19,990
This is just a pattern of three symbols on the screen, 1, 2, 3, and your mind
630
00:28:19,990 --> 00:28:23,967
is rapidly assigning mathematical meaning to them, 123, but why is that?
631
00:28:23,967 --> 00:28:26,550
Well, if you're like me, you probably learned back in the day,
632
00:28:26,550 --> 00:28:29,800
when you have a three-digit number like, this the rightmost number
633
00:28:29,800 --> 00:28:32,800
is in the 1's place, the middle digit is the 10's place,
634
00:28:32,800 --> 00:28:35,870
the leftmost digit is in the 100's place, and why is that relevant?
635
00:28:35,870 --> 00:28:39,580
Well, if you then quickly do some mental math, as you and I just do instantly
636
00:28:39,580 --> 00:28:44,170
nowadays, that just means 100 times 1 plus 10 times 2 plus 1 times
637
00:28:44,170 --> 00:28:50,500
3, of course, 100 plus 20 plus 3 gives us the number you and I know as 123.
638
00:28:50,500 --> 00:28:55,630
But beyond that, how do we get to just two digits instead of as many as 9
639
00:28:55,630 --> 00:28:56,900
in the decimal system?
640
00:28:56,900 --> 00:28:58,150
Well, let's generalize this.
641
00:28:58,150 --> 00:29:00,610
In the decimal system, you and I know, if we've
642
00:29:00,610 --> 00:29:03,220
got three digits represented by these hashes here,
643
00:29:03,220 --> 00:29:05,920
yes, it's the 1's place, 10's place, 100's place,
644
00:29:05,920 --> 00:29:09,860
and if we keep going 1,000's, 10,000's, and so forth, but why is that?
645
00:29:09,860 --> 00:29:12,580
Well, base terminology is now a little more germane.
646
00:29:12,580 --> 00:29:17,420
That's technically the 10 to the 0th column, the 10 to the 1, 10 to the 2.
647
00:29:17,420 --> 00:29:20,650
So these are powers of 10, where 10 is your base.
648
00:29:20,650 --> 00:29:23,482
Computers just simplify things a little bit,
649
00:29:23,482 --> 00:29:25,190
because computers, at the end of the day,
650
00:29:25,190 --> 00:29:27,520
only have access to electricity, on or off.
651
00:29:27,520 --> 00:29:31,330
They don't have access to 10 different types of electricity, just 2,
652
00:29:31,330 --> 00:29:32,780
on or off, if you will.
653
00:29:32,780 --> 00:29:34,640
Well, they just use a different base.
654
00:29:34,640 --> 00:29:37,900
And the rightmost digit would be in the so-called 2 to the 0ths.
655
00:29:37,900 --> 00:29:39,850
Then the middle digit is 2 to the 1.
656
00:29:39,850 --> 00:29:41,830
The left most is 2 to the 2, a.k.a.
657
00:29:41,830 --> 00:29:45,520
1's place, 2's place, 4's place, and as we kept going, 8,
658
00:29:45,520 --> 00:29:49,990
and if we keep going, 16, 32, 64, 128, and so forth,
659
00:29:49,990 --> 00:29:51,920
but the idea is fundamentally the same.
660
00:29:51,920 --> 00:29:55,270
So why is this how the computer represents
661
00:29:55,270 --> 00:29:56,770
the number you and I know is 0?
662
00:29:56,770 --> 00:30:01,900
Well, off, off, off, from right to left or in this case left to right,
663
00:30:01,900 --> 00:30:02,750
is just 0.
664
00:30:02,750 --> 00:30:03,250
Why?
665
00:30:03,250 --> 00:30:08,320
Because that's 4 times 0 plus 2 times 0 plus 1 times 0 is, of course, 0.
666
00:30:08,320 --> 00:30:12,400
This is why 001 represents 1.
667
00:30:12,400 --> 00:30:20,320
This is why 010 represents 2 and 3 and 4 and 5 and 6 and 7 on up.
668
00:30:20,320 --> 00:30:23,500
And why did we need a 4th bit to represent 8?
669
00:30:23,500 --> 00:30:26,570
Well, we kind of needed to carry the 1, so to speak,
670
00:30:26,570 --> 00:30:29,740
using our familiar human terminology.
671
00:30:29,740 --> 00:30:33,280
But for that we need a 4th bit, another transistor, and this
672
00:30:33,280 --> 00:30:34,630
now represents the number 8.
673
00:30:34,630 --> 00:30:39,070
And that's why we ended with on-- from left to right-- off, off, off.
674
00:30:39,070 --> 00:30:42,760
So I keep saying on and off, or the light bulb is on or off,
675
00:30:42,760 --> 00:30:45,310
but really, I just mean 1 or 0.
676
00:30:45,310 --> 00:30:48,700
And so computers and we humans think of things digitally
677
00:30:48,700 --> 00:30:50,860
as just being 0's and 1's, but mechanically,
678
00:30:50,860 --> 00:30:52,860
you can think of it indeed is these light bulbs.
679
00:30:52,860 --> 00:30:54,220
Now, a bit is not very useful.
680
00:30:54,220 --> 00:30:56,300
Even 3 bits, 4 bits, not that useful.
681
00:30:56,300 --> 00:30:58,780
You can count to 7 or 15, generally speaking,
682
00:30:58,780 --> 00:31:01,240
bytes are a more useful unit of measure.
683
00:31:01,240 --> 00:31:04,190
And anyone familiar how many bits is in a byte?
684
00:31:04,190 --> 00:31:04,690
Yeah.
685
00:31:04,690 --> 00:31:06,310
So 8 bits are in a byte.
686
00:31:06,310 --> 00:31:08,557
You can think of it as an octet equivalently.
687
00:31:08,557 --> 00:31:11,140
In some contexts, there are nuances there, but think of a byte
688
00:31:11,140 --> 00:31:14,200
as just being 8 bits, and that's just a more useful measure.
689
00:31:14,200 --> 00:31:15,890
So what does this mean in real terms?
690
00:31:15,890 --> 00:31:21,430
So if you've ever downloaded like a music file or a photograph or a video,
691
00:31:21,430 --> 00:31:22,990
those are measured in bytes.
692
00:31:22,990 --> 00:31:25,150
Probably not small numbers of bytes, probably
693
00:31:25,150 --> 00:31:28,630
kilobytes for thousands of bytes, megabytes for millions of bytes,
694
00:31:28,630 --> 00:31:32,290
gigabytes for billions of bytes, especially for video.
695
00:31:32,290 --> 00:31:35,470
That just means you have a lot of patterns
696
00:31:35,470 --> 00:31:39,670
of 8 bits, some combination of 0's and 1's on your computer's hard drive.
697
00:31:39,670 --> 00:31:44,830
Here then, with a byte of bits, 8 bits, is how a computer would typically
698
00:31:44,830 --> 00:31:46,330
represent the number 0.
699
00:31:46,330 --> 00:31:49,570
And if that same computer uses all 8 of its bits,
700
00:31:49,570 --> 00:31:52,570
its full byte, to change it to 1--
701
00:31:52,570 --> 00:31:54,850
anyone who's quick with math or have seen this before,
702
00:31:54,850 --> 00:31:58,247
how high can a computer count with 8 bits or 1?
703
00:31:58,247 --> 00:31:59,080
[INTERPOSING VOICES]
704
00:31:59,080 --> 00:32:00,400
Yeah, 255.
705
00:32:00,400 --> 00:32:00,902
Why is that?
706
00:32:00,902 --> 00:32:03,610
Well, we're not going to turn this into a constant math exercise.
707
00:32:03,610 --> 00:32:05,402
Indeed, after today, we're not really going
708
00:32:05,402 --> 00:32:08,230
to think about or talk about bits at this low level.
709
00:32:08,230 --> 00:32:14,560
But this is the 1's place, 2's, 4's, 8s, 16, 32, 64, 128,
710
00:32:14,560 --> 00:32:19,520
and if I do all of that math from left to right, that indeed gives me 255.
711
00:32:19,520 --> 00:32:22,210
It ignores how we might represent negative numbers, but perhaps
712
00:32:22,210 --> 00:32:24,160
more on those some other day.
713
00:32:24,160 --> 00:32:25,960
But computers, of course, do so much more
714
00:32:25,960 --> 00:32:28,810
than numbers and math and all this low level stuff.
715
00:32:28,810 --> 00:32:32,150
We send text messages, write documents, emails, and the like.
716
00:32:32,150 --> 00:32:36,220
So how might a computer represent something like the letter A?
717
00:32:36,220 --> 00:32:38,980
I claim, at the end of the day, your Mac, your PC,
718
00:32:38,980 --> 00:32:42,430
your phone just has lots of transistors, lots of switches
719
00:32:42,430 --> 00:32:46,210
that it can use in units of 8, in units of bytes.
720
00:32:46,210 --> 00:32:49,900
How, though, if it's already using those patterns of 0's and 1's
721
00:32:49,900 --> 00:32:53,380
apparently to represent numbers from 0 on up,
722
00:32:53,380 --> 00:32:58,440
how do you go about representing letters of the alphabet, might you think?
723
00:32:58,440 --> 00:32:59,230
Yeah?
724
00:32:59,230 --> 00:32:59,730
OK.
725
00:32:59,730 --> 00:33:01,800
So we could assign a number to every letter.
726
00:33:01,800 --> 00:33:01,920
OK.
727
00:33:01,920 --> 00:33:03,810
So let me just conjecture, well, let's just
728
00:33:03,810 --> 00:33:08,760
call A 0, for simplicity, B 1, C 2, and now let me play devil's advocate.
729
00:33:08,760 --> 00:33:13,500
OK, how do I now represent 0 or 1 or 2?
730
00:33:13,500 --> 00:33:16,080
Well, we've maybe created a problem for ourselves,
731
00:33:16,080 --> 00:33:19,050
if now we have to steal some numbers to represent letters.
732
00:33:19,050 --> 00:33:21,060
We kind of have to pick a lane, but there's a solution to that
733
00:33:21,060 --> 00:33:21,870
too that we'll see.
734
00:33:21,870 --> 00:33:25,710
And it turns out the world is not quite as simple as A being 0.
735
00:33:25,710 --> 00:33:29,100
A typically is represented, by computers everywhere, phones everywhere,
736
00:33:29,100 --> 00:33:32,640
with the number 65, the decimal number 65.
737
00:33:32,640 --> 00:33:36,690
Using 8 bits, if we turn some of the 0's to 1's, let me just stipulate,
738
00:33:36,690 --> 00:33:41,220
you can represent the letter A using 8 bits, by turning certain ones on
739
00:33:41,220 --> 00:33:45,240
and certain ones off, but we will try not to focus on that binary level
740
00:33:45,240 --> 00:33:46,000
too much.
741
00:33:46,000 --> 00:33:49,740
So if A is 65, it turns out that B is going to be 66,
742
00:33:49,740 --> 00:33:54,060
and C is going to be 67, and so forth, and so where does that get us?
743
00:33:54,060 --> 00:33:59,130
Well, it turns out there's a whole system that maps numbers to letters.
744
00:33:59,130 --> 00:34:02,010
And here, as I alluded to verbally a moment ago,
745
00:34:02,010 --> 00:34:05,130
is the pattern of 0's and 1's via which you'd represent 65.
746
00:34:05,130 --> 00:34:07,770
And just quick check here, we won't constantly
747
00:34:07,770 --> 00:34:13,770
do math 1's place, that's easy, 2's, 4's, 8's, 16's, 32's, 64's place.
748
00:34:13,770 --> 00:34:17,969
So 64 plus 1 gives us 65.
749
00:34:17,969 --> 00:34:21,540
So once I do that, how do I get to all of the others?
750
00:34:21,540 --> 00:34:25,020
Well, it turns out a bunch of Americans years ago came up with this
751
00:34:25,020 --> 00:34:28,889
ASCII, the American Standard Code for Information Interchange.
752
00:34:28,889 --> 00:34:30,344
Now, what does that mean?
753
00:34:30,344 --> 00:34:32,969
Well, it's just an acronym describing what really you proposed,
754
00:34:32,969 --> 00:34:37,620
a mapping between numbers and letters, not quite as simple as 0, 1, 2.
755
00:34:37,620 --> 00:34:40,980
Starts at 65, 66, 67 for capital letters,
756
00:34:40,980 --> 00:34:45,610
but here are most of the letters in use today, at least with this system.
757
00:34:45,610 --> 00:34:47,969
So this is just a big chart from online, and you'll
758
00:34:47,969 --> 00:34:52,800
see in the middle of this chart, here, here's my 65, A. Here's my 66,
759
00:34:52,800 --> 00:34:58,750
B, C, and let's see, 72 is H, 73 is I, and so forth.
760
00:34:58,750 --> 00:35:01,890
So there's a mapping, at least for English, between all of these numbers
761
00:35:01,890 --> 00:35:03,160
and all of these letters.
762
00:35:03,160 --> 00:35:07,080
And if we focus here, those are the beginning of our uppercase alphabet.
763
00:35:07,080 --> 00:35:11,670
So suppose then that today, tomorrow, you receive a text message
764
00:35:11,670 --> 00:35:15,630
from someone, and underneath the hood, now that you're a computer person,
765
00:35:15,630 --> 00:35:19,110
you figure out a way to see what pattern of 0's and 1's was sent.
766
00:35:19,110 --> 00:35:21,300
In this case, it's wireless as opposed to wired,
767
00:35:21,300 --> 00:35:23,147
but it's still some pattern of 0's and 1's.
768
00:35:23,147 --> 00:35:25,230
And your phone is turning some switches of its own
769
00:35:25,230 --> 00:35:27,900
on and off to represent that message from a friend.
770
00:35:27,900 --> 00:35:30,630
Suppose that the three patterns you received
771
00:35:30,630 --> 00:35:33,360
were these three bytes, from left to right,
772
00:35:33,360 --> 00:35:35,430
spelling out a three-letter word.
773
00:35:35,430 --> 00:35:38,880
Well, if we do out the math, 1's place, 2's place, and so forth--
774
00:35:38,880 --> 00:35:40,060
I'll spoil it for you.
775
00:35:40,060 --> 00:35:45,420
Suppose that you received a text message that doesn't literally say 72, 73, 33,
776
00:35:45,420 --> 00:35:49,410
but you've received a pattern of 8 plus 8 plus 8, 24 bits
777
00:35:49,410 --> 00:35:54,510
that if you do out the math represent the decimal number 72, 73, 33.
778
00:35:54,510 --> 00:35:57,270
Anyone recall what message you might have received
779
00:35:57,270 --> 00:35:58,980
from the green and white charts?
780
00:35:58,980 --> 00:35:59,970
Yeah?
781
00:35:59,970 --> 00:36:00,930
AUDIENCE: Hi.
782
00:36:00,930 --> 00:36:02,010
DAVID J. MALAN: Hi.
783
00:36:02,010 --> 00:36:08,370
Yes, hi is the message, but 72, 73 gives us H and I. What's 33?
784
00:36:08,370 --> 00:36:10,560
Any guesses to 33?
785
00:36:10,560 --> 00:36:12,100
Yeah, over here.
786
00:36:12,100 --> 00:36:12,600
Yeah.
787
00:36:12,600 --> 00:36:13,770
So it's an exclamation point.
788
00:36:13,770 --> 00:36:14,650
How would you know that?
789
00:36:14,650 --> 00:36:17,025
Well, you really do need some kind of cheat sheet, a.k.a.
790
00:36:17,025 --> 00:36:18,010
ASCII in this case.
791
00:36:18,010 --> 00:36:20,802
And if we look elsewhere-- let me highlight the left of the chart--
792
00:36:20,802 --> 00:36:24,490
you can see that next to 33 in decimal is indeed the exclamation point.
793
00:36:24,490 --> 00:36:27,120
So back in the day, a bunch of humans got in a room,
794
00:36:27,120 --> 00:36:31,200
decided that, hey, when we start building PCs and later Macs and phones,
795
00:36:31,200 --> 00:36:35,550
we all just have to agree on this form of representation of letters
796
00:36:35,550 --> 00:36:38,110
of the English alphabet, in this case.
797
00:36:38,110 --> 00:36:39,810
We just need to agree on this mapping.
798
00:36:39,810 --> 00:36:42,910
But somewhat curiously, notice this.
799
00:36:42,910 --> 00:36:46,380
It turns out that, once you paint yourself into this corner
800
00:36:46,380 --> 00:36:51,210
and start using 65 for A, 66 for B, well,
801
00:36:51,210 --> 00:36:54,660
how do you represent 65 the number and 66 the number,
802
00:36:54,660 --> 00:36:57,690
if you want to do math or use Excel or something like that?
803
00:36:57,690 --> 00:37:01,380
Does anyone see the solution, perhaps?
804
00:37:01,380 --> 00:37:06,070
How do you represent the number 1 in ASCII?
805
00:37:06,070 --> 00:37:07,528
Yeah, in the middle?
806
00:37:07,528 --> 00:37:09,065
AUDIENCE: [INAUDIBLE].
807
00:37:09,065 --> 00:37:09,940
DAVID J. MALAN: Yeah.
808
00:37:09,940 --> 00:37:12,955
So this is getting a little maybe inception or something,
809
00:37:12,955 --> 00:37:15,080
but you could represent numbers with other numbers.
810
00:37:15,080 --> 00:37:16,830
And so if you want to represent the number
811
00:37:16,830 --> 00:37:19,450
you and I know as 1, like when you type it on your keyboard,
812
00:37:19,450 --> 00:37:23,230
turns out the computer stores that as the decimal number 49.
813
00:37:23,230 --> 00:37:27,070
If you hit 2 on your keyboard, the computer is not storing 2, per se.
814
00:37:27,070 --> 00:37:29,230
It's storing the decimal number 50.
815
00:37:29,230 --> 00:37:33,940
Now, thankfully, the paradox stops there.
816
00:37:33,940 --> 00:37:36,340
We just have a mapping now of numbers to numbers.
817
00:37:36,340 --> 00:37:37,780
But really, at the end of the day-- and you're
818
00:37:37,780 --> 00:37:40,822
going to learn this when we start writing code in that other language, C,
819
00:37:40,822 --> 00:37:43,720
next week-- it's just context-dependent, at the end of the day.
820
00:37:43,720 --> 00:37:47,380
Inside of your Mac, PC, and phone, there's just all of these permutations
821
00:37:47,380 --> 00:37:49,720
of bits, all of these patterns of 0's and 1's.
822
00:37:49,720 --> 00:37:53,650
And generally speaking, when you open up a text message that you've received
823
00:37:53,650 --> 00:37:55,252
from someone, it's 0's and 1's.
824
00:37:55,252 --> 00:37:58,210
But obviously, if it's a text message, the whole point of text messages
825
00:37:58,210 --> 00:38:01,660
is to send text, and so those patterns of 0's and 1's, by default,
826
00:38:01,660 --> 00:38:04,862
will typically be interpreted as letters of the alphabet.
827
00:38:04,862 --> 00:38:06,070
So you won't see 0's and 1's.
828
00:38:06,070 --> 00:38:07,320
You won't see decimal numbers.
829
00:38:07,320 --> 00:38:10,090
You'll see the English message that your friend intended.
830
00:38:10,090 --> 00:38:13,390
By contrast, if you open up something like Excel,
831
00:38:13,390 --> 00:38:19,660
that same pattern 0's zeros and 1's might indeed work out to be 72, 73, 33.
832
00:38:19,660 --> 00:38:23,530
You might see cells in your spreadsheet with literally those three numbers.
833
00:38:23,530 --> 00:38:24,040
Why?
834
00:38:24,040 --> 00:38:26,248
Because spreadsheets are all about numbers and number
835
00:38:26,248 --> 00:38:28,210
crunching and math, in many cases.
836
00:38:28,210 --> 00:38:31,420
If by contrast, you open up Photoshop and try
837
00:38:31,420 --> 00:38:36,820
to look at that same pattern of 0's and 1's, it's not going to be 72, 73, 33.
838
00:38:36,820 --> 00:38:38,530
It's not going to be 0's and 1's.
839
00:38:38,530 --> 00:38:39,670
It's not going to be hi.
840
00:38:39,670 --> 00:38:42,100
It's going to be some color of the rainbow.
841
00:38:42,100 --> 00:38:44,530
You're going to use those patterns of 0's and 1's, it
842
00:38:44,530 --> 00:38:46,810
turns out too, to represent colors.
843
00:38:46,810 --> 00:38:50,470
And indeed, so long as you and I just agree, as humans long
844
00:38:50,470 --> 00:38:53,830
have, what these patterns are going to be, all of our systems,
845
00:38:53,830 --> 00:38:56,560
many of our systems nowadays are indeed interoperable.
846
00:38:56,560 --> 00:38:59,590
But I'm being very biased here, and indeed, the A and ASCII
847
00:38:59,590 --> 00:39:01,330
is very American-centric.
848
00:39:01,330 --> 00:39:03,940
What do you not see in this chart?
849
00:39:03,940 --> 00:39:06,100
If you speak any other language than English,
850
00:39:06,100 --> 00:39:09,400
odds are, you're not seeing characters you know and love and need
851
00:39:09,400 --> 00:39:11,620
every day to type or send messages.
852
00:39:11,620 --> 00:39:13,480
Well, there's a huge character set that's
853
00:39:13,480 --> 00:39:15,962
not supported here, whether it's accented characters
854
00:39:15,962 --> 00:39:17,170
and a lot of Asian alphabets.
855
00:39:17,170 --> 00:39:20,420
You have many more symbols than can fit even on this screen here.
856
00:39:20,420 --> 00:39:24,010
And so humans kind of painted themselves into a corner, early on, or really,
857
00:39:24,010 --> 00:39:24,970
Americans did.
858
00:39:24,970 --> 00:39:27,820
But on a typical keyboard, US English keyboard, yeah, you
859
00:39:27,820 --> 00:39:31,450
have A's and B's and C's, uppercase and lowercase,
860
00:39:31,450 --> 00:39:33,880
but you also have accented characters here.
861
00:39:33,880 --> 00:39:37,900
And nowadays, not sure if this is maybe necessary, but nowadays,
862
00:39:37,900 --> 00:39:41,090
you have other characters on your keyboard, like these.
863
00:39:41,090 --> 00:39:43,780
And these are a playful incarnation of what's actually
864
00:39:43,780 --> 00:39:45,730
a technical solution to this problem.
865
00:39:45,730 --> 00:39:50,397
If I claim for the moment that ASCII historically used 7 bits to represent
866
00:39:50,397 --> 00:39:52,480
letters-- and let's just round that up to a byte--
867
00:39:52,480 --> 00:39:57,730
8 bits to represent letters, ASCII can represent as many as 255,
868
00:39:57,730 --> 00:40:00,520
or really 256, total characters.
869
00:40:00,520 --> 00:40:01,550
Why 256?
870
00:40:01,550 --> 00:40:04,840
Well, if you have them all 0, that's 0, and the highest number
871
00:40:04,840 --> 00:40:07,070
I claimed a moment ago was 255.
872
00:40:07,070 --> 00:40:09,160
So that's 256 total possibilities.
873
00:40:09,160 --> 00:40:10,360
That's not many letters.
874
00:40:10,360 --> 00:40:13,730
It's fine for English, but not a lot of human languages.
875
00:40:13,730 --> 00:40:16,210
So what might the intuitive solution be, if you
876
00:40:16,210 --> 00:40:21,460
want to represent accented characters, Asian characters, emoji, even
877
00:40:21,460 --> 00:40:25,360
like these, which are just keys on a keyboard nowadays?
878
00:40:25,360 --> 00:40:28,610
What's the intuitive solution, if a byte's too few?
879
00:40:28,610 --> 00:40:29,110
Yeah?
880
00:40:29,110 --> 00:40:30,320
AUDIENCE: Add another digit.
881
00:40:30,320 --> 00:40:30,710
DAVID J. MALAN: Yeah.
882
00:40:30,710 --> 00:40:31,610
So add another digit.
883
00:40:31,610 --> 00:40:34,850
Just like we had a 4th volunteer come on up to give us a 4th bid,
884
00:40:34,850 --> 00:40:37,640
let's just throw hardware at the problem and use a few more bits.
885
00:40:37,640 --> 00:40:41,925
So maybe instead of 1 byte, let's use 2, or heck, let's use 3 or 4 bytes.
886
00:40:41,925 --> 00:40:43,800
Even though it's getting a little expensive--
887
00:40:43,800 --> 00:40:47,000
we're going from 8 to 16 to 24 or 32 bits--
888
00:40:47,000 --> 00:40:49,047
that's how computers do things, these days.
889
00:40:49,047 --> 00:40:52,130
And thankfully, we have so much memory inside of our computers and phones,
890
00:40:52,130 --> 00:40:54,710
we can certainly spare a few to represent these things.
891
00:40:54,710 --> 00:40:58,400
And the solution then to ASCII is what we'll call Unicode.
892
00:40:58,400 --> 00:41:04,730
So Unicode is also just a mapping of numbers to letters
893
00:41:04,730 --> 00:41:06,380
but in many different languages.
894
00:41:06,380 --> 00:41:08,450
And indeed, the Unicode Consortium is a bunch
895
00:41:08,450 --> 00:41:12,200
of people from all different companies--
896
00:41:12,200 --> 00:41:16,610
a lot of different companies and countries and cultures whose mission,
897
00:41:16,610 --> 00:41:22,460
as an organization, is to capture digitally all forms of human language
898
00:41:22,460 --> 00:41:23,070
in this case.
899
00:41:23,070 --> 00:41:26,360
And to ensure that especially smaller demographics of humans speaking
900
00:41:26,360 --> 00:41:29,960
lesser-known languages are nonetheless represented and preserved digitally
901
00:41:29,960 --> 00:41:32,600
using some mapping of these 0's and 1's.
902
00:41:32,600 --> 00:41:36,590
It turns out, though, if you start using 32 bits, as many as 32 bits,
903
00:41:36,590 --> 00:41:42,020
to represent characters on a keyboard, that's 4 billion possible permutations
904
00:41:42,020 --> 00:41:42,830
of 0's and 1's.
905
00:41:42,830 --> 00:41:45,538
That's way more than we need for most human languages.
906
00:41:45,538 --> 00:41:48,830
So there's a little bit of room in there for some of those more playful things,
907
00:41:48,830 --> 00:41:49,850
like those emoji.
908
00:41:49,850 --> 00:41:52,760
So for instance, suppose you got a text message with this pattern
909
00:41:52,760 --> 00:41:55,040
of 0's and 1's.
910
00:41:55,040 --> 00:41:57,950
Or if we do out the math, suppose you receive a text message
911
00:41:57,950 --> 00:42:03,080
that, if you do out the math in decimal, is 4,036,991,106.
912
00:42:03,080 --> 00:42:07,110
Anyone know what emoji you're looking at?
913
00:42:07,110 --> 00:42:11,033
It would be weird if you do, but what is this?
914
00:42:11,033 --> 00:42:12,950
Well, it turns out that, as of this past year,
915
00:42:12,950 --> 00:42:16,990
this is the most popular emoji to be sent by many measures,
916
00:42:16,990 --> 00:42:18,970
Face with Tears of Joy.
917
00:42:18,970 --> 00:42:22,540
So that is the pattern that a bunch of humans in the Unicode Consortium
918
00:42:22,540 --> 00:42:24,070
decided would represent this.
919
00:42:24,070 --> 00:42:27,430
But you'll notice, many of you might have iPhones, some of you
920
00:42:27,430 --> 00:42:30,280
might have Android devices too, and sometimes, these
921
00:42:30,280 --> 00:42:31,810
don't actually look quite the same.
922
00:42:31,810 --> 00:42:36,850
This happens to be the current version of Face with Tears of Joy on iOS.
923
00:42:36,850 --> 00:42:40,150
On Android, it tends to look a little something more like this,
924
00:42:40,150 --> 00:42:42,550
and here is kind of a curiosity.
925
00:42:42,550 --> 00:42:45,850
Even though you and I look at these things and they look like images,
926
00:42:45,850 --> 00:42:46,840
they're not images.
927
00:42:46,840 --> 00:42:50,470
They're characters, at least as we've defined them now in Unicode.
928
00:42:50,470 --> 00:42:56,350
And iOS and Android and Windows and Facebook and other companies and apps,
929
00:42:56,350 --> 00:42:59,325
nowadays, really just have different fonts, if you will.
930
00:42:59,325 --> 00:43:01,450
So just like fonts with English and other languages
931
00:43:01,450 --> 00:43:04,690
can give you different characters with serifs or not,
932
00:43:04,690 --> 00:43:07,958
emoji are themselves, yes, drawings that someone made,
933
00:43:07,958 --> 00:43:09,250
but they're really just a font.
934
00:43:09,250 --> 00:43:13,120
And so that same pattern of 0's and 1's might just render slightly differently
935
00:43:13,120 --> 00:43:14,830
on someone's phone or another.
936
00:43:14,830 --> 00:43:17,950
If you've ever gotten like an icon on your phone that's broken,
937
00:43:17,950 --> 00:43:21,610
and you've been sent an emoji, but it's like a square or something arbitrary
938
00:43:21,610 --> 00:43:24,700
and not sensible, it might just mean that you have not
939
00:43:24,700 --> 00:43:27,070
updated to the latest version of iOS or Android,
940
00:43:27,070 --> 00:43:29,710
which just updates the font of supported emoji.
941
00:43:29,710 --> 00:43:32,290
Because those folks at Unicode, pretty much every year
942
00:43:32,290 --> 00:43:36,838
nowadays, are adding more and more emoji to that particular character set.
943
00:43:36,838 --> 00:43:39,880
Now, I went down the rabbit hole of figuring out the other day just which
944
00:43:39,880 --> 00:43:41,710
are the most popular emoji these days.
945
00:43:41,710 --> 00:43:47,120
On Twitter specifically, this past year, the most popular emoji, by contrast,
946
00:43:47,120 --> 00:43:49,390
was Loudly Crying Face.
947
00:43:49,390 --> 00:43:52,780
I don't know if that says more about 2021 or about Twitter,
948
00:43:52,780 --> 00:43:55,780
but you'll see different trends, certainly, in how these are used.
949
00:43:55,780 --> 00:44:00,400
But even humans themselves didn't necessarily think two steps ahead,
950
00:44:00,400 --> 00:44:03,790
and now a lot of the emoji are the default yellow color.
951
00:44:03,790 --> 00:44:06,630
But there's a lot of emoji that aren't these cartoon characters,
952
00:44:06,630 --> 00:44:09,130
but they're meant to represent humans in various professions
953
00:44:09,130 --> 00:44:10,690
or gestures or the like.
954
00:44:10,690 --> 00:44:14,830
And nowadays too, you've probably noticed on your phone and Macs and PCs,
955
00:44:14,830 --> 00:44:18,190
there are different skin tones that you can assign to certain emojis.
956
00:44:18,190 --> 00:44:21,190
If it's supported by the company and by Unicode,
957
00:44:21,190 --> 00:44:23,858
you can actually touch and hold on a certain emoji,
958
00:44:23,858 --> 00:44:25,900
and then you can choose the appropriate skin tone
959
00:44:25,900 --> 00:44:27,483
to represent yourself or someone else.
960
00:44:27,483 --> 00:44:29,440
And that then modifies the display.
961
00:44:29,440 --> 00:44:31,210
Well, let's just think for a moment here,
962
00:44:31,210 --> 00:44:33,910
how did Apple and Google and Microsoft and others
963
00:44:33,910 --> 00:44:38,650
go about implementing support for emoji with different skin tones?
964
00:44:38,650 --> 00:44:41,000
How could you do this?
965
00:44:41,000 --> 00:44:44,563
If you want to represent some smiling emoji but in five,
966
00:44:44,563 --> 00:44:47,230
in this case, different skin tones, you could come up with what?
967
00:44:47,230 --> 00:44:50,740
Five different patterns that are identical, structurally,
968
00:44:50,740 --> 00:44:53,950
except for the skin tone used in places in that image.
969
00:44:53,950 --> 00:44:58,360
But that's a little inefficient to just do copy, paste, paste, paste, paste,
970
00:44:58,360 --> 00:45:00,890
and change the color in Photoshop, if you will.
971
00:45:00,890 --> 00:45:05,020
That's going to use more bits, more information than you might need to.
972
00:45:05,020 --> 00:45:07,575
How else, if you now start to think a little bit more
973
00:45:07,575 --> 00:45:10,450
like a computer scientist, if at the end of the day, all you have are
974
00:45:10,450 --> 00:45:14,180
0's and 1's, how else could you implement skin tones, might you think?
975
00:45:14,180 --> 00:45:14,680
Yeah?
976
00:45:14,680 --> 00:45:15,298
AUDIENCE: RGB.
977
00:45:15,298 --> 00:45:16,090
DAVID J. MALAN: OK.
978
00:45:16,090 --> 00:45:18,190
So RGB, we'll come to that in just a moment.
979
00:45:18,190 --> 00:45:19,630
That stands for Red, Green, Blue.
980
00:45:19,630 --> 00:45:20,590
That's one way.
981
00:45:20,590 --> 00:45:23,590
In this case, though, I'm seeking an alternative
982
00:45:23,590 --> 00:45:28,390
to just using five different patterns of 0's and 1's to represent the same emoji
983
00:45:28,390 --> 00:45:29,720
but different skin tones.
984
00:45:29,720 --> 00:45:30,620
So not quite RGB.
985
00:45:30,620 --> 00:45:31,120
Yeah?
986
00:45:31,120 --> 00:45:32,278
AUDIENCE: [INAUDIBLE]
987
00:45:32,278 --> 00:45:33,070
DAVID J. MALAN: OK.
988
00:45:33,070 --> 00:45:37,660
So store one copy of the emoji and then store different variants of the color
989
00:45:37,660 --> 00:45:39,410
that you want to assign to that emoji.
990
00:45:39,410 --> 00:45:39,910
Yeah.
991
00:45:39,910 --> 00:45:42,452
So this is actually an example of-- do you want to elaborate?
992
00:45:42,452 --> 00:45:43,858
AUDIENCE: You could use a loop.
993
00:45:43,858 --> 00:45:44,650
DAVID J. MALAN: OK.
994
00:45:44,650 --> 00:45:46,900
So you can use a loop to actually output these things.
995
00:45:46,900 --> 00:45:47,942
More on that in a moment.
996
00:45:47,942 --> 00:45:49,990
Let me go down this road for just a moment.
997
00:45:49,990 --> 00:45:54,335
This would be in some sense a better design, if you will, but why?
998
00:45:54,335 --> 00:45:54,835
Yeah?
999
00:45:54,835 --> 00:45:55,798
AUDIENCE: A filter?
1000
00:45:55,798 --> 00:45:56,590
DAVID J. MALAN: OK.
1001
00:45:56,590 --> 00:45:58,780
So filter, if we think of in the Instagram sense.
1002
00:45:58,780 --> 00:46:01,885
You can change the color of something, and that could be related here too.
1003
00:46:01,885 --> 00:46:03,967
AUDIENCE: Could it be another font [INAUDIBLE]
1004
00:46:03,967 --> 00:46:05,300
DAVID J. MALAN: Oh, interesting.
1005
00:46:05,300 --> 00:46:07,410
So maybe it could be just a completely different font.
1006
00:46:07,410 --> 00:46:09,380
And you have five different fonts that are almost identical,
1007
00:46:09,380 --> 00:46:12,800
except for the various interpretations of skin tone for those same emoji.
1008
00:46:12,800 --> 00:46:13,550
Let me spoil.
1009
00:46:13,550 --> 00:46:15,680
I think if we go down this one particular road,
1010
00:46:15,680 --> 00:46:20,210
the way the Unicode folks decided to do this some years ago where
1011
00:46:20,210 --> 00:46:25,130
the first byte or bytes that you receive via text or email
1012
00:46:25,130 --> 00:46:28,520
just represent like the structure of the emoji, the default yellow version,
1013
00:46:28,520 --> 00:46:29,060
thereof.
1014
00:46:29,060 --> 00:46:32,540
But if it's immediately followed by a certain pattern of bits
1015
00:46:32,540 --> 00:46:34,970
that these humans standardize to represent
1016
00:46:34,970 --> 00:46:39,170
each of these different shades of skin tone, then the phone, the Mac,
1017
00:46:39,170 --> 00:46:42,920
the PC will change that default color, yellow in most cases,
1018
00:46:42,920 --> 00:46:45,290
to whatever the more apt human tone is.
1019
00:46:45,290 --> 00:46:49,970
So you just use twice as many bits, but you don't use five times as many bits.
1020
00:46:49,970 --> 00:46:50,720
So what do I mean?
1021
00:46:50,720 --> 00:46:54,890
You don't have five completely distinct patterns, per se.
1022
00:46:54,890 --> 00:46:59,720
For each of these possible variants, you have a representation
1023
00:46:59,720 --> 00:47:03,710
of just the emoji itself, structurally, and then
1024
00:47:03,710 --> 00:47:07,920
reusable patterns for those five skin tones.
1025
00:47:07,920 --> 00:47:11,570
Unfortunately, that wasn't quite versatile enough for other features
1026
00:47:11,570 --> 00:47:13,850
that were in the pipeline, and nowadays too,
1027
00:47:13,850 --> 00:47:16,100
and there's a double meaning now to representation.
1028
00:47:16,100 --> 00:47:20,900
Emojis intended to focus on certain professions, and early on too,
1029
00:47:20,900 --> 00:47:24,150
were certain professions associated with certain genders and vice versa.
1030
00:47:24,150 --> 00:47:26,612
And you couldn't necessarily be one gender or another,
1031
00:47:26,612 --> 00:47:28,070
in a certain profession or another.
1032
00:47:28,070 --> 00:47:30,230
There were these combinatorics that just weren't possible.
1033
00:47:30,230 --> 00:47:31,970
But nowadays, as you might have seen, you
1034
00:47:31,970 --> 00:47:33,920
can have couples in love for instance that
1035
00:47:33,920 --> 00:47:37,200
actually look a little more like three emojis, but just kind of combined
1036
00:47:37,200 --> 00:47:37,700
into one.
1037
00:47:37,700 --> 00:47:39,980
And indeed, this is just one key press on your phone,
1038
00:47:39,980 --> 00:47:43,550
and you can combine different emoji on the left and then the right
1039
00:47:43,550 --> 00:47:45,390
with the emoji in the middle.
1040
00:47:45,390 --> 00:47:47,480
And so it turns out how computers nowadays
1041
00:47:47,480 --> 00:47:52,940
represent these patterns are one set of bits for the character on the left,
1042
00:47:52,940 --> 00:47:54,920
one set of bits for a character on the right,
1043
00:47:54,920 --> 00:47:58,080
one set of bits for whatever emoji you want in the middle.
1044
00:47:58,080 --> 00:48:02,450
And then you assemble more complicated compositions of emoji
1045
00:48:02,450 --> 00:48:06,350
by just reusing those same patterns and bits and bits.
1046
00:48:06,350 --> 00:48:10,160
The Unicode folks don't have to come up with a whole new representation
1047
00:48:10,160 --> 00:48:12,620
for some very specific incarnation.
1048
00:48:12,620 --> 00:48:15,080
They can create one for person, for male,
1049
00:48:15,080 --> 00:48:18,110
for, female for other characters that you might want to display,
1050
00:48:18,110 --> 00:48:21,390
and reuse those same patterns of 0's and 1's.
1051
00:48:21,390 --> 00:48:23,632
And so here, you see the imperfection, of or lack
1052
00:48:23,632 --> 00:48:26,090
of foresight, of humans for building a system early on that
1053
00:48:26,090 --> 00:48:29,390
was entirely American-centric, no characters, emoji, or the like,
1054
00:48:29,390 --> 00:48:30,500
that's evolved too.
1055
00:48:30,500 --> 00:48:33,800
And so that's an important detail in computing, nowadays.
1056
00:48:33,800 --> 00:48:36,020
It too is evolving, and the languages you're
1057
00:48:36,020 --> 00:48:39,780
about to learn in the coming days, those too are evolving as well.
1058
00:48:39,780 --> 00:48:42,980
And new features are getting added, and even programming languages have
1059
00:48:42,980 --> 00:48:43,942
version numbers.
1060
00:48:43,942 --> 00:48:46,400
You might have a different version of an app on your phone.
1061
00:48:46,400 --> 00:48:49,770
Programming languages too have different versions as well.
1062
00:48:49,770 --> 00:48:51,830
Questions then thus far on how information
1063
00:48:51,830 --> 00:48:56,900
is represented using ASCII or Unicode or anything in between?
1064
00:48:56,900 --> 00:48:57,440
Yeah?
1065
00:48:57,440 --> 00:48:59,383
AUDIENCE: [INAUDIBLE]
1066
00:48:59,383 --> 00:49:00,800
DAVID J. MALAN: Oh, good question.
1067
00:49:00,800 --> 00:49:04,240
So to recap, why can't you just--
1068
00:49:04,240 --> 00:49:07,390
well, let me summarize that as why can't you
1069
00:49:07,390 --> 00:49:10,843
similarly use different patterns to change the context of what
1070
00:49:10,843 --> 00:49:12,760
these patterns of bits represent, whether it's
1071
00:49:12,760 --> 00:49:14,950
a number or a letter or a graphic?
1072
00:49:14,950 --> 00:49:17,920
In actuality, that's kind of what's happening underneath the hood.
1073
00:49:17,920 --> 00:49:21,220
It's not standardized in quite the same way, but starting next week,
1074
00:49:21,220 --> 00:49:24,670
when we transition from scratch to C, you'll learn about types, data types.
1075
00:49:24,670 --> 00:49:27,970
Where the onus initially is going to be on you, the programmer,
1076
00:49:27,970 --> 00:49:30,850
to tell the program whether or not this pattern of bits
1077
00:49:30,850 --> 00:49:33,820
should be interpreted as a number or as a letter
1078
00:49:33,820 --> 00:49:35,642
or as a color or something else.
1079
00:49:35,642 --> 00:49:37,850
Nowadays, though, and toward the end of the semester,
1080
00:49:37,850 --> 00:49:41,170
you'll use languages, like Python, where the computer just figures it out
1081
00:49:41,170 --> 00:49:45,950
for you by context, which makes it even easier and faster to program as well.
1082
00:49:45,950 --> 00:49:50,580
Other questions on Unicode, ASCII or the like?
1083
00:49:50,580 --> 00:49:51,080
All right.
1084
00:49:51,080 --> 00:49:53,330
Well, how about just a few other forms of information?
1085
00:49:53,330 --> 00:49:55,250
RGB was called out earlier, Red, Green, Blue.
1086
00:49:55,250 --> 00:49:57,410
How do images get represented in computers?
1087
00:49:57,410 --> 00:50:01,000
Well, in fact, it's typically an assembly of some amount of red,
1088
00:50:01,000 --> 00:50:02,750
some amount of green, some amount of blue,
1089
00:50:02,750 --> 00:50:04,340
but there are other representations.
1090
00:50:04,340 --> 00:50:06,423
If you're a graphic designer, you might know them,
1091
00:50:06,423 --> 00:50:08,000
but RGB is still pretty common.
1092
00:50:08,000 --> 00:50:08,970
What does this mean?
1093
00:50:08,970 --> 00:50:12,560
This means to represent every dot on your phone or every dot
1094
00:50:12,560 --> 00:50:15,470
on your TV or your laptop or desktop, there
1095
00:50:15,470 --> 00:50:18,395
is a number representing how much red that dot should show,
1096
00:50:18,395 --> 00:50:21,020
a number representing how much green, and a number representing
1097
00:50:21,020 --> 00:50:24,690
how much blue it should show, red, green, blue, respectively.
1098
00:50:24,690 --> 00:50:27,980
So for instance, if a dot on your screen were
1099
00:50:27,980 --> 00:50:33,410
using these three numbers, these three values or bytes, 72, 73, 33,
1100
00:50:33,410 --> 00:50:37,250
in a text message or email, that would be interpreted as I claimed "High."
1101
00:50:37,250 --> 00:50:40,160
But in Photoshop or in some graphical program,
1102
00:50:40,160 --> 00:50:43,670
that same pattern would be interpreted as let's
1103
00:50:43,670 --> 00:50:46,610
call it a medium amount of red, a medium amount of green,
1104
00:50:46,610 --> 00:50:47,840
and a little bit of blue.
1105
00:50:47,840 --> 00:50:49,250
And why medium and little?
1106
00:50:49,250 --> 00:50:53,120
Turns out, each of these are bytes, the smallest value you can have in a byte
1107
00:50:53,120 --> 00:50:54,110
we said is 0.
1108
00:50:54,110 --> 00:50:56,558
The largest value you can have a byte is 255,
1109
00:50:56,558 --> 00:50:58,100
so I'm just kind of spitballing here.
1110
00:50:58,100 --> 00:51:01,700
This is like medium, medium, and a low amount of red, green, blue,
1111
00:51:01,700 --> 00:51:02,660
specifically.
1112
00:51:02,660 --> 00:51:07,640
Those three colors, like wavelengths of light, are combined in such a way
1113
00:51:07,640 --> 00:51:11,690
that you would have this dot on the screen, a sort of murky shade
1114
00:51:11,690 --> 00:51:12,890
of yellow or brown.
1115
00:51:12,890 --> 00:51:16,910
That is how a computer would store precisely that color,
1116
00:51:16,910 --> 00:51:18,470
and in fact, we've seen this color.
1117
00:51:18,470 --> 00:51:22,010
When you type in Face with Tears of Joy, generally, on your screen,
1118
00:51:22,010 --> 00:51:23,930
it looks like this, typically much smaller.
1119
00:51:23,930 --> 00:51:27,980
But let's zoom in, or let's Zoom in a little more.
1120
00:51:27,980 --> 00:51:30,212
What are you starting to see, if you know the term?
1121
00:51:30,212 --> 00:51:30,920
AUDIENCE: Pixels.
1122
00:51:30,920 --> 00:51:33,212
DAVID J. MALAN: So pixels, it's getting very pixelated.
1123
00:51:33,212 --> 00:51:36,510
A pixel is just a dot on the screen, and if you really zoom in on it,
1124
00:51:36,510 --> 00:51:40,520
you can literally see all of the dots that compose an emoji,
1125
00:51:40,520 --> 00:51:43,940
in this case on iOS, in the font that Apple is using to represent this
1126
00:51:43,940 --> 00:51:45,750
particular pattern of 0's and 0's.
1127
00:51:45,750 --> 00:51:49,400
So one of those yellow dots-- and there's many of them all kind of blend
1128
00:51:49,400 --> 00:51:50,330
together here--
1129
00:51:50,330 --> 00:51:53,780
each dot on the screen I claim is 3 bytes.
1130
00:51:53,780 --> 00:51:55,700
How much red, green, blue for this dot?
1131
00:51:55,700 --> 00:51:57,350
How much red green blue for this dot?
1132
00:51:57,350 --> 00:51:58,700
How much red green blue for this dot?
1133
00:51:58,700 --> 00:52:01,575
And you'll notice too, that when it gets to be sort of brownish here,
1134
00:52:01,575 --> 00:52:02,720
the dots really stand out.
1135
00:52:02,720 --> 00:52:05,480
The 3 values, the 3 bytes, a.k.a.
1136
00:52:05,480 --> 00:52:08,160
24 bits, are just slightly different.
1137
00:52:08,160 --> 00:52:11,570
And so underneath the hood, this is why images, photographs
1138
00:52:11,570 --> 00:52:14,930
that you take or gif that you download, get so darn big,
1139
00:52:14,930 --> 00:52:19,850
potentially, because you have a number representing every dot on the screen.
1140
00:52:19,850 --> 00:52:22,970
Well, if this I claim is indeed how images are typically
1141
00:52:22,970 --> 00:52:25,340
represented, using pattern of bits that are
1142
00:52:25,340 --> 00:52:29,630
assigned to some amount of red, green, or blue, how do you get video?
1143
00:52:29,630 --> 00:52:34,820
What is a video, if at the end of the day, all we have are 0's and 1's?
1144
00:52:34,820 --> 00:52:36,500
What's a video, perhaps?
1145
00:52:36,500 --> 00:52:37,610
Yeah?
1146
00:52:37,610 --> 00:52:39,420
Let's go here, way in back.
1147
00:52:39,420 --> 00:52:39,920
Yeah.
1148
00:52:39,920 --> 00:52:42,155
Pixels really changing values over time.
1149
00:52:42,155 --> 00:52:44,780
and do you want to confirm or deny, the hand that went up here?
1150
00:52:44,780 --> 00:52:45,380
AUDIENCE: [INAUDIBLE]
1151
00:52:45,380 --> 00:52:47,422
DAVID J. MALAN: Yeah, or equivalently, a sequence
1152
00:52:47,422 --> 00:52:49,828
of images that, over time, are changing on the screen.
1153
00:52:49,828 --> 00:52:51,620
So both of those are valid interpretations,
1154
00:52:51,620 --> 00:52:55,010
and just for fun, if you grew up with these picture books,
1155
00:52:55,010 --> 00:52:57,540
you might remember a little something like this.
1156
00:52:57,540 --> 00:52:59,191
if we could dim the lights?
1157
00:52:59,191 --> 00:53:02,628
[MUSIC PLAYING]
1158
00:53:02,628 --> 00:53:19,925
1159
00:53:19,925 --> 00:53:22,340
So that's the old school analog way to implement
1160
00:53:22,340 --> 00:53:27,320
a video, in the sense that that artist wrote out like hundreds of pieces
1161
00:53:27,320 --> 00:53:31,250
of paper, with almost identical images, but where the ink from their pencil
1162
00:53:31,250 --> 00:53:33,015
or pen was slightly moving.
1163
00:53:33,015 --> 00:53:35,390
And if you digitize that, such that each of those strokes
1164
00:53:35,390 --> 00:53:38,300
are represented with dots instead, that's really what you're seeing
1165
00:53:38,300 --> 00:53:41,220
is a sequence of all of these images flying across the screen.
1166
00:53:41,220 --> 00:53:44,180
And if we dive into the real world, if you've ever watched a film,
1167
00:53:44,180 --> 00:53:48,320
a Hollywood movie is typically 24 FPS, Frames Per Second.
1168
00:53:48,320 --> 00:53:53,030
That really means you're seeing 24 images per second, or on TV or in soap
1169
00:53:53,030 --> 00:53:55,370
operas, it's often 30 frames per second.
1170
00:53:55,370 --> 00:53:57,210
That makes things look a little more smooth.
1171
00:53:57,210 --> 00:53:59,750
So it's not actual motion picture, if you will.
1172
00:53:59,750 --> 00:54:02,480
It's sequences of pictures, and your brain and mind
1173
00:54:02,480 --> 00:54:05,900
are interpolating that, oh, this is smooth movement, even though we're just
1174
00:54:05,900 --> 00:54:07,985
seeing a lot of pictures really fast.
1175
00:54:07,985 --> 00:54:10,610
Now, that gets really big, and we'll talk later in the semester
1176
00:54:10,610 --> 00:54:13,700
how you can compress information, so that you're not using way
1177
00:54:13,700 --> 00:54:15,200
more bits than you actually need to.
1178
00:54:15,200 --> 00:54:17,330
And there's fancy algorithms that folks have developed,
1179
00:54:17,330 --> 00:54:19,850
but at the end of the day, that's really all a video might
1180
00:54:19,850 --> 00:54:21,590
be is a sequence of images.
1181
00:54:21,590 --> 00:54:23,870
Conversely, if you want to represent the music that
1182
00:54:23,870 --> 00:54:27,410
accompanies that or something else, if any of you play an instrument
1183
00:54:27,410 --> 00:54:30,978
and can read sheet music, how could you digitize this?
1184
00:54:30,978 --> 00:54:31,520
[PIANO MUSIC]
1185
00:54:31,520 --> 00:54:35,330
Like how could you represent musical notes in a computer?
1186
00:54:35,330 --> 00:54:37,850
You and I hear them when we play files, but what's
1187
00:54:37,850 --> 00:54:41,210
really going on underneath the hood?
1188
00:54:41,210 --> 00:54:44,760
Any musicians, piano players?
1189
00:54:44,760 --> 00:54:45,260
Anyone?
1190
00:54:45,260 --> 00:54:45,760
Yeah?
1191
00:54:45,760 --> 00:54:47,150
AUDIENCE: Hertz value?
1192
00:54:47,150 --> 00:54:49,590
DAVID J. MALAN: OK, so Hertz value, so some frequency.
1193
00:54:49,590 --> 00:54:52,528
So sound is some frequency, and it's kind of hitting your eardrum.
1194
00:54:52,528 --> 00:54:55,320
And that's what makes it sound low or high or somewhere in between.
1195
00:54:55,320 --> 00:54:58,460
So maybe we could assign, just like there's letters A through G here,
1196
00:54:58,460 --> 00:55:00,860
maybe we could assign specific frequency values, which
1197
00:55:00,860 --> 00:55:02,480
are just going to be numbers measured in something
1198
00:55:02,480 --> 00:55:04,010
called Hertz, something per second.
1199
00:55:04,010 --> 00:55:06,843
And maybe we could have a few other numbers for each of these notes,
1200
00:55:06,843 --> 00:55:08,780
not just the note or the frequency.
1201
00:55:08,780 --> 00:55:12,350
Maybe, we could represent the loudness of it, like how hard
1202
00:55:12,350 --> 00:55:14,900
or how softly a human might equivalently press it.
1203
00:55:14,900 --> 00:55:17,630
Maybe a third number, like duration, like how long
1204
00:55:17,630 --> 00:55:19,380
is there finger on the keyboard?
1205
00:55:19,380 --> 00:55:22,790
So you can imagine quantifying something like music that in the real world
1206
00:55:22,790 --> 00:55:25,400
is perfectly continuous as something more discrete,
1207
00:55:25,400 --> 00:55:29,450
by representing each note over time as just some sequence of values.
1208
00:55:29,450 --> 00:55:32,510
And there's so many different ways to do this, MIDI, if you've heard,
1209
00:55:32,510 --> 00:55:34,680
mp3's, AAC.
1210
00:55:34,680 --> 00:55:37,670
Almost all of the file extensions you see on your Mac or PC,
1211
00:55:37,670 --> 00:55:40,280
if you see them at all, ultimately just mean
1212
00:55:40,280 --> 00:55:43,170
there's a different form of representation for, in this case,
1213
00:55:43,170 --> 00:55:45,290
something like sound.
1214
00:55:45,290 --> 00:55:48,860
So let me just stipulate, there are these and many more
1215
00:55:48,860 --> 00:55:51,950
ways to represent inputs and outputs, and thankfully, humans
1216
00:55:51,950 --> 00:55:53,300
have standardized a lot of this.
1217
00:55:53,300 --> 00:55:55,580
They don't always agree, and this is why we
1218
00:55:55,580 --> 00:55:57,920
have different file formats for Apple numbers
1219
00:55:57,920 --> 00:55:59,840
and Microsoft Excel and Google Spreadsheets
1220
00:55:59,840 --> 00:56:02,100
and stupid incompatibilities like that.
1221
00:56:02,100 --> 00:56:04,190
But generally speaking, humans have standardized
1222
00:56:04,190 --> 00:56:07,670
how we represent the inputs and outputs to and from problems.
1223
00:56:07,670 --> 00:56:10,370
But let's now focus on this Black box, so to speak,
1224
00:56:10,370 --> 00:56:12,590
in the middle, this abstraction.
1225
00:56:12,590 --> 00:56:15,020
So abstraction is technically a term that you'll
1226
00:56:15,020 --> 00:56:16,820
see all over the place in computer science,
1227
00:56:16,820 --> 00:56:18,695
and really, problem solving, that just refers
1228
00:56:18,695 --> 00:56:23,630
to the simplification of something, so that you don't focus on the lower
1229
00:56:23,630 --> 00:56:25,310
level implementation details.
1230
00:56:25,310 --> 00:56:29,880
You really just focus on the high level goals or the process itself.
1231
00:56:29,880 --> 00:56:32,570
Therefore, your car, if you have a license
1232
00:56:32,570 --> 00:56:35,660
and have driven or have been in a car, a car, so far as you're concerned,
1233
00:56:35,660 --> 00:56:36,830
is probably an abstraction.
1234
00:56:36,830 --> 00:56:38,997
Most of us, if you're like me, probably don't really
1235
00:56:38,997 --> 00:56:42,290
know or care how the engine works and all the parts that are moving.
1236
00:56:42,290 --> 00:56:44,570
To you, it's just a way of getting from point A
1237
00:56:44,570 --> 00:56:47,570
to point B. It's an abstraction, but someone, hopefully the mechanic,
1238
00:56:47,570 --> 00:56:50,510
does know those lower level implementation details.
1239
00:56:50,510 --> 00:56:53,030
If you had to understand how a car works every time you
1240
00:56:53,030 --> 00:56:55,220
want to go to school or to the store, it's probably
1241
00:56:55,220 --> 00:56:56,700
going to be a pretty slow process.
1242
00:56:56,700 --> 00:57:00,590
You just want to think and operate at this higher level of abstraction,
1243
00:57:00,590 --> 00:57:04,350
and we're going to do this all the time when writing code and solving problems.
1244
00:57:04,350 --> 00:57:07,940
So what then is in this black box, this abstraction at the moment?
1245
00:57:07,940 --> 00:57:10,100
Well, generally, it's what a computer scientist
1246
00:57:10,100 --> 00:57:14,180
would call an algorithm, step-by-step instructions for solving some problem.
1247
00:57:14,180 --> 00:57:16,940
Now, let's consider the implementation details,
1248
00:57:16,940 --> 00:57:20,150
that is to say how you might solve certain problems,
1249
00:57:20,150 --> 00:57:24,230
and let's take a old school example but in modern form.
1250
00:57:24,230 --> 00:57:27,970
This icon, if you have an iPhone, is, of course for your contacts application.
1251
00:57:27,970 --> 00:57:30,470
And if you've got a whole bunch of family members or friends
1252
00:57:30,470 --> 00:57:32,420
or colleagues in your phonebook, you have
1253
00:57:32,420 --> 00:57:35,330
some kind of contacts pictured here, and it's alphabetized typically
1254
00:57:35,330 --> 00:57:36,800
by first name and last name.
1255
00:57:36,800 --> 00:57:38,510
And odds are, you and I are in the habit,
1256
00:57:38,510 --> 00:57:41,690
if they're not already a favorite, of like clicking on Search
1257
00:57:41,690 --> 00:57:42,950
and then using autocomplete.
1258
00:57:42,950 --> 00:57:45,140
And what happens when you start typing autocomplete?
1259
00:57:45,140 --> 00:57:48,710
Well if you type in the letter H, you'll see only, presumably,
1260
00:57:48,710 --> 00:57:50,640
Hagrid, Harry, Hermione, and so forth.
1261
00:57:50,640 --> 00:57:54,350
If you type in H-A that shows you only Hagrid and Harry,
1262
00:57:54,350 --> 00:57:56,280
and it all happens super fast.
1263
00:57:56,280 --> 00:57:57,720
So how is that happening?
1264
00:57:57,720 --> 00:58:02,600
Well, typically, you could just start at the top and look to the bottom,
1265
00:58:02,600 --> 00:58:06,110
searching for all of the H's or all of the H-A's, but for larger data
1266
00:58:06,110 --> 00:58:07,490
sets that's going to get slow.
1267
00:58:07,490 --> 00:58:10,130
For the Googles of the world, that's going to get really slow.
1268
00:58:10,130 --> 00:58:13,580
And even on our phones when you have hundreds, thousands of contacts,
1269
00:58:13,580 --> 00:58:17,210
eventually, even that kind of approach, that algorithm step by step
1270
00:58:17,210 --> 00:58:18,300
it might be slow.
1271
00:58:18,300 --> 00:58:21,050
So how might we go about searching for someone in a phonebook
1272
00:58:21,050 --> 00:58:24,230
like this, like say John Harvard?
1273
00:58:24,230 --> 00:58:26,720
Well, here's an old school incarnation of this,
1274
00:58:26,720 --> 00:58:29,630
and odds are, you might not have had occasion to even physically
1275
00:58:29,630 --> 00:58:30,910
use this thing, nowadays.
1276
00:58:30,910 --> 00:58:32,660
And in fact, this is a bit of a white lie,
1277
00:58:32,660 --> 00:58:34,535
because this is the yellow pages, which means
1278
00:58:34,535 --> 00:58:36,950
this is a book of companies not people.
1279
00:58:36,950 --> 00:58:40,370
But this is all you can find, and at that, it's even hard to find this.
1280
00:58:40,370 --> 00:58:43,770
But this is the same thing in analog form, physical form.
1281
00:58:43,770 --> 00:58:47,310
So if I wanted to search for someone like John Harvard, how could I do that?
1282
00:58:47,310 --> 00:58:49,580
Well, I could start on page 1, and I could
1283
00:58:49,580 --> 00:58:55,345
start searching for page 2, page 3, page 4, page 5.
1284
00:58:55,345 --> 00:58:57,470
A little hard to do physically, especially since no
1285
00:58:57,470 --> 00:58:59,345
one's used this phone book in a lot of years.
1286
00:58:59,345 --> 00:59:06,260
But is this algorithm correct, turning page by page, very inelegantly?
1287
00:59:06,260 --> 00:59:06,980
Is this correct?
1288
00:59:06,980 --> 00:59:09,690
Will I find John Harvard, if he's in here?
1289
00:59:09,690 --> 00:59:10,190
All right.
1290
00:59:10,190 --> 00:59:11,030
So yes.
1291
00:59:11,030 --> 00:59:14,240
This is a little stupidly tedious, because if there's like 1,000 pages,
1292
00:59:14,240 --> 00:59:16,870
he might be a few hundred pages into this, but it's correct.
1293
00:59:16,870 --> 00:59:20,120
At some point, I will find him, and if he's on the page, I'll be able to call.
1294
00:59:20,120 --> 00:59:20,620
Why?
1295
00:59:20,620 --> 00:59:22,950
Because presumably, the names are alphabetized in here,
1296
00:59:22,950 --> 00:59:24,608
and there's no cheat sheet on the edge.
1297
00:59:24,608 --> 00:59:27,650
So I have to search for John Harvard from left to right, searching for H,
1298
00:59:27,650 --> 00:59:29,150
if it's alphabetized by last name.
1299
00:59:29,150 --> 00:59:30,740
Well, what would be marginally better?
1300
00:59:30,740 --> 00:59:32,240
Well, how about two pages at a time?
1301
00:59:32,240 --> 00:59:34,160
It's hard to do with a 20-year-old phone book,
1302
00:59:34,160 --> 00:59:41,150
where the pages are grown together, but 2, 4, 6, 8, 10, 12.
1303
00:59:41,150 --> 00:59:42,605
This algorithm, is this correct?
1304
00:59:42,605 --> 00:59:43,465
AUDIENCE: No.
1305
00:59:43,465 --> 00:59:44,150
DAVID J. MALAN: All right, so no.
1306
00:59:44,150 --> 00:59:44,780
Why?
1307
00:59:44,780 --> 00:59:46,280
AUDIENCE: You're skipping pages.
1308
00:59:46,280 --> 00:59:46,610
DAVID J. MALAN: Yeah.
1309
00:59:46,610 --> 00:59:48,060
So I'm skipping every other page.
1310
00:59:48,060 --> 00:59:53,480
So if I don't consider that, and I find myself in like the I section or the J
1311
00:59:53,480 --> 00:59:56,120
section, well, I might accidentally conclude,
1312
00:59:56,120 --> 00:59:59,755
nope, I haven't found John Harvard yet, just because I skipped him, because it
1313
00:59:59,755 --> 01:00:01,130
was sandwiched between two pages.
1314
01:00:01,130 --> 01:00:04,700
Now, I can fix this, I think, if I do hit the I section,
1315
01:00:04,700 --> 01:00:08,100
well, let me just double back one page, just in case he was in that last page.
1316
01:00:08,100 --> 01:00:12,277
So it's recoverable, but it's almost twice as fast, minus that hiccup there.
1317
01:00:12,277 --> 01:00:14,360
But what most of us would do, and what your phones
1318
01:00:14,360 --> 01:00:16,395
are doing, albeit digitally, is they open up
1319
01:00:16,395 --> 01:00:18,020
roughly to the middle of the phonebook.
1320
01:00:18,020 --> 01:00:21,747
And they look down, and they say, oh, I'm in roughly the M section.
1321
01:00:21,747 --> 01:00:24,080
So I'm roughly halfway through the 1,000-page phonebook.
1322
01:00:24,080 --> 01:00:26,510
But what do I now know about John Harvard?
1323
01:00:26,510 --> 01:00:29,820
Where is he, to my left or to my right?
1324
01:00:29,820 --> 01:00:30,320
All right.
1325
01:00:30,320 --> 01:00:32,810
So alphabetically, he's presumably to my left,
1326
01:00:32,810 --> 01:00:37,340
and so here I can, both metaphorically and physically,
1327
01:00:37,340 --> 01:00:39,950
tear the problem in half.
1328
01:00:39,950 --> 01:00:41,330
You don't need to be impressed.
1329
01:00:41,330 --> 01:00:43,280
It's really easy down the spine that way,
1330
01:00:43,280 --> 01:00:46,460
but I know that John Harvard is to the left here.
1331
01:00:46,460 --> 01:00:51,500
But now I can throw, unnecessarily dramatically, half and page one out
1332
01:00:51,500 --> 01:00:53,810
of the way, and what do I now know?
1333
01:00:53,810 --> 01:00:55,910
I've gone from 1,000 pages to like 500.
1334
01:00:55,910 --> 01:00:57,602
I can repeat roughly the same algorithm.
1335
01:00:57,602 --> 01:01:00,560
Go to the half of this, and so this time, I went back a little too far.
1336
01:01:00,560 --> 01:01:03,150
I'm in now the E section.
1337
01:01:03,150 --> 01:01:04,040
So what do I know?
1338
01:01:04,040 --> 01:01:07,490
Is John Harvard to my left or to my right?
1339
01:01:07,490 --> 01:01:09,950
To my right, so I can, again, tear the problem in half.
1340
01:01:09,950 --> 01:01:12,800
Throw this half away, and now I'm really flying.
1341
01:01:12,800 --> 01:01:17,180
I'm doing it verbally slowly, but that went from 1,000 pages to 500
1342
01:01:17,180 --> 01:01:18,380
to now 250.
1343
01:01:18,380 --> 01:01:20,960
And now I can do it again, 125.
1344
01:01:20,960 --> 01:01:25,160
I do it again, roughly like 67, and keep doing it again and again and again,
1345
01:01:25,160 --> 01:01:29,840
until I get left with, hopefully, just one single page or in this case
1346
01:01:29,840 --> 01:01:32,210
an ad for, ironically, a mechanic.
1347
01:01:32,210 --> 01:01:36,620
OK, so what is the implication for our performance?
1348
01:01:36,620 --> 01:01:39,590
Well, let's just do this sort of in the abstract, if you will,
1349
01:01:39,590 --> 01:01:43,250
if that first algorithm were to be plotted just quickly on a chart
1350
01:01:43,250 --> 01:01:44,340
without even numbers.
1351
01:01:44,340 --> 01:01:47,340
Here's my x-axis, size of problem on the x-axis.
1352
01:01:47,340 --> 01:01:49,760
So the bigger the problem, the farther out that way.
1353
01:01:49,760 --> 01:01:51,200
Time to solve the problem.
1354
01:01:51,200 --> 01:01:55,530
The higher you go up on the y-axis, the more time you're taking to solve it.
1355
01:01:55,530 --> 01:01:58,190
How would we draw the running time, the amount of time
1356
01:01:58,190 --> 01:01:59,850
taken to run that first algorithm?
1357
01:01:59,850 --> 01:02:01,010
Well, it's going to be a straight line.
1358
01:02:01,010 --> 01:02:01,520
Why?
1359
01:02:01,520 --> 01:02:03,485
Because if you add one more page next year
1360
01:02:03,485 --> 01:02:05,360
because more people move to Cambridge, you're
1361
01:02:05,360 --> 01:02:07,790
going to add one more page turn potentially, so
1362
01:02:07,790 --> 01:02:09,790
one more second, one more unit of time.
1363
01:02:09,790 --> 01:02:10,790
So it's a straight line.
1364
01:02:10,790 --> 01:02:12,207
And we'll abstract it away as "n."
1365
01:02:12,207 --> 01:02:15,560
If there's n pages in the phone book, the slope of this line
1366
01:02:15,560 --> 01:02:16,640
is essentially n.
1367
01:02:16,640 --> 01:02:20,180
The second algorithm, wherein I was doing two pages at a time,
1368
01:02:20,180 --> 01:02:22,970
was twice as fast, but it's still a straight line.
1369
01:02:22,970 --> 01:02:25,190
And in fact, let me just draw some dotted lines here.
1370
01:02:25,190 --> 01:02:28,310
If the phone book is this big, with my first algorithm,
1371
01:02:28,310 --> 01:02:32,990
it might take this many units of time, this many steps, this many page turns.
1372
01:02:32,990 --> 01:02:37,430
But with that second algorithm, notice that the intersection is much lower
1373
01:02:37,430 --> 01:02:39,260
on the yellow line than on the red.
1374
01:02:39,260 --> 01:02:42,170
So n over 2 means there's half as many pages here,
1375
01:02:42,170 --> 01:02:43,517
if n is the number of pages.
1376
01:02:43,517 --> 01:02:45,350
So indeed, that algorithm-- the second one--
1377
01:02:45,350 --> 01:02:49,530
is twice as fast minus the little hiccup that I have to double back one page.
1378
01:02:49,530 --> 01:02:53,000
But that's not a big deal if I'm still doing things twice as fast.
1379
01:02:53,000 --> 01:02:55,340
But the third algorithm looks fundamentally different.
1380
01:02:55,340 --> 01:02:56,300
It looks like this.
1381
01:02:56,300 --> 01:02:58,970
Logarithms, if you recall from high school or prior--
1382
01:02:58,970 --> 01:03:00,320
if you don't, that's fine too.
1383
01:03:00,320 --> 01:03:03,900
It's just a fundamentally different function, a different shape.
1384
01:03:03,900 --> 01:03:05,870
And notice that the green line is going up
1385
01:03:05,870 --> 01:03:11,060
and up and up but a much slower rate of increase, which means crazy things
1386
01:03:11,060 --> 01:03:11,720
are possible.
1387
01:03:11,720 --> 01:03:14,270
If two towns in Massachusetts, like Cambridge and Allston,
1388
01:03:14,270 --> 01:03:18,560
across the river, merge next year, for instance, in terms of their phone book,
1389
01:03:18,560 --> 01:03:20,890
they're phone book just got twice as big.
1390
01:03:20,890 --> 01:03:23,740
For the first algorithm, that's going to take me twice as many steps
1391
01:03:23,740 --> 01:03:24,550
to go through.
1392
01:03:24,550 --> 01:03:28,390
The second algorithm, almost it's going to take me 50% more steps
1393
01:03:28,390 --> 01:03:29,780
to go through, two at a time.
1394
01:03:29,780 --> 01:03:33,010
But the third algorithm, that I ended with, tearing things again and again,
1395
01:03:33,010 --> 01:03:36,190
dividing and conquering, if you will, in half and in half and in half,
1396
01:03:36,190 --> 01:03:38,650
how many more steps will my third algorithm
1397
01:03:38,650 --> 01:03:42,965
take if Cambridge and Allston merge into a phone book that's twice as big?
1398
01:03:42,965 --> 01:03:43,840
AUDIENCE: Four steps.
1399
01:03:43,840 --> 01:03:45,590
DAVID J. MALAN: Just one more step, right?
1400
01:03:45,590 --> 01:03:46,160
No big deal.
1401
01:03:46,160 --> 01:03:48,243
You just take a really big bite out of the problem
1402
01:03:48,243 --> 01:03:50,960
once you decide if John Harvard is to the left or to the right.
1403
01:03:50,960 --> 01:03:53,470
And so you've made much faster progress.
1404
01:03:53,470 --> 01:03:56,800
And so this, in essence, is what your computer, your phone
1405
01:03:56,800 --> 01:04:00,730
is probably doing underneath the hood when searching for Harry or Hermione
1406
01:04:00,730 --> 01:04:04,090
or Hagrid or anyone else because it's that much faster,
1407
01:04:04,090 --> 01:04:05,590
especially when you have large data.
1408
01:04:05,590 --> 01:04:07,632
If you don't have that many contacts, it probably
1409
01:04:07,632 --> 01:04:11,318
doesn't matter if you search from top to bottom or more
1410
01:04:11,318 --> 01:04:13,360
in the form of this divide-and-conquer algorithm.
1411
01:04:13,360 --> 01:04:16,600
But if you're the Googles of the world or you're analyzing large data sets,
1412
01:04:16,600 --> 01:04:18,920
indeed, this is going to add up quite quickly.
1413
01:04:18,920 --> 01:04:20,420
So where do we go with this?
1414
01:04:20,420 --> 01:04:23,140
Well, we're going to introduce next something called pseudocode.
1415
01:04:23,140 --> 01:04:25,630
How can I translate what I did verbally there,
1416
01:04:25,630 --> 01:04:27,673
sort of intuitively, to actual code?
1417
01:04:27,673 --> 01:04:28,840
Well, this won't be Scratch.
1418
01:04:28,840 --> 01:04:30,830
This won't be C or Python just yet.
1419
01:04:30,830 --> 01:04:32,840
It's just going to be an English-like syntax.
1420
01:04:32,840 --> 01:04:35,530
And this is how many programmers would start solving a problem.
1421
01:04:35,530 --> 01:04:38,763
They don't start typing out code in C or Python or the like.
1422
01:04:38,763 --> 01:04:40,930
They use English or whatever their human language is
1423
01:04:40,930 --> 01:04:43,210
to jot down an outline for their ideas.
1424
01:04:43,210 --> 01:04:45,700
My first step, really, was picking up the phone book.
1425
01:04:45,700 --> 01:04:48,790
My second step was opening to the middle of the phone book.
1426
01:04:48,790 --> 01:04:52,570
My third step was somewhat different-- look at the page, because why?
1427
01:04:52,570 --> 01:04:57,670
My fourth step was if person I'm looking for is on the page, I then do what?
1428
01:04:57,670 --> 01:05:00,820
It never happened in my example, but I call the person.
1429
01:05:00,820 --> 01:05:01,960
So I'm done.
1430
01:05:01,960 --> 01:05:04,750
Else if the person is earlier in the book
1431
01:05:04,750 --> 01:05:08,830
alphabetically, as John Harvard was in the case of my H, then
1432
01:05:08,830 --> 01:05:12,460
I should search to the middle of the left of the phone book.
1433
01:05:12,460 --> 01:05:15,800
And then I should go back to step three.
1434
01:05:15,800 --> 01:05:18,820
Step 3 is look at the page, thereby repeating the same process
1435
01:05:18,820 --> 01:05:19,540
again and again.
1436
01:05:19,540 --> 01:05:22,960
Step 9, though, might be else if the person is later in the book,
1437
01:05:22,960 --> 01:05:25,960
then let's go ahead and open to the middle of the right half of the book
1438
01:05:25,960 --> 01:05:29,050
and then go back to line 3.
1439
01:05:29,050 --> 01:05:32,350
Else there's a fourth scenario we should probably consider,
1440
01:05:32,350 --> 01:05:36,520
lest my search process freeze or crash or give me one of those spinning beach
1441
01:05:36,520 --> 01:05:37,750
balls with a bug.
1442
01:05:37,750 --> 01:05:38,470
Yeah--
1443
01:05:38,470 --> 01:05:39,580
AUDIENCE: [INAUDIBLE]
1444
01:05:39,580 --> 01:05:41,620
DAVID J. MALAN: Yeah, what if John Harvard isn't in the phone book?
1445
01:05:41,620 --> 01:05:44,438
I'd prefer that my algorithm, my phone not just reboot or freeze.
1446
01:05:44,438 --> 01:05:46,480
I should handle that with some kind of catch all.
1447
01:05:46,480 --> 01:05:49,420
Else, so to speak, let's just quit the program.
1448
01:05:49,420 --> 01:05:54,340
So there's well-defined behavior for every possible scenario of the four.
1449
01:05:54,340 --> 01:05:56,470
Now, let's call out a few of these salient terms.
1450
01:05:56,470 --> 01:05:58,390
It turns out, if I highlight in yellow here,
1451
01:05:58,390 --> 01:06:00,620
there's a pattern to what I've been doing here.
1452
01:06:00,620 --> 01:06:02,050
These are all of my English verbs.
1453
01:06:02,050 --> 01:06:05,410
And in a moment, we're going to start calling those verbs "functions."
1454
01:06:05,410 --> 01:06:09,280
When you program or write code and you want the program or the computer
1455
01:06:09,280 --> 01:06:11,980
to do something for you, some action or verb,
1456
01:06:11,980 --> 01:06:14,020
we're going to refer to those actions or verbs
1457
01:06:14,020 --> 01:06:16,510
as these things called "functions," like those here.
1458
01:06:16,510 --> 01:06:18,640
By contrast, I've just highlighted, instead,
1459
01:06:18,640 --> 01:06:20,950
my "if," my "else if," my "else if," and "else."
1460
01:06:20,950 --> 01:06:22,990
This is going to represent what we're going
1461
01:06:22,990 --> 01:06:26,590
to start calling a "conditional," a proverbial fork in the road where
1462
01:06:26,590 --> 01:06:29,860
you can either go this way or that way, do this thing or this other thing.
1463
01:06:29,860 --> 01:06:32,320
And you're going to decide which of those things
1464
01:06:32,320 --> 01:06:35,740
to do based on what I've now highlighted here, which are going
1465
01:06:35,740 --> 01:06:38,230
to be called "Boolean expressions"--
1466
01:06:38,230 --> 01:06:41,260
Boole, referring to a mathematician, last name Boole.
1467
01:06:41,260 --> 01:06:47,140
A Boolean expression is just a question with a yes/no, a true/false, a 1 or a 0
1468
01:06:47,140 --> 01:06:47,980
answer, if you will.
1469
01:06:47,980 --> 01:06:51,790
And it governs whether you do this thing or this thing or this thing or that.
1470
01:06:51,790 --> 01:06:54,440
The indentation, in this case, is important.
1471
01:06:54,440 --> 01:06:59,020
The fact that I've indented line 5 implies, by convention in programming,
1472
01:06:59,020 --> 01:07:03,610
that I should only do line 5 if the answer to line 4 is a yes or a true,
1473
01:07:03,610 --> 01:07:06,410
and same for these other indented lines as well.
1474
01:07:06,410 --> 01:07:09,492
And the last characteristic here is this here.
1475
01:07:09,492 --> 01:07:11,200
Someone called this out earlier, in fact.
1476
01:07:11,200 --> 01:07:15,580
These lines, 8 and 11, are now highlighted and represent what?
1477
01:07:15,580 --> 01:07:18,790
What might we call these in code if you've done--
1478
01:07:18,790 --> 01:07:21,340
yeah, so these are loops, some kind of cycles
1479
01:07:21,340 --> 01:07:24,820
that result in my doing the same thing again and again,
1480
01:07:24,820 --> 01:07:28,240
but there's a key detail with this algorithm in pseudocode.
1481
01:07:28,240 --> 01:07:31,200
Even though it's telling me to go back to line 3,
1482
01:07:31,200 --> 01:07:34,750
why is this algorithm eventually going to stop?
1483
01:07:34,750 --> 01:07:37,600
Why do I not constantly keep looking for John Harvard
1484
01:07:37,600 --> 01:07:43,010
forever by nature of these loops telling me to keep going back to line 3?
1485
01:07:43,010 --> 01:07:43,580
Good.
1486
01:07:43,580 --> 01:07:48,590
Eventually, he'll be on the page or, to your point earlier, he won't be at all
1487
01:07:48,590 --> 01:07:51,260
and we're out of pages, and so we just quit.
1488
01:07:51,260 --> 01:07:54,050
And that's the key about going to the left half or the right half.
1489
01:07:54,050 --> 01:07:56,430
It doesn't matter if you do the same thing again and again.
1490
01:07:56,430 --> 01:07:58,847
You're not going to get stuck in a so-called infinite loop
1491
01:07:58,847 --> 01:08:02,750
so long as you keep dividing the problem and shrinking it into something
1492
01:08:02,750 --> 01:08:03,890
smaller, smaller, smaller.
1493
01:08:03,890 --> 01:08:06,570
Eventually, there's going to be no problem left to solve.
1494
01:08:06,570 --> 01:08:09,110
So even if you don't think of yourself as a computer person,
1495
01:08:09,110 --> 01:08:12,140
even if you've never written code, what you'll find in the coming days
1496
01:08:12,140 --> 01:08:15,920
is that these ideas that we've just kind of harnessed from real life
1497
01:08:15,920 --> 01:08:18,080
are at your fingertips already.
1498
01:08:18,080 --> 01:08:20,090
And a lot of the process of learning to code
1499
01:08:20,090 --> 01:08:22,819
is just going to be some bumps in the road
1500
01:08:22,819 --> 01:08:25,880
because you can't quite see the new syntax in a familiar way.
1501
01:08:25,880 --> 01:08:28,005
But you'll find that the ideas, in fact, are
1502
01:08:28,005 --> 01:08:30,380
going to be more familiar than you might otherwise think.
1503
01:08:30,380 --> 01:08:32,540
And so we'll see in a bit-- and we'll take a break
1504
01:08:32,540 --> 01:08:35,000
in a moment to take a breather-- that you
1505
01:08:35,000 --> 01:08:38,240
will see these same ideas in a moment in the context of Scratch,
1506
01:08:38,240 --> 01:08:40,850
an actual programming language via which we'll drag and drop
1507
01:08:40,850 --> 01:08:42,743
puzzle pieces to make actual code work.
1508
01:08:42,743 --> 01:08:44,660
We'll see some variants of these ideas, things
1509
01:08:44,660 --> 01:08:47,040
called "arguments" and "return values" and "variables."
1510
01:08:47,040 --> 01:08:50,660
But we'll ultimately convert it into this somehow.
1511
01:08:50,660 --> 01:08:53,689
Anyone want to wager what this program will
1512
01:08:53,689 --> 01:08:56,810
do if fed to your Mac or PC or phone?
1513
01:08:56,810 --> 01:08:59,976
Here's just a massive pattern of zeros and ones.
1514
01:08:59,976 --> 01:09:01,200
AUDIENCE: [INAUDIBLE]
1515
01:09:01,200 --> 01:09:04,158
DAVID J. MALAN: It will indeed say, rather disappointingly, apparently,
1516
01:09:04,158 --> 01:09:05,370
just, "Hello, world."
1517
01:09:05,370 --> 01:09:11,250
And indeed, baked into all of these 0's and 1's are not just the H-E-L-L-O,
1518
01:09:11,250 --> 01:09:15,750
but also the verbs, the action of printing something to the screen.
1519
01:09:15,750 --> 01:09:18,555
And there's other stuff too so that the program knows how to start
1520
01:09:18,555 --> 01:09:20,430
and how to stop, a lot of stuff that we won't
1521
01:09:20,430 --> 01:09:23,370
have to worry about, that whoever designed the computer or the language
1522
01:09:23,370 --> 01:09:23,870
did.
1523
01:09:23,870 --> 01:09:25,830
But at the end of the day, you're never going
1524
01:09:25,830 --> 01:09:28,050
to be writing the 0's and 1's yourselves,
1525
01:09:28,050 --> 01:09:31,319
though our ancestors, once upon a time, did in some form.
1526
01:09:31,319 --> 01:09:35,189
We'll be using a much higher-level language, like this in C,
1527
01:09:35,189 --> 01:09:39,189
or better yet, in just a moment, like in Scratch, like this.
1528
01:09:39,189 --> 01:09:42,600
And indeed, this is why today we focus on and begin
1529
01:09:42,600 --> 01:09:45,160
with Scratch, this graphical programming language,
1530
01:09:45,160 --> 01:09:49,140
so we have a way of expressing ourselves with functions, conditionals, loops,
1531
01:09:49,140 --> 01:09:52,852
and more, but in a way that doesn't have stupid parentheses and curly braces
1532
01:09:52,852 --> 01:09:54,810
and all of these visual distractions in the way
1533
01:09:54,810 --> 01:09:57,720
and will translate that thereafter to this lower-level language.
1534
01:09:57,720 --> 01:09:59,027
But for now, that was a lot.
1535
01:09:59,027 --> 01:10:00,360
That was definitely a fire hose.
1536
01:10:00,360 --> 01:10:01,990
Let's go ahead and take a 10-minute break.
1537
01:10:01,990 --> 01:10:03,365
Feel free to get up or stay here.
1538
01:10:03,365 --> 01:10:06,755
And we'll resume in a bit with some actual code.
1539
01:10:06,755 --> 01:10:07,422
[VIDEO PLAYBACK]
1540
01:10:07,422 --> 01:10:11,374
[MUSIC - THE WEATHER GIRLS, "IT'S RAINING MEN"] [MUSIC PLAYING]
1541
01:10:11,374 --> 01:10:13,844
[THUNDER RUMBLING]
1542
01:10:13,844 --> 01:10:35,505
1543
01:10:35,505 --> 01:10:36,130
- (SINGING) Hi.
1544
01:10:36,130 --> 01:10:37,000
Hi.
1545
01:10:37,000 --> 01:10:41,491
We're your Weather Girls, and have we got news for you.
1546
01:10:41,491 --> 01:10:42,485
You better listen.
1547
01:10:42,485 --> 01:10:45,540
Get ready, all you lonely girls.
1548
01:10:45,540 --> 01:10:47,940
And leave those umbrellas at home.
1549
01:10:47,940 --> 01:10:49,217
All right.
1550
01:10:49,217 --> 01:10:51,140
Humidity is rising.
1551
01:10:51,140 --> 01:10:52,912
Hmm, rising.
1552
01:10:52,912 --> 01:10:54,460
Barometer's getting low.
1553
01:10:54,460 --> 01:10:56,034
How low, girl?
1554
01:10:56,034 --> 01:10:58,280
According to all sources--
1555
01:10:58,280 --> 01:11:01,600
what sources, now-- the street's the place to go.
1556
01:11:01,600 --> 01:11:02,880
We better hurry up.
1557
01:11:02,880 --> 01:11:09,147
'Cause tonight, for the first time, just about half-past 10:00--
1558
01:11:09,147 --> 01:11:13,736
half-past 10:00-- for the first time in history,
1559
01:11:13,736 --> 01:11:18,100
it's going to start raining men-- start raining men!
1560
01:11:18,100 --> 01:11:21,650
It's raining men, hallelujah!
1561
01:11:21,650 --> 01:11:25,780
It's raining men, amen!
1562
01:11:25,780 --> 01:11:27,505
I'm going to go out.
1563
01:11:27,505 --> 01:11:33,920
I'm going to let myself get absolutely soaking wet!
1564
01:11:33,920 --> 01:11:37,045
It's raining men, hallelujah!
1565
01:11:37,045 --> 01:11:38,795
DAVID J. MALAN: [CHUCKLES] All right, so--
1566
01:11:38,795 --> 01:11:39,378
[END PLAYBACK]
1567
01:11:39,378 --> 01:11:41,400
[APPLAUSE]
1568
01:11:41,400 --> 01:11:43,720
1569
01:11:43,720 --> 01:11:47,560
So this then is Scratch, a graphical programming language from our friends
1570
01:11:47,560 --> 01:11:49,897
down the road at MIT's Media Lab that indeed some of you
1571
01:11:49,897 --> 01:11:51,730
might have used in grade school or the like,
1572
01:11:51,730 --> 01:11:53,800
for playing and writing code and the like,
1573
01:11:53,800 --> 01:11:57,820
but you maybe didn't necessarily think about how some of these primitives
1574
01:11:57,820 --> 01:11:58,630
ultimately worked.
1575
01:11:58,630 --> 01:12:01,600
And in fact, everything you've done-- if you've used Scratch before--
1576
01:12:01,600 --> 01:12:05,245
and everything you'll see today is going to apply to all of the weeks to come,
1577
01:12:05,245 --> 01:12:07,120
as we explore these things called "functions"
1578
01:12:07,120 --> 01:12:09,820
and "loops" and "conditionals," "Boolean expressions" and more.
1579
01:12:09,820 --> 01:12:12,940
With Scratch, because it's so graphical and animated-congruent,
1580
01:12:12,940 --> 01:12:17,080
can you create animations, like this one, interactive art, and software
1581
01:12:17,080 --> 01:12:17,860
more generally.
1582
01:12:17,860 --> 01:12:20,170
But you'll do so by dragging and dropping
1583
01:12:20,170 --> 01:12:23,990
puzzle pieces that only lock together if it makes logical sense to do so.
1584
01:12:23,990 --> 01:12:26,770
And you won't have to deal with, in this first week of class,
1585
01:12:26,770 --> 01:12:30,400
is curly braces, parentheses, all of the weird symbology
1586
01:12:30,400 --> 01:12:33,460
that you might recall seeing, when we just wanted to say, "Hello, world."
1587
01:12:33,460 --> 01:12:35,980
Now, this particular program, "Raining Men,"
1588
01:12:35,980 --> 01:12:39,340
was written by a former CS50 teaching fellow, Andrew Berry,
1589
01:12:39,340 --> 01:12:41,860
who's actually now the general manager of the Cleveland
1590
01:12:41,860 --> 01:12:43,730
Browns, the American football team.
1591
01:12:43,730 --> 01:12:45,820
And so these are just some of the programs
1592
01:12:45,820 --> 01:12:48,400
that some of your predecessors in the class have created.
1593
01:12:48,400 --> 01:12:51,670
And you'll see, in the remainder of class here, a couple of others
1594
01:12:51,670 --> 01:12:56,170
as well, and more in the course's first assignment, namely, problem set zero.
1595
01:12:56,170 --> 01:12:57,470
So how do we get there?
1596
01:12:57,470 --> 01:13:00,040
Well, first a quick tour of what it is we're going to do.
1597
01:13:00,040 --> 01:13:03,523
This, in Scratch, is perhaps the simplest program you can write.
1598
01:13:03,523 --> 01:13:06,565
And even if you've never seen Scratch or any programming language before,
1599
01:13:06,565 --> 01:13:09,970
you can probably guess that this just says, on the screen somehow,
1600
01:13:09,970 --> 01:13:10,940
"Hello, World."
1601
01:13:10,940 --> 01:13:14,470
But what you don't have to do is type esoteric commands and weird syntax,
1602
01:13:14,470 --> 01:13:16,720
those curly braces and parentheses I keep alluding to.
1603
01:13:16,720 --> 01:13:18,970
You just drag this yellow puzzle piece.
1604
01:13:18,970 --> 01:13:20,530
You drag this purple puzzle piece.
1605
01:13:20,530 --> 01:13:22,810
Let them magnetically lock together, so to speak.
1606
01:13:22,810 --> 01:13:24,430
Click a button and boom.
1607
01:13:24,430 --> 01:13:27,010
With those same building blocks and several others,
1608
01:13:27,010 --> 01:13:31,010
can you make exactly the sorts of things that Andrew brought to life as well.
1609
01:13:31,010 --> 01:13:32,740
So here's what we're about to see.
1610
01:13:32,740 --> 01:13:38,500
At Scratch.MIT.edu is a cloud-based programming environment on MIT servers.
1611
01:13:38,500 --> 01:13:41,380
You can also download it offline on your own Mac or PC.
1612
01:13:41,380 --> 01:13:43,870
And it gives you an interface like this.
1613
01:13:43,870 --> 01:13:47,770
On the left-hand side of the screen, you'll see a blocks palette.
1614
01:13:47,770 --> 01:13:51,100
These puzzle pieces, a.k.a. blocks, come in different colors
1615
01:13:51,100 --> 01:13:52,540
which rather categorize them.
1616
01:13:52,540 --> 01:13:54,730
So pictured here, for instance, in blue, are
1617
01:13:54,730 --> 01:13:57,040
a whole bunch of motion-related blocks.
1618
01:13:57,040 --> 01:14:00,100
So Andrew used a whole bunch of those to have the singer and the men
1619
01:14:00,100 --> 01:14:02,950
moving around on the screen in synchronicity
1620
01:14:02,950 --> 01:14:05,110
with the song that was playing in the background.
1621
01:14:05,110 --> 01:14:09,310
Meanwhile, in the middle of this interface is going to be the code area.
1622
01:14:09,310 --> 01:14:12,100
And this is where Andrew, and soon you, will drag and drop
1623
01:14:12,100 --> 01:14:14,450
some of those puzzle pieces and other colors as well
1624
01:14:14,450 --> 01:14:17,560
and lock them together to get your character-- soon
1625
01:14:17,560 --> 01:14:20,110
to be invented-- to do something on the screen.
1626
01:14:20,110 --> 01:14:23,380
Indeed, at the bottom right here, will you see, ultimately,
1627
01:14:23,380 --> 01:14:26,260
a sprite area, where a sprite is a technical term
1628
01:14:26,260 --> 01:14:29,650
for like a character in a video game or a programming environment like this.
1629
01:14:29,650 --> 01:14:34,645
By default, historically, Scratch is the cat, the mascot, if you will,
1630
01:14:34,645 --> 01:14:36,020
for this programming environment.
1631
01:14:36,020 --> 01:14:38,110
And so here we see, by default, just one sprite
1632
01:14:38,110 --> 01:14:42,490
selected because on the top right of the screen is the stage for that sprite.
1633
01:14:42,490 --> 01:14:45,310
And you can click and zoom in to make it full screen.
1634
01:14:45,310 --> 01:14:47,350
But this is the world in which Scratch--
1635
01:14:47,350 --> 01:14:49,120
by default, the cat-- will live.
1636
01:14:49,120 --> 01:14:52,960
But you can change Scratch's costume so that it looks like a singer or the man
1637
01:14:52,960 --> 01:14:55,185
falling from the sky or the like or anything else,
1638
01:14:55,185 --> 01:14:58,060
either creating the art yourself or importing some of the things that
1639
01:14:58,060 --> 01:15:00,260
come with it or elsewhere online.
1640
01:15:00,260 --> 01:15:03,640
So what is this world that Scratch rather lives in?
1641
01:15:03,640 --> 01:15:06,610
Well, generally speaking, we won't have to care too much about numbers
1642
01:15:06,610 --> 01:15:09,820
because we'll be able to ask questions, like interactive ones,
1643
01:15:09,820 --> 01:15:12,040
like is Scratch the cat, or any character
1644
01:15:12,040 --> 01:15:15,520
otherwise, touching the edge of the screen, touching something else?
1645
01:15:15,520 --> 01:15:20,560
But Scratch does exist in this two-dimensional coordinate-system
1646
01:15:20,560 --> 01:15:21,080
world.
1647
01:15:21,080 --> 01:15:24,010
So when the cat or any character is dead center in the middle,
1648
01:15:24,010 --> 01:15:27,700
that would be xy location 0,0, if you will.
1649
01:15:27,700 --> 01:15:32,990
Meanwhile, over here is 240 pixels, or dots, all the way to the right.
1650
01:15:32,990 --> 01:15:37,700
So this would be 240,0, where y is 0 because it's right on that midline.
1651
01:15:37,700 --> 01:15:39,460
So it's neither up or below.
1652
01:15:39,460 --> 01:15:43,330
Over here to the left, of course, would be -240 and 0.
1653
01:15:43,330 --> 01:15:45,910
Above the cat would be x equals 0, because it's
1654
01:15:45,910 --> 01:15:48,880
right on that vertical midline, and 180.
1655
01:15:48,880 --> 01:15:52,152
And then down here, as you might guess, would be 0, negative 180.
1656
01:15:52,152 --> 01:15:53,860
Generally speaking, we don't have to care
1657
01:15:53,860 --> 01:15:55,733
about those precise pixel coordinates.
1658
01:15:55,733 --> 01:15:58,900
But it's helpful, ultimately, if you do want the cat to move up, down, left,
1659
01:15:58,900 --> 01:15:59,400
or right.
1660
01:15:59,400 --> 01:16:03,130
Having some sense of direction according to the x-axis and y-axis
1661
01:16:03,130 --> 01:16:06,010
as well can help you express your ideas, ultimately.
1662
01:16:06,010 --> 01:16:08,680
So what might some of those ideas be?
1663
01:16:08,680 --> 01:16:09,950
Well, let's do this.
1664
01:16:09,950 --> 01:16:15,340
I'm going to go ahead and create, on Scratch.MIT.edu, just
1665
01:16:15,340 --> 01:16:18,293
an empty screen like this one here.
1666
01:16:18,293 --> 01:16:19,960
And so this is the exact same interface.
1667
01:16:19,960 --> 01:16:24,490
But now I'm in my browser, full screen, so that I can start writing some code.
1668
01:16:24,490 --> 01:16:27,222
And let's get that cat to say something actually on the screen.
1669
01:16:27,222 --> 01:16:28,930
Now, this takes a little bit of practice.
1670
01:16:28,930 --> 01:16:32,200
But honestly, just by scrolling through these puzzle pieces can
1671
01:16:32,200 --> 01:16:35,830
you quickly get a sense of what's possible, not just categorically,
1672
01:16:35,830 --> 01:16:36,538
but specifically.
1673
01:16:36,538 --> 01:16:39,163
And I'll jump around because I've done this, of course, before.
1674
01:16:39,163 --> 01:16:41,410
But I'm going to go to events, in yellow, first.
1675
01:16:41,410 --> 01:16:44,080
And I'm going to drag and drop this first block, called
1676
01:16:44,080 --> 01:16:45,475
when Green Flag clicked.
1677
01:16:45,475 --> 01:16:48,100
And I've zoomed in there just to make it a little more legible.
1678
01:16:48,100 --> 01:16:50,380
And notice that the shape of this Green Flag
1679
01:16:50,380 --> 01:16:54,070
just so happens to mirror this Green Flag here
1680
01:16:54,070 --> 01:16:57,070
at top, next to this red Stop Sign, of sorts.
1681
01:16:57,070 --> 01:16:59,800
And the Green Flag is going to mean go and the red Stop Sign
1682
01:16:59,800 --> 01:17:02,320
is going to mean stop, to start or stop our program.
1683
01:17:02,320 --> 01:17:05,320
Next week, you're going to be writing a textual command at your keyboard
1684
01:17:05,320 --> 01:17:06,560
to do the exact same idea.
1685
01:17:06,560 --> 01:17:07,790
But for now, it's a button.
1686
01:17:07,790 --> 01:17:10,667
So when Green Flag clicked, what do I want Scratch to do?
1687
01:17:10,667 --> 01:17:13,750
Well, how about we have Scratch just initially say something like, "Hello,
1688
01:17:13,750 --> 01:17:16,960
world," which indeed, historically, is the first program
1689
01:17:16,960 --> 01:17:18,790
that most any programmer might write.
1690
01:17:18,790 --> 01:17:22,090
So anything related to what the cat looks like it's doing
1691
01:17:22,090 --> 01:17:24,490
is actually going to be under looks, here in purple.
1692
01:17:24,490 --> 01:17:27,100
So I'm going to drag over Say "hello."
1693
01:17:27,100 --> 01:17:30,820
And you'll notice something curious and different about this purple block.
1694
01:17:30,820 --> 01:17:33,400
It says, of course, "Say" in purple.
1695
01:17:33,400 --> 01:17:37,450
But then there's this white oval and some text that, by default, is "hello"
1696
01:17:37,450 --> 01:17:40,180
because MIT just decided that, by default, the placeholder will
1697
01:17:40,180 --> 01:17:40,810
be "hello."
1698
01:17:40,810 --> 01:17:42,940
But anytime you see this white oval, it's
1699
01:17:42,940 --> 01:17:48,790
an opportunity to provide an input into the function called Say.
1700
01:17:48,790 --> 01:17:50,980
And so here I'm borrowing terminology from before.
1701
01:17:50,980 --> 01:17:54,280
Problem solving, again, is all about inputs producing outputs.
1702
01:17:54,280 --> 01:17:56,045
And in between there is some algorithm.
1703
01:17:56,045 --> 01:17:58,420
In a moment, we're going to start referring to algorithms
1704
01:17:58,420 --> 01:18:00,470
quite frequently as "functions."
1705
01:18:00,470 --> 01:18:00,970
Why?
1706
01:18:00,970 --> 01:18:03,350
Because it's the implementation of some algorithm.
1707
01:18:03,350 --> 01:18:06,640
So let me override the default with, "Hello, world."
1708
01:18:06,640 --> 01:18:07,690
I'll zoom out.
1709
01:18:07,690 --> 01:18:11,260
And now if I go to the top right of the screen and click the Green Flag,
1710
01:18:11,260 --> 01:18:15,310
we'll see, hopefully, my very first program in code.
1711
01:18:15,310 --> 01:18:17,190
Now, it wasn't a huge lift, right?
1712
01:18:17,190 --> 01:18:19,690
It only was a matter of dragging and dropping puzzle pieces.
1713
01:18:19,690 --> 01:18:21,070
But what has now happened?
1714
01:18:21,070 --> 01:18:23,530
Well, it turns out that two things have happened.
1715
01:18:23,530 --> 01:18:26,710
When I, the human, clicked on that Green Flag,
1716
01:18:26,710 --> 01:18:29,570
I triggered, what we're going to start calling now, an "event."
1717
01:18:29,570 --> 01:18:32,800
An event is generally something graphical or interactive that
1718
01:18:32,800 --> 01:18:34,580
just happens in a computer program.
1719
01:18:34,580 --> 01:18:37,600
You and I trigger events on our phones all day long.
1720
01:18:37,600 --> 01:18:41,260
Whenever you tap or drag or long press or pinch
1721
01:18:41,260 --> 01:18:44,110
or any of those gestures in vogue nowadays on phones,
1722
01:18:44,110 --> 01:18:45,460
you are triggering events.
1723
01:18:45,460 --> 01:18:47,680
And people at Apple and Google and elsewhere
1724
01:18:47,680 --> 01:18:50,320
have written code that listen for those events
1725
01:18:50,320 --> 01:18:52,880
and do something when that event happens.
1726
01:18:52,880 --> 01:18:54,010
That's what I just did.
1727
01:18:54,010 --> 01:18:56,560
When Green Flag is clicked, I want something
1728
01:18:56,560 --> 01:19:01,150
to happen, namely, I want this purple function, this verb, this action called
1729
01:19:01,150 --> 01:19:02,910
Say, to do something.
1730
01:19:02,910 --> 01:19:03,910
What do I want it to do?
1731
01:19:03,910 --> 01:19:05,590
I want it to say what this input is.
1732
01:19:05,590 --> 01:19:07,900
And I'm going to introduce another vocabulary term.
1733
01:19:07,900 --> 01:19:11,350
The white ovals here are, yes, inputs, very generically.
1734
01:19:11,350 --> 01:19:13,660
But in a programmer's terminology, they're
1735
01:19:13,660 --> 01:19:16,080
called "arguments," otherwise known as "parameters."
1736
01:19:16,080 --> 01:19:17,830
And that just means an input to a function
1737
01:19:17,830 --> 01:19:20,050
that modifies its behavior in some way.
1738
01:19:20,050 --> 01:19:22,253
When I click Stop, that's just another event.
1739
01:19:22,253 --> 01:19:23,920
And that one is just built into Scratch.
1740
01:19:23,920 --> 01:19:26,740
Scratch knows that when you click the green Stop Sign,
1741
01:19:26,740 --> 01:19:28,490
everything should just stop automatically.
1742
01:19:28,490 --> 01:19:30,950
I don't have to write code to support that feature.
1743
01:19:30,950 --> 01:19:33,470
So that's all fine and good, "Hello, world."
1744
01:19:33,470 --> 01:19:36,880
But if I keep doing stop and start and stop and start,
1745
01:19:36,880 --> 01:19:39,130
it's going to do the same thing again and again.
1746
01:19:39,130 --> 01:19:41,650
And it's really not that interesting, at the end of the day,
1747
01:19:41,650 --> 01:19:45,350
maybe gratifying once, but it'd be nice if this were a little more interactive.
1748
01:19:45,350 --> 01:19:48,140
So it turns out that we can do that too.
1749
01:19:48,140 --> 01:19:50,450
But we need a different mental model instead.
1750
01:19:50,450 --> 01:19:53,740
So in this case here, when we think about this function,
1751
01:19:53,740 --> 01:19:56,230
Say, in this input, "Hello, world," this actually
1752
01:19:56,230 --> 01:19:58,300
maps pretty cleanly to this model earlier,
1753
01:19:58,300 --> 01:20:01,870
that I propose is problem solving, is computer science, if you will.
1754
01:20:01,870 --> 01:20:05,770
The input to the current problem is going to be in white here,
1755
01:20:05,770 --> 01:20:06,760
"Hello, world."
1756
01:20:06,760 --> 01:20:10,300
The algorithm is the "say" algorithm.
1757
01:20:10,300 --> 01:20:13,420
Now, I don't know how MIT got it to print out the little, pretty speech
1758
01:20:13,420 --> 01:20:14,330
bubble on the screen.
1759
01:20:14,330 --> 01:20:17,770
But they wrote those underlying low-level implementation details.
1760
01:20:17,770 --> 01:20:21,130
And they gave me and you a purple function, called Say,
1761
01:20:21,130 --> 01:20:22,630
that just does that for you.
1762
01:20:22,630 --> 01:20:24,670
You and I don't have to reinvent that wheel.
1763
01:20:24,670 --> 01:20:28,580
The output of Say is another technical term, now, called a "side effect."
1764
01:20:28,580 --> 01:20:31,570
A side effect is usually something visual that happens,
1765
01:20:31,570 --> 01:20:34,008
like as a side effect of you calling a function.
1766
01:20:34,008 --> 01:20:35,800
And so the side effect here is that the cat
1767
01:20:35,800 --> 01:20:39,800
has this speech bubble magically appear, inside of which is "Hello, world."
1768
01:20:39,800 --> 01:20:41,200
So we have an input.
1769
01:20:41,200 --> 01:20:42,370
We have an output.
1770
01:20:42,370 --> 01:20:43,540
We have an algorithm.
1771
01:20:43,540 --> 01:20:46,910
But now we're talking about these ideas in the context of programming.
1772
01:20:46,910 --> 01:20:49,420
So now the input is an "argument."
1773
01:20:49,420 --> 01:20:51,400
The algorithm is a "function."
1774
01:20:51,400 --> 01:20:53,840
And the output, in this case, is a "side effect"--
1775
01:20:53,840 --> 01:20:55,840
terminology that you'll just hear more and more.
1776
01:20:55,840 --> 01:20:59,200
And it'll eventually sink in, but not to worry if the terminology doesn't
1777
01:20:59,200 --> 01:21:00,800
come naturally early on.
1778
01:21:00,800 --> 01:21:03,890
So what more might I do with this?
1779
01:21:03,890 --> 01:21:07,077
Let me go back to Scratch here and make this maybe perhaps more interactive
1780
01:21:07,077 --> 01:21:09,910
and actually get the cat to say something a little more dynamically.
1781
01:21:09,910 --> 01:21:11,740
So instead of "Hello, world," why don't we
1782
01:21:11,740 --> 01:21:15,050
get him to say hello to me or to you or anyone else?
1783
01:21:15,050 --> 01:21:16,010
So let me do this.
1784
01:21:16,010 --> 01:21:18,970
Let me go under, say--
1785
01:21:18,970 --> 01:21:20,200
let me get rid of this first.
1786
01:21:20,200 --> 01:21:22,090
And you'll notice this neat trick.
1787
01:21:22,090 --> 01:21:25,090
As soon as you start dragging on a block, if it gets close to it,
1788
01:21:25,090 --> 01:21:28,480
it kind of goes gray, and it can be magnetically snapped together.
1789
01:21:28,480 --> 01:21:30,220
You don't have to do it very precisely.
1790
01:21:30,220 --> 01:21:32,345
Conversely, if I want to get rid of a puzzle piece,
1791
01:21:32,345 --> 01:21:35,800
I can just drag it anywhere on the left, let go, and that deletes it.
1792
01:21:35,800 --> 01:21:38,500
Or you can Right-click or Control-click and a little menu
1793
01:21:38,500 --> 01:21:39,902
will let you delete it as well.
1794
01:21:39,902 --> 01:21:41,110
Well, let me do this instead.
1795
01:21:41,110 --> 01:21:44,310
Under Sensing, which I know is there because I've done this before,
1796
01:21:44,310 --> 01:21:48,180
are a whole bunch of things related to Sensing, whereby the cat can kind
1797
01:21:48,180 --> 01:21:50,700
of feel out its world, in some sense.
1798
01:21:50,700 --> 01:21:54,360
It can do things like ask this question, "Am I touching the mouse pointer?"--
1799
01:21:54,360 --> 01:21:55,620
like the user's cursor.
1800
01:21:55,620 --> 01:21:59,010
"Am I touching a specific color that you can override to be anything you want?"
1801
01:21:59,010 --> 01:22:02,500
"Is the distance to the mouse pointer some specific value?"
1802
01:22:02,500 --> 01:22:05,940
But for now, I'm going to focus on this, this blue puzzle piece that
1803
01:22:05,940 --> 01:22:09,570
asks a question, which itself is this white oval that I can apparently
1804
01:22:09,570 --> 01:22:12,030
change, and then it's going to wait for a response.
1805
01:22:12,030 --> 01:22:14,443
But this puzzle piece is a little different.
1806
01:22:14,443 --> 01:22:15,360
It's a little special.
1807
01:22:15,360 --> 01:22:16,590
It comes with a freebie.
1808
01:22:16,590 --> 01:22:19,690
It comes with what we're going to call, technically, a "return value."
1809
01:22:19,690 --> 01:22:22,590
So some functions don't just do something on the screen.
1810
01:22:22,590 --> 01:22:25,620
They hand you back, so to speak, a value that you
1811
01:22:25,620 --> 01:22:27,570
can do anything that you want with.
1812
01:22:27,570 --> 01:22:29,820
Nothing happens immediately unless you do something
1813
01:22:29,820 --> 01:22:31,750
with that so-called return value.
1814
01:22:31,750 --> 01:22:35,633
So let me go ahead and drag this thing over here, ask, "What's your name?"
1815
01:22:35,633 --> 01:22:37,050
And I'll use the default question.
1816
01:22:37,050 --> 01:22:38,675
That seems a reasonable place to start.
1817
01:22:38,675 --> 01:22:41,970
I'm not going to override that default. And now let me go ahead and zoom out.
1818
01:22:41,970 --> 01:22:43,890
Let me go back to Looks.
1819
01:22:43,890 --> 01:22:46,320
Let me go to Say.
1820
01:22:46,320 --> 01:22:48,850
And let me just form the English sentence I want.
1821
01:22:48,850 --> 01:22:53,730
So let me zoom in here and type in "hello," maybe comma, space.
1822
01:22:53,730 --> 01:22:56,580
I could do "David," but that's obviously not right
1823
01:22:56,580 --> 01:22:58,830
because I'm asking for a name, and then I'm like,
1824
01:22:58,830 --> 01:23:00,270
in advance, hard-coding my name.
1825
01:23:00,270 --> 01:23:01,228
That's not what I want.
1826
01:23:01,228 --> 01:23:02,610
I just want, "hello," comma.
1827
01:23:02,610 --> 01:23:06,180
And now let me zoom out and grab one more Say block.
1828
01:23:06,180 --> 01:23:07,500
Let me maybe Say here.
1829
01:23:07,500 --> 01:23:09,180
OK, I don't want to say, "Hello, hello."
1830
01:23:09,180 --> 01:23:11,722
I don't want to just type in my own name because, again, then
1831
01:23:11,722 --> 01:23:14,230
what's the point of asking the user for their name?
1832
01:23:14,230 --> 01:23:15,300
But notice this.
1833
01:23:15,300 --> 01:23:19,950
If I go back to the sensing block, this is where that oval that's blue,
1834
01:23:19,950 --> 01:23:22,080
called Answer, is useful.
1835
01:23:22,080 --> 01:23:25,280
This will be the so-called "return value" of that function.
1836
01:23:25,280 --> 01:23:27,780
So I'm just going to go ahead and do this and drag and drop.
1837
01:23:27,780 --> 01:23:30,600
Even though it's not the right size, it is the right shape.
1838
01:23:30,600 --> 01:23:32,670
And so Scratch will be smart about it and grow
1839
01:23:32,670 --> 01:23:34,620
to fill that puzzle piece for you.
1840
01:23:34,620 --> 01:23:35,820
Let me zoom out now.
1841
01:23:35,820 --> 01:23:37,607
And now let me click the Green Flag.
1842
01:23:37,607 --> 01:23:40,440
You'll see that Scratch is indeed prompting me with a speech bubble,
1843
01:23:40,440 --> 01:23:41,232
"What's your name?"
1844
01:23:41,232 --> 01:23:44,320
Notice the little text box below the cat is asking, what's your name?
1845
01:23:44,320 --> 01:23:46,590
So I'm going to type in D-A-V-I-D and hit Enter.
1846
01:23:46,590 --> 01:23:48,120
Or I can click the blue check.
1847
01:23:48,120 --> 01:23:50,310
Enter.
1848
01:23:50,310 --> 01:23:51,802
OK, it's a little weird.
1849
01:23:51,802 --> 01:23:53,760
I wanted him to say, "hello," not just my name.
1850
01:23:53,760 --> 01:23:55,020
So let me Stop.
1851
01:23:55,020 --> 01:23:56,190
Let me start it again.
1852
01:23:56,190 --> 01:23:57,648
All right, hello, what's your name?
1853
01:23:57,648 --> 01:24:00,230
D-A-V-I-D. Enter.
1854
01:24:00,230 --> 01:24:02,490
Huh-- kind of rude.
1855
01:24:02,490 --> 01:24:04,890
Why is there this bug?
1856
01:24:04,890 --> 01:24:07,920
Like, I wanted to say, "Hello, David," not just "David."
1857
01:24:07,920 --> 01:24:11,550
And yet twice it has failed to do so.
1858
01:24:11,550 --> 01:24:12,560
Yeah--
1859
01:24:12,560 --> 01:24:13,577
AUDIENCE: [INAUDIBLE]
1860
01:24:13,577 --> 01:24:16,160
DAVID J. MALAN: Yeah, the computer's processing my directions,
1861
01:24:16,160 --> 01:24:17,658
my actions, really quickly.
1862
01:24:17,658 --> 01:24:18,950
And so it actually is doing it.
1863
01:24:18,950 --> 01:24:20,510
It's just, you and I, in the room, are just way
1864
01:24:20,510 --> 01:24:23,220
too slow to notice that it said-- (QUICKLY) "Hello, David."
1865
01:24:23,220 --> 01:24:25,470
It just seems to have just said, "David."
1866
01:24:25,470 --> 01:24:26,910
So all right, how can I fix this?
1867
01:24:26,910 --> 01:24:28,640
Well, here's where you start to poke around and think
1868
01:24:28,640 --> 01:24:29,940
about how you might solve this.
1869
01:24:29,940 --> 01:24:31,100
Let me go back under Looks.
1870
01:24:31,100 --> 01:24:33,330
Maybe there's a smarter way to do this.
1871
01:24:33,330 --> 01:24:34,520
Maybe I could do--
1872
01:24:34,520 --> 01:24:35,540
OK, I could do this.
1873
01:24:35,540 --> 01:24:37,490
How about instead of just Say "hello," there's
1874
01:24:37,490 --> 01:24:41,060
apparently another puzzle piece where I can time it so I can maybe slow things
1875
01:24:41,060 --> 01:24:42,150
down a little bit.
1876
01:24:42,150 --> 01:24:43,110
So let me do this.
1877
01:24:43,110 --> 01:24:45,810
Let me throw away all of this.
1878
01:24:45,810 --> 01:24:49,140
Let me drag a Say "hello" for 2 seconds.
1879
01:24:49,140 --> 01:24:51,870
Let me drag another Say "hello" for 2 seconds.
1880
01:24:51,870 --> 01:24:54,500
Let me change the first one to, indeed, "hello" comma.
1881
01:24:54,500 --> 01:24:57,530
And then let me go back to Sensing.
1882
01:24:57,530 --> 01:25:00,460
Let me grab that same answer because I threw it away a second ago,
1883
01:25:00,460 --> 01:25:01,460
and I'll just change it.
1884
01:25:01,460 --> 01:25:02,960
I don't even have to delete "hello."
1885
01:25:02,960 --> 01:25:04,740
I can just overwrite it like this.
1886
01:25:04,740 --> 01:25:08,360
So now I think we'll kind of pump the brakes and see things more slowly.
1887
01:25:08,360 --> 01:25:09,620
Let me Stop.
1888
01:25:09,620 --> 01:25:10,550
Let me start.
1889
01:25:10,550 --> 01:25:12,050
D-A-V-I-D, Enter.
1890
01:25:12,050 --> 01:25:14,600
Hello, David.
1891
01:25:14,600 --> 01:25:18,120
OK, so it's better, like it seems to be working.
1892
01:25:18,120 --> 01:25:21,255
I think your hypothesis was right, just looks kind of stupid, right?
1893
01:25:21,255 --> 01:25:22,880
Like, the fact that it's saying Hello--
1894
01:25:22,880 --> 01:25:24,170
[PAUSE]
1895
01:25:24,170 --> 01:25:25,640
--David, like we can do better.
1896
01:25:25,640 --> 01:25:29,000
And like, literally every piece of software on your phone or Mac or PC
1897
01:25:29,000 --> 01:25:29,930
is better than that.
1898
01:25:29,930 --> 01:25:33,750
It adds words together in the user interfaces you and I are familiar with.
1899
01:25:33,750 --> 01:25:35,480
So let's go a little more fishing here.
1900
01:25:35,480 --> 01:25:36,950
Let me throw away these.
1901
01:25:36,950 --> 01:25:39,860
Let me go back to Looks and just get the simpler Say.
1902
01:25:39,860 --> 01:25:44,060
I want this to say, "Hello" comma name, where name
1903
01:25:44,060 --> 01:25:45,720
comes from that Answer return value.
1904
01:25:45,720 --> 01:25:46,740
So how can I do this?
1905
01:25:46,740 --> 01:25:49,440
Well, let me go under Operations, which we haven't been before.
1906
01:25:49,440 --> 01:25:50,732
There's a lot of stuff in here.
1907
01:25:50,732 --> 01:25:53,720
Some of it's mathematically related, adding, subtracting, and so forth.
1908
01:25:53,720 --> 01:25:56,280
You can generate random numbers which might be useful.
1909
01:25:56,280 --> 01:25:59,810
And if I keep scrolling down, there's this Join "apple" and "banana."
1910
01:25:59,810 --> 01:26:01,280
But that's just placeholder text.
1911
01:26:01,280 --> 01:26:04,837
You can join one piece of text with another piece of text,
1912
01:26:04,837 --> 01:26:06,170
by default "apple" and "banana."
1913
01:26:06,170 --> 01:26:08,480
But let's change it to "hello" and my name.
1914
01:26:08,480 --> 01:26:11,120
So this, too, wrong size but right shape.
1915
01:26:11,120 --> 01:26:13,190
So let me let it snap into place.
1916
01:26:13,190 --> 01:26:15,920
Let me go ahead now and do "hello" comma.
1917
01:26:15,920 --> 01:26:20,840
And now I think I just want to go grab that Answer return value.
1918
01:26:20,840 --> 01:26:23,780
Let me drag the same oval as before, clobber--
1919
01:26:23,780 --> 01:26:25,280
that is, overwrite-- banana.
1920
01:26:25,280 --> 01:26:28,430
So now I'm kind of composing functions.
1921
01:26:28,430 --> 01:26:31,700
The output of one function, Join, is going
1922
01:26:31,700 --> 01:26:33,972
to be the input of another function, Say.
1923
01:26:33,972 --> 01:26:36,680
So let's see what happens now that they're kind of stacked on top
1924
01:26:36,680 --> 01:26:38,660
of each other or nested, so to speak.
1925
01:26:38,660 --> 01:26:42,440
Click the Green Flag, D-A-V-I-D. Enter.
1926
01:26:42,440 --> 01:26:43,357
"Hello, David."
1927
01:26:43,357 --> 01:26:44,690
All right, that was pretty fast.
1928
01:26:44,690 --> 01:26:45,680
Let's just do it once more.
1929
01:26:45,680 --> 01:26:46,310
Stop.
1930
01:26:46,310 --> 01:26:47,270
Start.
1931
01:26:47,270 --> 01:26:49,545
Here we go, D-A-V-I-D. Enter.
1932
01:26:49,545 --> 01:26:50,045
OK.
1933
01:26:50,045 --> 01:26:52,650
All right, it's not the most exciting program in the world.
1934
01:26:52,650 --> 01:26:53,750
But it's more correct.
1935
01:26:53,750 --> 01:26:56,090
It's better designed just because that's what you would kind of expect
1936
01:26:56,090 --> 01:26:59,450
the software to do and not be some kind of lame user interface that's just
1937
01:26:59,450 --> 01:27:02,540
inserting random delays to just make it kind of work, like that's
1938
01:27:02,540 --> 01:27:03,962
a workaround, a hack, if you will.
1939
01:27:03,962 --> 01:27:06,170
But there's some cool things you can do with Scratch.
1940
01:27:06,170 --> 01:27:08,030
And we won't really go down the rabbit hole
1941
01:27:08,030 --> 01:27:10,940
of all of the fun and family-friendly features that it has.
1942
01:27:10,940 --> 01:27:12,810
But there is one that's kind of cool here.
1943
01:27:12,810 --> 01:27:16,550
Let me go into the Extensions button at the bottom left of my screen.
1944
01:27:16,550 --> 01:27:17,720
And this one's kind of cool.
1945
01:27:17,720 --> 01:27:19,575
Let me go to Text to Speech.
1946
01:27:19,575 --> 01:27:22,700
And you'll notice that this one requires internet because it's cloud based.
1947
01:27:22,700 --> 01:27:27,715
But this just gave me some new puzzle pieces in a new category,
1948
01:27:27,715 --> 01:27:28,340
Text to Speech.
1949
01:27:28,340 --> 01:27:30,710
And these green ones do exactly what they say.
1950
01:27:30,710 --> 01:27:31,860
So let me do this.
1951
01:27:31,860 --> 01:27:33,120
Let me zoom out again.
1952
01:27:33,120 --> 01:27:34,543
Let me keep the Join block.
1953
01:27:34,543 --> 01:27:36,710
And I'm just going to temporarily toss it over here.
1954
01:27:36,710 --> 01:27:39,252
It's not going to delete itself because I didn't drag it over
1955
01:27:39,252 --> 01:27:40,070
to the other side.
1956
01:27:40,070 --> 01:27:42,800
But I'm going to get rid of the Say block, in purple.
1957
01:27:42,800 --> 01:27:47,570
I'm going to do the Speak block here, in green, and let it snap into place.
1958
01:27:47,570 --> 01:27:50,990
And then I'm going to drag and drop this onto the input to Speak.
1959
01:27:50,990 --> 01:27:54,290
And now, perhaps a little more adorably, let's try this.
1960
01:27:54,290 --> 01:27:56,270
Green Flag, what's your name?
1961
01:27:56,270 --> 01:27:57,710
D-A-V-I-D. Enter.
1962
01:27:57,710 --> 01:27:58,460
And--
1963
01:27:58,460 --> 01:28:00,578
COMPUTER: Hello, David.
1964
01:28:00,578 --> 01:28:01,370
DAVID J. MALAN: OK.
1965
01:28:01,370 --> 01:28:02,370
[LAUGHTER]
1966
01:28:02,370 --> 01:28:03,890
It's a little robotic.
1967
01:28:03,890 --> 01:28:05,917
But at least now it has synthesized speech.
1968
01:28:05,917 --> 01:28:08,750
And I've kind of got my own, like, Siri or Google Assistant or Alexa
1969
01:28:08,750 --> 01:28:12,020
thing going on here now, where it's now recognized whatever text
1970
01:28:12,020 --> 01:28:13,550
it is, and it's played it.
1971
01:28:13,550 --> 01:28:17,480
Well, let's make this an actual cat that doesn't talk in that weird human voice.
1972
01:28:17,480 --> 01:28:19,610
Let me go ahead and get rid of most of this stuff.
1973
01:28:19,610 --> 01:28:22,730
And let's get the cat to actually meow, like a cat tends to.
1974
01:28:22,730 --> 01:28:24,650
And let me go under the Sounds block.
1975
01:28:24,650 --> 01:28:28,343
Now MIT gives you a few sounds for free because it's designed around a cat,
1976
01:28:28,343 --> 01:28:30,260
by default. And I'm going to go ahead and grab
1977
01:28:30,260 --> 01:28:33,080
this one, Play Sound Meow until done.
1978
01:28:33,080 --> 01:28:36,860
And now-- and we heard a teaser for this earlier in the crowd--
1979
01:28:36,860 --> 01:28:38,982
[MEOW]
1980
01:28:38,982 --> 01:28:40,440
It's a little piercing, admittedly.
1981
01:28:40,440 --> 01:28:42,460
Maybe we can lower the volume a little bit there.
1982
01:28:42,460 --> 01:28:45,710
But notice, if I want the cat to meow a second time, I'll just click it again.
1983
01:28:45,710 --> 01:28:46,905
[MEOW]
1984
01:28:46,905 --> 01:28:48,690
OK, and over there, too, I hear--
1985
01:28:48,690 --> 01:28:50,320
[MEOW]
1986
01:28:50,320 --> 01:28:50,820
OK.
1987
01:28:50,820 --> 01:28:51,320
[MEOW]
1988
01:28:51,320 --> 01:28:53,500
All right, so it's kind of cute now, right?
1989
01:28:53,500 --> 01:28:54,960
So it's just meow--
1990
01:28:54,960 --> 01:28:56,610
OK, yes, echo, echo.
1991
01:28:56,610 --> 01:28:59,767
So it's meowing now every time I hit the Green Flag.
1992
01:28:59,767 --> 01:29:02,100
Now, that's great, but even a kid is probably going to--
1993
01:29:02,100 --> 01:29:02,730
[MEOW]
1994
01:29:02,730 --> 01:29:03,780
--like would prefer that it--
1995
01:29:03,780 --> 01:29:03,840
[MEOW]
1996
01:29:03,840 --> 01:29:06,790
--just meow, perhaps, like again and again, without having to keep--
1997
01:29:06,790 --> 01:29:07,290
[MEOW]
1998
01:29:07,290 --> 01:29:08,165
--hitting the button.
1999
01:29:08,165 --> 01:29:09,880
So how might we do this?
2000
01:29:09,880 --> 01:29:13,470
All right, well, if I want it to meow multiple times, why don't I just, like,
2001
01:29:13,470 --> 01:29:16,570
grab it another time and another time?
2002
01:29:16,570 --> 01:29:19,350
Alternatively, you can Right-click or Control-click a puzzle piece
2003
01:29:19,350 --> 01:29:21,725
and just duplicate it from a little menu that drops down.
2004
01:29:21,725 --> 01:29:22,920
So here we go, three meows.
2005
01:29:22,920 --> 01:29:25,090
[MEOWING]
2006
01:29:25,090 --> 01:29:25,960
2007
01:29:25,960 --> 01:29:27,730
All right, that's not really a happy cat.
2008
01:29:27,730 --> 01:29:29,200
It sounds maybe hungry.
2009
01:29:29,200 --> 01:29:31,240
So can we slow that down?
2010
01:29:31,240 --> 01:29:32,380
Well, maybe.
2011
01:29:32,380 --> 01:29:34,630
In fact, if I poke around, let me go under Control.
2012
01:29:34,630 --> 01:29:36,850
It looks like there's a Wait block.
2013
01:29:36,850 --> 01:29:40,630
Wait 1 Second, by default. And notice, Scratch will be pretty accommodating.
2014
01:29:40,630 --> 01:29:44,410
If you just hover in between blocks, it will grow to fill that too.
2015
01:29:44,410 --> 01:29:47,095
So I could change it to 1 or 2 or anything, seconds.
2016
01:29:47,095 --> 01:29:48,970
I'll just leave it at the default for now, 1.
2017
01:29:48,970 --> 01:29:50,680
And now I'll go ahead and do this.
2018
01:29:50,680 --> 01:29:52,838
[MEOWING]
2019
01:29:52,838 --> 01:29:55,580
2020
01:29:55,580 --> 01:29:58,790
OK, so cuter and less hungry and just more friendly.
2021
01:29:58,790 --> 01:30:00,740
But this isn't the best design.
2022
01:30:00,740 --> 01:30:01,460
It is correct.
2023
01:30:01,460 --> 01:30:02,960
And let's use that as a term of art.
2024
01:30:02,960 --> 01:30:05,720
Correct means the code does what you want it to do.
2025
01:30:05,720 --> 01:30:08,090
I want the cat to meow three times slowly.
2026
01:30:08,090 --> 01:30:08,720
And it did.
2027
01:30:08,720 --> 01:30:10,370
So I'd wager this is correct.
2028
01:30:10,370 --> 01:30:12,380
But it's not the best design.
2029
01:30:12,380 --> 01:30:14,810
And this is where things get more subjective, right?
2030
01:30:14,810 --> 01:30:18,630
Like, you could write accurate sentences in an essay for an English class,
2031
01:30:18,630 --> 01:30:20,663
but otherwise, it's just completely a mess.
2032
01:30:20,663 --> 01:30:23,580
Like, your arguments here and there, and you don't say anything wrong,
2033
01:30:23,580 --> 01:30:24,830
but you don't say it well.
2034
01:30:24,830 --> 01:30:28,110
In the context of code, we can do better than this.
2035
01:30:28,110 --> 01:30:31,340
And Copy/Paste or repeating yourself again and again tends
2036
01:30:31,340 --> 01:30:32,340
to be bad practice.
2037
01:30:32,340 --> 01:30:32,840
Why?
2038
01:30:32,840 --> 01:30:37,188
Suppose that you want to change the Wait to 2 seconds instead of 1.
2039
01:30:37,188 --> 01:30:38,480
It's admittedly not a big deal.
2040
01:30:38,480 --> 01:30:40,400
Fine, I click there, I change it to 2.
2041
01:30:40,400 --> 01:30:41,990
I click there, I change it to 2.
2042
01:30:41,990 --> 01:30:43,820
But what if you meow 5 times, 10 times?
2043
01:30:43,820 --> 01:30:46,858
Now I have to change the Wait, like, in 5, 10 different places.
2044
01:30:46,858 --> 01:30:47,900
Like, that's just stupid.
2045
01:30:47,900 --> 01:30:51,395
It's taking unnecessary human time, and you're going to screw up eventually,
2046
01:30:51,395 --> 01:30:53,270
especially if your program is getting longer.
2047
01:30:53,270 --> 01:30:54,740
You're going to miss one of the inputs.
2048
01:30:54,740 --> 01:30:56,365
You're going to leave the number wrong.
2049
01:30:56,365 --> 01:30:57,080
And that's a bug.
2050
01:30:57,080 --> 01:30:59,030
So just based on what you've seen already
2051
01:30:59,030 --> 01:31:01,760
or if you've program before, which a few of you have,
2052
01:31:01,760 --> 01:31:04,050
what's the term of art here that will solve this?
2053
01:31:04,050 --> 01:31:06,095
How can we design this better?
2054
01:31:06,095 --> 01:31:07,230
AUDIENCE: [INAUDIBLE]
2055
01:31:07,230 --> 01:31:08,563
DAVID J. MALAN: I heard it here.
2056
01:31:08,563 --> 01:31:12,110
Yeah, so a loop-- a loop, some kind of cycle that says, do that again.
2057
01:31:12,110 --> 01:31:15,140
Do that again-- not infinitely many times, necessarily,
2058
01:31:15,140 --> 01:31:16,430
but some finite number.
2059
01:31:16,430 --> 01:31:18,740
Well, you can perhaps see a spoiler on the screen.
2060
01:31:18,740 --> 01:31:22,760
Under the same orange Control category is a Repeat block.
2061
01:31:22,760 --> 01:31:24,320
And by default, it's proposing 10.
2062
01:31:24,320 --> 01:31:25,440
But we can change that.
2063
01:31:25,440 --> 01:31:26,280
So let me do this.
2064
01:31:26,280 --> 01:31:29,930
I'm going to throw away most of this Copy/Paste as redundant.
2065
01:31:29,930 --> 01:31:33,470
I'm going to detach this temporarily just to make room for something else.
2066
01:31:33,470 --> 01:31:37,590
And I'm going to drag a Repeat block over here and let that snap into place.
2067
01:31:37,590 --> 01:31:40,340
And I'm going to change it for now, just to be 3, for consistency.
2068
01:31:40,340 --> 01:31:42,715
And this is the correct shape even though it's too small,
2069
01:31:42,715 --> 01:31:44,660
but Scratch will accommodate that for us.
2070
01:31:44,660 --> 01:31:50,370
And now-- same output but arguably better designed.
2071
01:31:50,370 --> 01:31:50,870
Why?
2072
01:31:50,870 --> 01:31:53,953
Because if I want to change the number of meows, I change it in one place,
2073
01:31:53,953 --> 01:31:55,040
no Copy/Paste messiness.
2074
01:31:55,040 --> 01:31:56,867
If I want to change the waiting, one place.
2075
01:31:56,867 --> 01:31:59,450
I don't have to change it in multiple places and not screw up.
2076
01:31:59,450 --> 01:32:00,920
So let me hit the Green Flag.
2077
01:32:00,920 --> 01:32:03,405
[MEOWING]
2078
01:32:03,405 --> 01:32:05,890
2079
01:32:05,890 --> 01:32:06,940
All right, so-- nice.
2080
01:32:06,940 --> 01:32:11,140
Now, it would have been nice if MIT had just given us a meow block that
2081
01:32:11,140 --> 01:32:12,970
just automates all of this for us.
2082
01:32:12,970 --> 01:32:15,910
Let me wager, they gave us the low-level implementation details.
2083
01:32:15,910 --> 01:32:18,400
They gave us the Play Sound Meow.
2084
01:32:18,400 --> 01:32:21,100
But I had to implement a decent number of blocks
2085
01:32:21,100 --> 01:32:23,120
just to get a cat to meow again and again.
2086
01:32:23,120 --> 01:32:26,350
I feel like we should have gotten that for free from MIT.
2087
01:32:26,350 --> 01:32:30,100
Well, they don't have to be the only ones that invent blocks for us to use.
2088
01:32:30,100 --> 01:32:33,920
You can write your own functions, your own verbs or actions.
2089
01:32:33,920 --> 01:32:35,180
So how can we do this?
2090
01:32:35,180 --> 01:32:39,640
Let's make our own puzzle piece, called Meow, that uses this code
2091
01:32:39,640 --> 01:32:42,650
but creates it in such a way that it's reusable elsewhere.
2092
01:32:42,650 --> 01:32:43,420
So let me do this.
2093
01:32:43,420 --> 01:32:46,660
Under my blocks in pink here, I'm going to go ahead and click,
2094
01:32:46,660 --> 01:32:48,310
literally, Make a Block.
2095
01:32:48,310 --> 01:32:51,100
Now, here's an interface by which I can give the block a name.
2096
01:32:51,100 --> 01:32:53,410
M-E-O-W will be the name of this block.
2097
01:32:53,410 --> 01:32:55,810
And I'm just going to go ahead and quickly click OK.
2098
01:32:55,810 --> 01:32:58,780
That just gives me a very generic, pink puzzle piece
2099
01:32:58,780 --> 01:33:01,390
that starts with the word Define because scratch
2100
01:33:01,390 --> 01:33:04,750
is asking me to define, that is, implement or create,
2101
01:33:04,750 --> 01:33:06,717
this new puzzle piece for me.
2102
01:33:06,717 --> 01:33:08,050
Well, what does it mean to Meow?
2103
01:33:08,050 --> 01:33:12,770
I'm going to claim that it means to do these two steps, to play the sound meow
2104
01:33:12,770 --> 01:33:14,890
and then just wait for 1 second.
2105
01:33:14,890 --> 01:33:18,520
But what's powerful about this idea is look at this up top.
2106
01:33:18,520 --> 01:33:21,970
Now that I've made a block, it exists in Scratch.
2107
01:33:21,970 --> 01:33:23,710
MIT didn't need to create this for me.
2108
01:33:23,710 --> 01:33:27,190
I created it for myself and even you, if we end up sharing code.
2109
01:33:27,190 --> 01:33:30,320
So I can now drag Meow up in here.
2110
01:33:30,320 --> 01:33:33,910
And what's nice about Meow is that itself is, yes, a function,
2111
01:33:33,910 --> 01:33:35,680
but it's also an abstraction.
2112
01:33:35,680 --> 01:33:40,060
Like, never again do I or even you need to worry or care about what
2113
01:33:40,060 --> 01:33:42,340
it means to meow or implement it.
2114
01:33:42,340 --> 01:33:43,930
I can sort of drag it out of the way.
2115
01:33:43,930 --> 01:33:45,790
I didn't delete it-- drag it out of the way.
2116
01:33:45,790 --> 01:33:46,970
Out of sight, out of mind.
2117
01:33:46,970 --> 01:33:47,470
Why?
2118
01:33:47,470 --> 01:33:49,945
Because my code is now even better designed, in some sense,
2119
01:33:49,945 --> 01:33:51,070
because it's more readable.
2120
01:33:51,070 --> 01:33:51,910
What is it doing?
2121
01:33:51,910 --> 01:33:55,000
When the Green Flag is clicked, repeat 3 times Meow.
2122
01:33:55,000 --> 01:33:56,305
It just says what it means.
2123
01:33:56,305 --> 01:33:58,180
And so it's a lot easier to read it, and it's
2124
01:33:58,180 --> 01:33:59,930
a lot easier to think about it, especially
2125
01:33:59,930 --> 01:34:03,040
if you're using Meow in other projects too.
2126
01:34:03,040 --> 01:34:05,380
Now, let me go ahead and click Play.
2127
01:34:05,380 --> 01:34:06,280
[MEOW]
2128
01:34:06,280 --> 01:34:07,200
Same thing.
2129
01:34:07,200 --> 01:34:07,700
[MEOW]
2130
01:34:07,700 --> 01:34:09,282
So it's not really fundamentally any different.
2131
01:34:09,282 --> 01:34:09,830
[MEOW]
2132
01:34:09,830 --> 01:34:13,840
But I can make this custom puzzle piece, this own function of mine, Meow,
2133
01:34:13,840 --> 01:34:15,010
even more powerful.
2134
01:34:15,010 --> 01:34:18,520
Let me kind of rewind a bit and go to my Meow puzzle piece.
2135
01:34:18,520 --> 01:34:21,658
And I am going to Control-click or Right-click on my pink puzzle piece.
2136
01:34:21,658 --> 01:34:22,700
And I'm going to edit it.
2137
01:34:22,700 --> 01:34:25,210
So I kind of regret making Meow so simple.
2138
01:34:25,210 --> 01:34:28,660
Wouldn't it be nice if Meow took an input, a.k.a.
2139
01:34:28,660 --> 01:34:31,960
an argument, that tells Meow how many times to meow.
2140
01:34:31,960 --> 01:34:34,300
Then I can get rid of that loop and just tell
2141
01:34:34,300 --> 01:34:36,530
Meow how many meows I actually want.
2142
01:34:36,530 --> 01:34:40,390
So I'm going to click on another button here called, literally, Add an Input.
2143
01:34:40,390 --> 01:34:42,085
And it's going to have placeholder here.
2144
01:34:42,085 --> 01:34:43,960
So I'm just going to put a placeholder there.
2145
01:34:43,960 --> 01:34:47,880
I keep using "n" for number, which is a go to in computer scientist terms.
2146
01:34:47,880 --> 01:34:50,380
And I'm going to add some descriptive text just so that it's
2147
01:34:50,380 --> 01:34:52,030
a little more self-explanatory.
2148
01:34:52,030 --> 01:34:54,070
I'm just going to say Meow n Times.
2149
01:34:54,070 --> 01:34:55,510
But there's only one oval.
2150
01:34:55,510 --> 01:34:57,700
Times is just going to be explanatory text.
2151
01:34:57,700 --> 01:34:59,290
And now notice what has happened.
2152
01:34:59,290 --> 01:35:02,800
Now my puzzle piece takes an input, a.k.a.
2153
01:35:02,800 --> 01:35:06,580
an argument, that will tell that function to meow some number of times.
2154
01:35:06,580 --> 01:35:08,440
But it's not just going to work magically.
2155
01:35:08,440 --> 01:35:11,110
I need to implement that lower level detail.
2156
01:35:11,110 --> 01:35:12,700
So let me zoom out.
2157
01:35:12,700 --> 01:35:14,918
I have to remind myself what this function was.
2158
01:35:14,918 --> 01:35:16,960
So I'm going to drag it higher up just so they're
2159
01:35:16,960 --> 01:35:18,550
on the screen at the same time.
2160
01:35:18,550 --> 01:35:22,090
I'm going to go ahead now and temporarily move this over here.
2161
01:35:22,090 --> 01:35:24,190
I'm going to temporarily detach this over here.
2162
01:35:24,190 --> 01:35:24,700
Why?
2163
01:35:24,700 --> 01:35:30,130
Because what I thing I want to do is move my loop into the function itself,
2164
01:35:30,130 --> 01:35:33,760
move the Play and the Wait into the loop.
2165
01:35:33,760 --> 01:35:35,470
But I don't want a hardcode 3.
2166
01:35:35,470 --> 01:35:39,760
Notice that n here is its own oval I can drag a copy of n
2167
01:35:39,760 --> 01:35:41,390
and just let it go there.
2168
01:35:41,390 --> 01:35:45,700
So now I have a new version of Meow that takes an argument, n,
2169
01:35:45,700 --> 01:35:48,170
that tells Meow how many times to meow.
2170
01:35:48,170 --> 01:35:51,125
And now let me, again, drag this out of sight, out of mind,
2171
01:35:51,125 --> 01:35:52,750
because who cares how I implemented it?
2172
01:35:52,750 --> 01:35:54,880
Once it's implemented, it's sort of done.
2173
01:35:54,880 --> 01:35:58,070
Now my program is even better designed, in some sense.
2174
01:35:58,070 --> 01:35:58,570
Why?
2175
01:35:58,570 --> 01:36:01,120
Because now it really just says what it means.
2176
01:36:01,120 --> 01:36:01,840
There's no loop.
2177
01:36:01,840 --> 01:36:04,360
There's no repeat, no implementation details.
2178
01:36:04,360 --> 01:36:07,060
When Green Flag Clicked, Meow 3 Times.
2179
01:36:07,060 --> 01:36:10,840
And so functions indeed let you implement algorithms,
2180
01:36:10,840 --> 01:36:13,070
like they're just code that do something for you.
2181
01:36:13,070 --> 01:36:14,930
But they're also themselves abstractions.
2182
01:36:14,930 --> 01:36:15,430
Why?
2183
01:36:15,430 --> 01:36:17,660
Because once a function exists, it has a name.
2184
01:36:17,660 --> 01:36:19,580
And you can think about it in that term.
2185
01:36:19,580 --> 01:36:21,610
And you can use it by its name.
2186
01:36:21,610 --> 01:36:25,940
You don't have to care or remember how the function itself was built,
2187
01:36:25,940 --> 01:36:27,730
whether it's by you or even MIT.
2188
01:36:27,730 --> 01:36:29,480
So again, here I'll click the Green Flag.
2189
01:36:29,480 --> 01:36:30,313
It's the same thing.
2190
01:36:30,313 --> 01:36:32,292
[MEOWING]
2191
01:36:32,292 --> 01:36:35,130
2192
01:36:35,130 --> 01:36:38,250
So still correct, but better and better designed.
2193
01:36:38,250 --> 01:36:41,090
And so any time, here and out, with Scratch, or soon C,
2194
01:36:41,090 --> 01:36:43,790
and eventually Python, when you find yourself doing anything
2195
01:36:43,790 --> 01:36:47,828
resembling Copy/Paste or again and again grabbing the same code, probably
2196
01:36:47,828 --> 01:36:49,370
an opportunity to say, wait a minute.
2197
01:36:49,370 --> 01:36:52,490
Let me refactor this, so to speak, that is, rip out
2198
01:36:52,490 --> 01:36:54,697
the code that seems to be repeated again and again
2199
01:36:54,697 --> 01:36:57,530
and put it in its own function so you can give it a descriptive name
2200
01:36:57,530 --> 01:36:59,580
and use and reuse it.
2201
01:36:59,580 --> 01:37:04,880
Any questions just yet on now saying or these loops or these functions
2202
01:37:04,880 --> 01:37:06,590
that we're using?
2203
01:37:06,590 --> 01:37:07,130
Yeah--
2204
01:37:07,130 --> 01:37:09,883
AUDIENCE: [INAUDIBLE]
2205
01:37:09,883 --> 01:37:12,300
DAVID J. MALAN: How did I make it so it meows three times?
2206
01:37:12,300 --> 01:37:15,060
So I originally only had a puzzle piece called Meow.
2207
01:37:15,060 --> 01:37:16,770
And I decided to improve it.
2208
01:37:16,770 --> 01:37:20,000
So I held down Control and I Right-clicked or Control-clicked
2209
01:37:20,000 --> 01:37:22,070
on the pink puzzle piece at top left.
2210
01:37:22,070 --> 01:37:23,120
And I clicked Edit.
2211
01:37:23,120 --> 01:37:25,070
And that brought back the original interface
2212
01:37:25,070 --> 01:37:28,790
that lets me add some arguments to the puzzle piece itself.
2213
01:37:28,790 --> 01:37:31,250
And I clicked Add an Input on the left here.
2214
01:37:31,250 --> 01:37:34,260
And then I clicked on Add a Label over here.
2215
01:37:34,260 --> 01:37:36,960
So that just lets you customize it even further.
2216
01:37:36,960 --> 01:37:38,490
All right, so we've done this.
2217
01:37:38,490 --> 01:37:41,900
Let's add one of those other primitives too to do something optionally.
2218
01:37:41,900 --> 01:37:46,400
So how about we make the cat meow only if it's being petted by a human,
2219
01:37:46,400 --> 01:37:50,540
as by moving the mouse to hover over the cat, like a human would pet a cat?
2220
01:37:50,540 --> 01:37:55,110
Well, let me go ahead and throw away the meowing for now.
2221
01:37:55,110 --> 01:37:57,083
And let me simplify it by just using a sound.
2222
01:37:57,083 --> 01:37:58,500
I'm going to go ahead and do this.
2223
01:37:58,500 --> 01:38:02,030
I'm going to go ahead and have a Control block that
2224
01:38:02,030 --> 01:38:06,560
says If, because I want to implement the idea of if the cursor is touching
2225
01:38:06,560 --> 01:38:08,930
the cat, then play sound meow.
2226
01:38:08,930 --> 01:38:10,710
Or I could use my same pink puzzle piece.
2227
01:38:10,710 --> 01:38:14,150
But I'm going to throw that away and focus only now on the sounds.
2228
01:38:14,150 --> 01:38:15,840
And I'm going to do this.
2229
01:38:15,840 --> 01:38:19,598
If touching mouse pointer-- so I need to sense something about the world.
2230
01:38:19,598 --> 01:38:20,640
And we saw this earlier--
2231
01:38:20,640 --> 01:38:22,500
so If Touching Mouse Pointer.
2232
01:38:22,500 --> 01:38:24,810
So notice this shape here, way too big.
2233
01:38:24,810 --> 01:38:25,920
But it is the right shape.
2234
01:38:25,920 --> 01:38:29,210
So if I hover just right, it'll snap into place.
2235
01:38:29,210 --> 01:38:33,410
And this now, in blue, is my Boolean expression, a yes/no question,
2236
01:38:33,410 --> 01:38:34,850
true false.
2237
01:38:34,850 --> 01:38:36,860
"If" is a conditional.
2238
01:38:36,860 --> 01:38:38,100
And what do I want to do?
2239
01:38:38,100 --> 01:38:42,290
Well, if the cat is touching the mouse pointer,
2240
01:38:42,290 --> 01:38:44,970
I want to go ahead and play sound meow until done.
2241
01:38:44,970 --> 01:38:46,080
So let's do this.
2242
01:38:46,080 --> 01:38:48,808
I'm going to hit Green Flag, click.
2243
01:38:48,808 --> 01:38:51,350
Now nothing's happened yet because it's a conditional, right?
2244
01:38:51,350 --> 01:38:53,808
It's only supposed to do something if I'm touching the cat.
2245
01:38:53,808 --> 01:38:55,550
Let me move the cursor over to the cat.
2246
01:38:55,550 --> 01:38:59,365
And-- wait for it.
2247
01:38:59,365 --> 01:39:02,330
Hmm-- another bug.
2248
01:39:02,330 --> 01:39:06,200
Why is the cat not meowing even though I very explicitly
2249
01:39:06,200 --> 01:39:09,020
said, If Touching Mouse Pointer, Meow?
2250
01:39:09,020 --> 01:39:10,295
Yeah, in the middle--
2251
01:39:10,295 --> 01:39:11,893
AUDIENCE: [INAUDIBLE]
2252
01:39:11,893 --> 01:39:14,060
DAVID J. MALAN: Yeah, this is-- again, my computer's
2253
01:39:14,060 --> 01:39:15,440
just so darn fast, like yours.
2254
01:39:15,440 --> 01:39:17,317
I click the Green Flag, it asks the question,
2255
01:39:17,317 --> 01:39:18,650
am I touching the mouse pointer?
2256
01:39:18,650 --> 01:39:21,350
Well, no, because my cursor was up there, not touching the cat.
2257
01:39:21,350 --> 01:39:22,370
It's too late.
2258
01:39:22,370 --> 01:39:23,570
The cat's out of the bag.
2259
01:39:23,570 --> 01:39:28,490
And so we have to instead solve this by some other means.
2260
01:39:28,490 --> 01:39:29,940
How can we fix this?
2261
01:39:29,940 --> 01:39:31,470
How do we fix that sort of race?
2262
01:39:31,470 --> 01:39:31,970
Yeah--
2263
01:39:31,970 --> 01:39:32,690
AUDIENCE: [INAUDIBLE]
2264
01:39:32,690 --> 01:39:34,648
DAVID J. MALAN: Yeah, so why don't we just keep
2265
01:39:34,648 --> 01:39:38,570
asking the question until I eventually am or am not actually petting the cat?
2266
01:39:38,570 --> 01:39:40,610
So let me detach this temporarily.
2267
01:39:40,610 --> 01:39:42,050
Let me go under Control.
2268
01:39:42,050 --> 01:39:45,098
Let me go under-- instead of repeat some finite number of times,
2269
01:39:45,098 --> 01:39:46,140
let's just do it forever.
2270
01:39:46,140 --> 01:39:49,070
So sometimes loops that do work forever are a good thing.
2271
01:39:49,070 --> 01:39:51,140
The clock on your phone, that's in a loop
2272
01:39:51,140 --> 01:39:53,510
forever because you want it to always tell time and not
2273
01:39:53,510 --> 01:39:54,720
stop at the end of the day.
2274
01:39:54,720 --> 01:39:58,913
So sometimes you do want code to loop forever, as in this case.
2275
01:39:58,913 --> 01:40:00,830
So let me go ahead and drag and drop it there.
2276
01:40:00,830 --> 01:40:02,750
Let me, again, click the Green Flag.
2277
01:40:02,750 --> 01:40:03,900
Nothing's happening yet.
2278
01:40:03,900 --> 01:40:06,300
But notice, the program is still running.
2279
01:40:06,300 --> 01:40:10,700
And so if I move my cursor, move my cursor, move my cursor, and--
2280
01:40:10,700 --> 01:40:12,880
[MEOWING]
2281
01:40:12,880 --> 01:40:14,550
OK, so maybe we could add some Waiting.
2282
01:40:14,550 --> 01:40:17,750
But the cat does not want to be pet, in this case.
2283
01:40:17,750 --> 01:40:20,220
But it's indeed conditional.
2284
01:40:20,220 --> 01:40:22,380
So there we have an incarnation in Scratch
2285
01:40:22,380 --> 01:40:24,290
of doing something conditionally.
2286
01:40:24,290 --> 01:40:26,790
Now, we can make this really cool, really fast, if you will.
2287
01:40:26,790 --> 01:40:28,440
Let me stop this version.
2288
01:40:28,440 --> 01:40:31,270
Let me go ahead and do this.
2289
01:40:31,270 --> 01:40:33,270
Let me go ahead and throw all of this away.
2290
01:40:33,270 --> 01:40:36,210
Let me go into my little Extensions bucket over here.
2291
01:40:36,210 --> 01:40:40,020
And let me do Video Sensing, since most laptops or phones these days have
2292
01:40:40,020 --> 01:40:40,620
cameras.
2293
01:40:40,620 --> 01:40:44,190
And there, indeed, I am, with Sanders behind me.
2294
01:40:44,190 --> 01:40:45,780
And let me do this.
2295
01:40:45,780 --> 01:40:48,900
When Video Motion-- and let me get out of the way.
2296
01:40:48,900 --> 01:40:51,820
When Video Motion is Greater Than some value.
2297
01:40:51,820 --> 01:40:55,920
So 10 is the default. This is just a number that measures how much motion
2298
01:40:55,920 --> 01:40:56,670
there is or isn't.
2299
01:40:56,670 --> 01:40:58,290
So small number is like no motion.
2300
01:40:58,290 --> 01:40:59,920
Big number is lots of motion.
2301
01:40:59,920 --> 01:41:03,120
So I'm going to choose 50, somewhat arbitrarily here-- so 50.
2302
01:41:03,120 --> 01:41:05,370
This is not normal to program off to the side.
2303
01:41:05,370 --> 01:41:08,130
But I'm now going to say this.
2304
01:41:08,130 --> 01:41:14,460
When Video Motion is 50, go ahead and Play Sound Meow like this.
2305
01:41:14,460 --> 01:41:16,170
So the cat is still in that world.
2306
01:41:16,170 --> 01:41:18,970
I'm going to stop the program and rerun it.
2307
01:41:18,970 --> 01:41:21,090
So here we go, Green Flag.
2308
01:41:21,090 --> 01:41:24,780
And now here comes-- all right, this is a little creepy,
2309
01:41:24,780 --> 01:41:27,120
the way I'm petting the cat, but--
2310
01:41:27,120 --> 01:41:30,640
and-- [SIGH]
2311
01:41:30,640 --> 01:41:31,600
2312
01:41:31,600 --> 01:41:32,940
[MEOWING]
2313
01:41:32,940 --> 01:41:33,440
OK.
2314
01:41:33,440 --> 01:41:35,750
[LAUGHTER]
2315
01:41:35,750 --> 01:41:36,290
There we go.
2316
01:41:36,290 --> 01:41:37,910
OK, so 50 was too big of a number.
2317
01:41:37,910 --> 01:41:39,500
I have to pet the cat faster.
2318
01:41:39,500 --> 01:41:41,925
[MEOWING]
2319
01:41:41,925 --> 01:41:42,900
2320
01:41:42,900 --> 01:41:44,940
Whereas this, if--
2321
01:41:44,940 --> 01:41:46,410
I don't know--
2322
01:41:46,410 --> 01:41:48,330
[LAUGHTER]
2323
01:41:48,330 --> 01:41:49,924
Yeah, so--
2324
01:41:49,924 --> 01:41:50,710
[MEOW]
2325
01:41:50,710 --> 01:41:53,618
OK, so you can make things even more interactive in this way
2326
01:41:53,618 --> 01:41:55,410
by just assembling different puzzle pieces.
2327
01:41:55,410 --> 01:41:58,050
And honestly, there are so many different puzzle pieces in here.
2328
01:41:58,050 --> 01:42:00,610
We're not going to even scratch the surface of a lot of them.
2329
01:42:00,610 --> 01:42:02,940
But they generally just do what they say.
2330
01:42:02,940 --> 01:42:06,480
And indeed, when you see on the screen here this pallet
2331
01:42:06,480 --> 01:42:09,330
of puzzle pieces, really a lot of programming, especially early on,
2332
01:42:09,330 --> 01:42:12,970
when learning a language, is just trying different things and try and fail.
2333
01:42:12,970 --> 01:42:17,070
And if it doesn't work quite right, look for an alternative solution
2334
01:42:17,070 --> 01:42:20,000
there too, as even I just had to do a moment ago.
2335
01:42:20,000 --> 01:42:23,250
Well, let's go ahead and use, actually, how about another example of something
2336
01:42:23,250 --> 01:42:24,720
a predecessor of yours made?
2337
01:42:24,720 --> 01:42:28,830
Let me go ahead and grab a program I opened in advance here called
2338
01:42:28,830 --> 01:42:32,520
Whack-A-Mole Might we get a brave volunteer to come up,
2339
01:42:32,520 --> 01:42:38,895
who is willing to whack a mole with their head, virtually?
2340
01:42:38,895 --> 01:42:40,770
Maybe-- OK, let's see, how about in way back?
2341
01:42:40,770 --> 01:42:42,300
You want to come on down?
2342
01:42:42,300 --> 01:42:45,327
All right, come on down.
2343
01:42:45,327 --> 01:42:47,160
Sure, a round of applause for our volunteer.
2344
01:42:47,160 --> 01:42:49,382
[APPLAUSE]
2345
01:42:49,382 --> 01:42:52,130
2346
01:42:52,130 --> 01:42:56,835
All right, so here we have--
2347
01:42:56,835 --> 01:42:59,930
2348
01:42:59,930 --> 01:43:01,178
come on down.
2349
01:43:01,178 --> 01:43:01,970
AUDIENCE: Hi there.
2350
01:43:01,970 --> 01:43:02,720
DAVID J. MALAN: What's your name?
2351
01:43:02,720 --> 01:43:03,320
AUDIENCE: I'm Josh.
2352
01:43:03,320 --> 01:43:05,695
DAVID J. MALAN: Oh, actually, say it into the microphone.
2353
01:43:05,695 --> 01:43:06,950
AUDIENCE: Hi, I'm Josh.
2354
01:43:06,950 --> 01:43:07,670
DAVID J. MALAN: OK, nice.
2355
01:43:07,670 --> 01:43:08,240
Welcome, Josh.
2356
01:43:08,240 --> 01:43:08,782
Come on over.
2357
01:43:08,782 --> 01:43:10,850
[APPLAUSE]
2358
01:43:10,850 --> 01:43:12,892
All right, so same idea here--
2359
01:43:12,892 --> 01:43:13,850
I'll take the mic back.
2360
01:43:13,850 --> 01:43:15,230
You'll have to stand in front of the camera.
2361
01:43:15,230 --> 01:43:18,105
In just a moment, you're going to have to position your head in a box
2362
01:43:18,105 --> 01:43:20,150
that your classmate from yesteryear created.
2363
01:43:20,150 --> 01:43:22,340
[MUSIC PLAYING]
2364
01:43:22,340 --> 01:43:22,840
2365
01:43:22,840 --> 01:43:24,490
And we'll start with Beginner.
2366
01:43:24,490 --> 01:43:25,180
AUDIENCE: OK.
2367
01:43:25,180 --> 01:43:28,166
DAVID J. MALAN: So line your head up in the box in a moment.
2368
01:43:28,166 --> 01:43:29,142
AUDIENCE: All right.
2369
01:43:29,142 --> 01:43:37,438
2370
01:43:37,438 --> 01:43:39,878
[LAUGHTER]
2371
01:43:39,878 --> 01:43:43,800
2372
01:43:43,800 --> 01:43:45,390
DAVID J. MALAN: Nice.
2373
01:43:45,390 --> 01:43:46,155
12 seconds.
2374
01:43:46,155 --> 01:43:52,500
2375
01:43:52,500 --> 01:43:53,790
5 seconds.
2376
01:43:53,790 --> 01:43:55,710
Notice the score's up to 18 already.
2377
01:43:55,710 --> 01:43:58,410
Pretty good.
2378
01:43:58,410 --> 01:44:00,990
All right, a round of applause for Josh, if we could.
2379
01:44:00,990 --> 01:44:03,440
[APPLAUSE]
2380
01:44:03,440 --> 01:44:06,380
2381
01:44:06,380 --> 01:44:09,020
So notice how using some fairly simple primitives,
2382
01:44:09,020 --> 01:44:11,330
things do get interesting pretty fast.
2383
01:44:11,330 --> 01:44:12,560
And how was that implemented?
2384
01:44:12,560 --> 01:44:14,560
Well, there were probably at least four sprites.
2385
01:44:14,560 --> 01:44:16,222
So you're not confined to just one cat.
2386
01:44:16,222 --> 01:44:18,930
You can create more and more sprites, change what they look like.
2387
01:44:18,930 --> 01:44:20,930
So they actually look like a mole, in this case.
2388
01:44:20,930 --> 01:44:23,990
There's probably some conditionals in there, some loops for 30 seconds.
2389
01:44:23,990 --> 01:44:27,200
That's checking if Josh's head's movement is exceeding
2390
01:44:27,200 --> 01:44:29,210
some value over this way or over this way,
2391
01:44:29,210 --> 01:44:31,550
then increment something called a variable.
2392
01:44:31,550 --> 01:44:32,450
We'll see those too.
2393
01:44:32,450 --> 01:44:34,490
Just like in algebra you might have x and y
2394
01:44:34,490 --> 01:44:37,700
and z, storing values like numbers, so can computer programs,
2395
01:44:37,700 --> 01:44:42,350
have variables called x or y or z, or more descriptively called Score,
2396
01:44:42,350 --> 01:44:44,810
as in this case at top right, or another variable
2397
01:44:44,810 --> 01:44:47,780
called Countdown, typically one word in code, but in this case
2398
01:44:47,780 --> 01:44:49,632
two words, that just store some value.
2399
01:44:49,632 --> 01:44:51,590
So there's probably some math going on in there
2400
01:44:51,590 --> 01:44:55,040
whereby the author of this program just is incrementing, that is,
2401
01:44:55,040 --> 01:44:59,060
adding 1 and 1 every time it detected that a mole had been whacked,
2402
01:44:59,060 --> 01:45:00,570
in this case, with movement.
2403
01:45:00,570 --> 01:45:04,910
So back in the day, I, myself, actually implemented my very first program
2404
01:45:04,910 --> 01:45:08,180
in Scratch when I was a graduate student, actually, at MIT--
2405
01:45:08,180 --> 01:45:11,290
cross-registered at MIT, taking a class from MIT's Media Lab,
2406
01:45:11,290 --> 01:45:14,540
specifically, the lifelong kindergarten group, which is the group that created
2407
01:45:14,540 --> 01:45:15,500
Scratch, itself.
2408
01:45:15,500 --> 01:45:19,580
And the program I wrote all those years ago and still
2409
01:45:19,580 --> 01:45:22,040
rather cling to is a little something here
2410
01:45:22,040 --> 01:45:26,620
called Oscartime, that I thought I'd play just a quick excerpt of myself
2411
01:45:26,620 --> 01:45:27,120
here.
2412
01:45:27,120 --> 01:45:30,530
So in this case, consider, as the music starts playing,
2413
01:45:30,530 --> 01:45:33,800
how this program, which is much more sophisticated, certainly,
2414
01:45:33,800 --> 01:45:38,060
than the earliest "Say hello" examples we just did might also be implemented.
2415
01:45:38,060 --> 01:45:40,745
Let me go ahead now and click the Green Flag.
2416
01:45:40,745 --> 01:45:44,950
[MUSIC - OSCAR THE GROUCH, "I LOVE TRASH"]
2417
01:45:44,950 --> 01:45:46,973
OSCAR THE GROUCH: (SINGING) Oh, I love trash.
2418
01:45:46,973 --> 01:45:50,140
DAVID J. MALAN: OK, so some trash is moving, presumably in some kind of loop
2419
01:45:50,140 --> 01:45:51,970
from the top.
2420
01:45:51,970 --> 01:45:56,440
If I'm touching the mouse cursor, it follows me.
2421
01:45:56,440 --> 01:45:59,920
If I hover over the trash can, it responds.
2422
01:45:59,920 --> 01:46:04,600
If I let go, in some kind of loop, Oscar pops out,
2423
01:46:04,600 --> 01:46:06,910
creates a variable with the current score.
2424
01:46:06,910 --> 01:46:07,900
And it happens again.
2425
01:46:07,900 --> 01:46:09,983
OSCAR THE GROUCH: (SINGING) It's awful, the holes.
2426
01:46:09,983 --> 01:46:11,950
And the laces are torn.
2427
01:46:11,950 --> 01:46:16,030
A gift from my mother the day I was born.
2428
01:46:16,030 --> 01:46:20,050
I love it because it's trash.
2429
01:46:20,050 --> 01:46:21,070
Oh, I--
2430
01:46:21,070 --> 01:46:24,050
DAVID J. MALAN: It's pretty easy at first but--
2431
01:46:24,050 --> 01:46:26,410
OSCAR THE GROUCH: (SINGING) --anything dirty or dingy--
2432
01:46:26,410 --> 01:46:28,980
DAVID J. MALAN: So I don't need to keep playing this up
2433
01:46:28,980 --> 01:46:30,230
on stage in front of everyone.
2434
01:46:30,230 --> 01:46:32,950
So my score is already now up to some 6 or so.
2435
01:46:32,950 --> 01:46:35,900
But in a moment, too, you'll see that it's going to escalate.
2436
01:46:35,900 --> 01:46:38,080
So I'm taking into account some time apparently.
2437
01:46:38,080 --> 01:46:38,980
So now--
2438
01:46:38,980 --> 01:46:42,343
OSCAR THE GROUCH: (SINGING) I have here some newspaper, 13 months old.
2439
01:46:42,343 --> 01:46:45,010
DAVID J. MALAN: So more and more sprites are suddenly appearing.
2440
01:46:45,010 --> 01:46:46,540
And notice, that each time they're appearing
2441
01:46:46,540 --> 01:46:48,040
from a different part of the screen.
2442
01:46:48,040 --> 01:46:49,840
That's an illusion, perhaps, too, that--
2443
01:46:49,840 --> 01:46:52,270
pick a random number between x and y.
2444
01:46:52,270 --> 01:46:54,490
So you can actually pick some range of values
2445
01:46:54,490 --> 01:46:56,032
to have the game constantly changing.
2446
01:46:56,032 --> 01:46:58,073
And indeed, I'm going to go ahead and click Stop,
2447
01:46:58,073 --> 01:47:00,860
since I spent like eight hours plus, years ago, making this.
2448
01:47:00,860 --> 01:47:02,602
And I can never listen to the song again,
2449
01:47:02,602 --> 01:47:04,810
not that I should be anyway at this point in my life.
2450
01:47:04,810 --> 01:47:09,670
But this song is synchronized then with a lot of the actions that's happening.
2451
01:47:09,670 --> 01:47:12,130
And ultimately, there's just a lot of building blocks.
2452
01:47:12,130 --> 01:47:15,610
But I didn't sit down and implement Oscartime, as I called it, all at once.
2453
01:47:15,610 --> 01:47:17,500
I really did take baby steps, so to speak.
2454
01:47:17,500 --> 01:47:21,970
And I figured out, well, how could I decompose this vision I had at the time
2455
01:47:21,970 --> 01:47:24,220
to create this game ultimately?
2456
01:47:24,220 --> 01:47:26,470
And how do I bite off maybe the easiest parts first?
2457
01:47:26,470 --> 01:47:29,912
And honestly, the first thing I did was I found this image, and I just dragged
2458
01:47:29,912 --> 01:47:31,120
and dropped it into Scratch--
2459
01:47:31,120 --> 01:47:33,580
OK, done-- like, lamppost is installed.
2460
01:47:33,580 --> 01:47:34,575
It doesn't do anything.
2461
01:47:34,575 --> 01:47:35,450
It's not interactive.
2462
01:47:35,450 --> 01:47:37,995
But I at least set the stage, so to speak, for the program.
2463
01:47:37,995 --> 01:47:39,370
Then what else might I have done?
2464
01:47:39,370 --> 01:47:40,640
Well, let me do this.
2465
01:47:40,640 --> 01:47:44,290
Let me go ahead and open up in another editor here
2466
01:47:44,290 --> 01:47:48,980
an early incarnation of Oscartime by doing this.
2467
01:47:48,980 --> 01:47:51,220
Let me go into Oscartime here.
2468
01:47:51,220 --> 01:47:52,510
Let me full screen this.
2469
01:47:52,510 --> 01:47:54,190
And here you have--
2470
01:47:54,190 --> 01:47:56,080
let me hide the trash for just a moment--
2471
01:47:56,080 --> 01:48:00,010
is what I might call the second version of my program, wherein,
2472
01:48:00,010 --> 01:48:03,220
at the top right of the stage here, I had the lamppost, which I just
2473
01:48:03,220 --> 01:48:06,267
dragged and dropped and got going, but then I added an actual sprite.
2474
01:48:06,267 --> 01:48:09,100
And it has to be a sprite if you want it to do things interactively.
2475
01:48:09,100 --> 01:48:10,420
The lamppost-- not a sprite.
2476
01:48:10,420 --> 01:48:13,240
It's just an image a costume, if you will, for the whole
2477
01:48:13,240 --> 01:48:14,680
stage itself, a backdrop.
2478
01:48:14,680 --> 01:48:16,780
But this thing is indeed a sprite because it
2479
01:48:16,780 --> 01:48:20,240
needs to respond to code and events, like dragging and dropping.
2480
01:48:20,240 --> 01:48:23,960
So what might I have done early on with that code?
2481
01:48:23,960 --> 01:48:26,350
Well, maybe the first version would have been
2482
01:48:26,350 --> 01:48:30,250
something like this, whereby my very first version of Oscartime
2483
01:48:30,250 --> 01:48:32,170
might have said something like, oh, this.
2484
01:48:32,170 --> 01:48:35,500
How about, let me control the program as before-- or, rather, events.
2485
01:48:35,500 --> 01:48:38,630
When the Green Flag is clicked, what do I want to do?
2486
01:48:38,630 --> 01:48:43,600
Well, I want to go ahead and forever do something like this.
2487
01:48:43,600 --> 01:48:46,687
Forever-- so I want the lid to open up if I touch it.
2488
01:48:46,687 --> 01:48:49,270
So if the cursor gets near the lid, I want the lid to open up.
2489
01:48:49,270 --> 01:48:51,340
And then if I move away, I want it to close.
2490
01:48:51,340 --> 01:48:52,640
So how can I do that?
2491
01:48:52,640 --> 01:48:56,080
I want an If, but I just don't want one question, I really want two,
2492
01:48:56,080 --> 01:48:59,200
a fork in the road that goes left or right, so to speak.
2493
01:48:59,200 --> 01:49:02,830
And let me grab this puzzle piece here, as I did long ago.
2494
01:49:02,830 --> 01:49:04,690
So notice, it grows to fill.
2495
01:49:04,690 --> 01:49:06,430
What's the question I want to ask?
2496
01:49:06,430 --> 01:49:08,680
Well, under Sensing, I'm going to go ahead here
2497
01:49:08,680 --> 01:49:12,670
and say If this trashcan is Touching the Mouse Pointer--
2498
01:49:12,670 --> 01:49:13,863
what do I want to do?
2499
01:49:13,863 --> 01:49:16,030
Well, I want to change what the trashcan looks like.
2500
01:49:16,030 --> 01:49:17,740
And this part, I did in advance of class.
2501
01:49:17,740 --> 01:49:21,040
If you go up here to Costumes, this is where all the graphical stuff happens.
2502
01:49:21,040 --> 01:49:25,720
And you'll see that I imported a whole bunch of different costumes
2503
01:49:25,720 --> 01:49:29,080
that effectively, much like a video, when you play them quickly,
2504
01:49:29,080 --> 01:49:31,423
creates the illusion of movement, some animation.
2505
01:49:31,423 --> 01:49:33,340
But it's really just dot, dot, dot, dot, dot--
2506
01:49:33,340 --> 01:49:35,320
different images showing on the screen.
2507
01:49:35,320 --> 01:49:39,250
Well, some of these costumes are called like Oscar1, Oscar2.
2508
01:49:39,250 --> 01:49:40,810
Oscar1 is closed.
2509
01:49:40,810 --> 01:49:42,070
Oscar2 is open.
2510
01:49:42,070 --> 01:49:43,730
So let's just deal with those first.
2511
01:49:43,730 --> 01:49:48,100
So if I'm touching the mouse pointer, let me go under-- how about Looks?
2512
01:49:48,100 --> 01:49:50,680
And we didn't use this before, but there's this block,
2513
01:49:50,680 --> 01:49:52,870
Switch Costume to Something Else.
2514
01:49:52,870 --> 01:49:55,150
I'm going to drag and drop this inside of the If.
2515
01:49:55,150 --> 01:49:57,070
And notice it's a little bit indented.
2516
01:49:57,070 --> 01:50:00,730
I'm going to change it not to Oscar8, but Oscar2.
2517
01:50:00,730 --> 01:50:03,340
Otherwise, If Not Touching the Mouse Pointer--
2518
01:50:03,340 --> 01:50:05,890
this is the other direction in the fork in the road--
2519
01:50:05,890 --> 01:50:10,840
let's go ahead and switch the costume back to what I described as Oscar1.
2520
01:50:10,840 --> 01:50:12,400
So let me run this program.
2521
01:50:12,400 --> 01:50:14,900
And not much of interest is happening yet.
2522
01:50:14,900 --> 01:50:18,550
But notice, if I move the cursor up, down--
2523
01:50:18,550 --> 01:50:20,030
but how is that working?
2524
01:50:20,030 --> 01:50:22,940
It's just changing the costume that's being overlaid on the sprite.
2525
01:50:22,940 --> 01:50:24,790
So it looks like interactivity, but you are really
2526
01:50:24,790 --> 01:50:25,998
just changing the aesthetics.
2527
01:50:25,998 --> 01:50:29,135
And we humans are just kind of assuming, oh, it's opening up.
2528
01:50:29,135 --> 01:50:30,760
Well, no, it's just changing a costume.
2529
01:50:30,760 --> 01:50:31,802
So here's the difference.
2530
01:50:31,802 --> 01:50:34,210
The high-level abstraction-- trashcan opening.
2531
01:50:34,210 --> 01:50:36,850
The lower-level implementation detail-- costume
2532
01:50:36,850 --> 01:50:38,500
changing, creating that illusion.
2533
01:50:38,500 --> 01:50:41,680
And if I want it to look prettier, I could just have many other costumes
2534
01:50:41,680 --> 01:50:45,220
and go boom, boom, boom, boom, boom to create more frames per second,
2535
01:50:45,220 --> 01:50:46,130
if you will.
2536
01:50:46,130 --> 01:50:48,820
So I need to do one other thing.
2537
01:50:48,820 --> 01:50:51,130
Maybe if I accidentally leave the trashcan open,
2538
01:50:51,130 --> 01:50:52,750
let me make one change here.
2539
01:50:52,750 --> 01:50:54,730
Let me make sure that the very first thing
2540
01:50:54,730 --> 01:50:58,360
I do when the Green Flag is clicked, is always start with the trashcan
2541
01:50:58,360 --> 01:51:01,810
closed because otherwise, you might accidentally leave it open.
2542
01:51:01,810 --> 01:51:03,590
So this gets me into some default state.
2543
01:51:03,590 --> 01:51:08,133
So now it's always closed until I manually hover over it instead.
2544
01:51:08,133 --> 01:51:09,550
Well, what might I have done next?
2545
01:51:09,550 --> 01:51:13,587
Well, if I wanted to introduce something like the trash, I need a second sprite.
2546
01:51:13,587 --> 01:51:15,670
And here, in advance, I grabbed the image already.
2547
01:51:15,670 --> 01:51:18,310
Let me pretend that this never happened.
2548
01:51:18,310 --> 01:51:20,260
Let me drag this away here.
2549
01:51:20,260 --> 01:51:24,033
And now I have nothing in my code area for this piece of trash.
2550
01:51:24,033 --> 01:51:25,200
But it is the second sprite.
2551
01:51:25,200 --> 01:51:27,900
And all I did was I clicked on the little cat plus icon here,
2552
01:51:27,900 --> 01:51:29,190
created a second sprite.
2553
01:51:29,190 --> 01:51:30,330
I named it trash.
2554
01:51:30,330 --> 01:51:32,250
I added a costume for it.
2555
01:51:32,250 --> 01:51:34,170
Sort of the aesthetic stuff, I did in advance.
2556
01:51:34,170 --> 01:51:36,360
But here I'll do now the code.
2557
01:51:36,360 --> 01:51:37,720
How do I want to do this?
2558
01:51:37,720 --> 01:51:42,160
Well, how about when the Green Flag is clicked, for the trash can,
2559
01:51:42,160 --> 01:51:45,090
I want the trash can in parallel to do--
2560
01:51:45,090 --> 01:51:48,190
I want the trash, the piece of trash, to do its own thing.
2561
01:51:48,190 --> 01:51:52,500
So what I want it to do is maybe let's do Motion, how about?
2562
01:51:52,500 --> 01:51:54,240
And let's go to a specific coordinate.
2563
01:51:54,240 --> 01:51:55,750
Now, there's a lot of options here.
2564
01:51:55,750 --> 01:52:00,870
There's Turning, Go to a Random Position, Go to x,y, Glide,
2565
01:52:00,870 --> 01:52:01,740
more elegantly.
2566
01:52:01,740 --> 01:52:03,990
There's a lot of different ways to implement movement.
2567
01:52:03,990 --> 01:52:06,310
I just want it to go to a very specific location first.
2568
01:52:06,310 --> 01:52:09,000
So I'm just going to go to x,y first.
2569
01:52:09,000 --> 01:52:13,350
And I'm going to say x, how about, will be--
2570
01:52:13,350 --> 01:52:14,470
let's not hardcode this.
2571
01:52:14,470 --> 01:52:18,120
Let's just have it be-- well, let's do it at 0, initially, and then 240.
2572
01:52:18,120 --> 01:52:21,360
So-- whoops-- let's do 0,240 so that this piece of trash
2573
01:52:21,360 --> 01:52:23,410
always starts at the top middle of the screen.
2574
01:52:23,410 --> 01:52:26,160
If you think back to that coordinate system, 0,0 is in the middle.
2575
01:52:26,160 --> 01:52:28,230
240 is straight above it.
2576
01:52:28,230 --> 01:52:31,150
All right, now, after I do that, what do I want to do?
2577
01:52:31,150 --> 01:52:34,980
Well, how about I control this thing by forever falling.
2578
01:52:34,980 --> 01:52:36,427
Now, how do I make the trash move?
2579
01:52:36,427 --> 01:52:38,010
We haven't seen this puzzle piece yet.
2580
01:52:38,010 --> 01:52:42,390
But under Motion, the very first thing is called Move Some Number of Steps.
2581
01:52:42,390 --> 01:52:43,380
By default, it's 10.
2582
01:52:43,380 --> 01:52:44,880
But we'll do it more simply.
2583
01:52:44,880 --> 01:52:47,790
Let me go ahead and move--
2584
01:52:47,790 --> 01:52:48,660
oh, sorry.
2585
01:52:48,660 --> 01:52:52,350
Move is going to move it in whatever direction it's facing.
2586
01:52:52,350 --> 01:52:53,753
I only want it to move down.
2587
01:52:53,753 --> 01:52:56,670
So here, even I'm getting confused as to how many different ways there
2588
01:52:56,670 --> 01:52:57,540
are to do things.
2589
01:52:57,540 --> 01:52:59,680
What I thing I want to do is this.
2590
01:52:59,680 --> 01:53:03,670
Let me only change my y-axis as follows.
2591
01:53:03,670 --> 01:53:06,060
So here's another puzzle piece called Change y.
2592
01:53:06,060 --> 01:53:07,680
So again, y is the vertical.
2593
01:53:07,680 --> 01:53:11,400
So let me just change y by one pixel downward
2594
01:53:11,400 --> 01:53:14,170
at a time, so -1 one pixel at a time.
2595
01:53:14,170 --> 01:53:15,540
So it's kind of slow.
2596
01:53:15,540 --> 01:53:17,010
And I think now--
2597
01:53:17,010 --> 01:53:17,890
I think that's it.
2598
01:53:17,890 --> 01:53:18,990
Let me hit Stop.
2599
01:53:18,990 --> 01:53:21,700
Notice that my trashcan is still going to be interactive.
2600
01:53:21,700 --> 01:53:23,370
I haven't changed or deleted that code.
2601
01:53:23,370 --> 01:53:26,850
I've just added now code for my piece of trash.
2602
01:53:26,850 --> 01:53:30,120
If I click the Green Flag, notice that--
2603
01:53:30,120 --> 01:53:32,920
after I enable it-- let me start that again.
2604
01:53:32,920 --> 01:53:34,320
I had it hidden for before class.
2605
01:53:34,320 --> 01:53:35,403
But let me enable it now--
2606
01:53:35,403 --> 01:53:40,080
Green Flag, notice it starts dead center, at x equals 0, y equals 240,
2607
01:53:40,080 --> 01:53:42,750
and it's dropping one pixel at a time.
2608
01:53:42,750 --> 01:53:46,470
If that seems a little boring, we can change it to -10 pixels at a time
2609
01:53:46,470 --> 01:53:47,530
and, boom, it's done.
2610
01:53:47,530 --> 01:53:49,780
So that's how you might change the speed of a program.
2611
01:53:49,780 --> 01:53:51,750
But I'm going to leave it more simply as -1.
2612
01:53:51,750 --> 01:53:54,708
And honestly, it would be nice if it doesn't always start from the top.
2613
01:53:54,708 --> 01:53:57,083
Otherwise, this game is not going to be very interactive.
2614
01:53:57,083 --> 01:54:00,580
I'm literally going to be grabbing the trash from the same place every time.
2615
01:54:00,580 --> 01:54:03,250
So why don't I, instead, Stop this.
2616
01:54:03,250 --> 01:54:06,550
Let me go under Operators, and let's pick a random number.
2617
01:54:06,550 --> 01:54:08,370
So let me change the hardcoded--
2618
01:54:08,370 --> 01:54:12,210
the manually inputted-- 0, and let's make x be somewhere between 0,
2619
01:54:12,210 --> 01:54:15,660
so in the middle and all the way over to--
2620
01:54:15,660 --> 01:54:18,000
what was it-- oh, I got my numbers wrong--
2621
01:54:18,000 --> 01:54:20,970
240 and my y will be 180.
2622
01:54:20,970 --> 01:54:23,560
Sorry, I got my x and my y confused.
2623
01:54:23,560 --> 01:54:25,420
So let me play this again.
2624
01:54:25,420 --> 01:54:27,645
And now we have a game that's more like games
2625
01:54:27,645 --> 01:54:29,520
you might have played growing up or even now,
2626
01:54:29,520 --> 01:54:30,978
like there's some randomness to it.
2627
01:54:30,978 --> 01:54:33,660
So the CPU, so to speak, is doing something more interesting.
2628
01:54:33,660 --> 01:54:34,570
Let me run it again.
2629
01:54:34,570 --> 01:54:35,820
Now it's a little to the left.
2630
01:54:35,820 --> 01:54:36,600
Let me run it again.
2631
01:54:36,600 --> 01:54:38,058
Now it's a little more to the left.
2632
01:54:38,058 --> 01:54:39,630
Again-- now it's back to the right.
2633
01:54:39,630 --> 01:54:41,640
So randomness just makes games more interesting.
2634
01:54:41,640 --> 01:54:44,130
And this is why when you play any video game, if different things are
2635
01:54:44,130 --> 01:54:46,172
happening, there's probably just some randomness.
2636
01:54:46,172 --> 01:54:48,360
And it's quantized as just a simple number.
2637
01:54:48,360 --> 01:54:52,050
Now, I think I just need one final flourish here, if I may.
2638
01:54:52,050 --> 01:54:54,060
Let me go ahead and add this.
2639
01:54:54,060 --> 01:54:57,570
How about Events-- or rather--
2640
01:54:57,570 --> 01:54:58,380
yes, Events.
2641
01:54:58,380 --> 01:55:02,700
When Green Flag is clicked, I can do multiple things within the same sprite.
2642
01:55:02,700 --> 01:55:05,190
They don't all have to be attached to the same one.
2643
01:55:05,190 --> 01:55:10,110
Let me go ahead and forever go ahead and do something else.
2644
01:55:10,110 --> 01:55:13,470
How about, Whenever the Trash is--
2645
01:55:13,470 --> 01:55:17,500
how about-- Touching the Trash Can--
2646
01:55:17,500 --> 01:55:20,850
so Forever If-- let's see, I need a Sensing block.
2647
01:55:20,850 --> 01:55:24,000
So how about, Is Touching--
2648
01:55:24,000 --> 01:55:28,530
not the Mouse Pointer, this time, but Touching Oscar himself there.
2649
01:55:28,530 --> 01:55:30,810
Now let's see what happens.
2650
01:55:30,810 --> 01:55:33,750
All right, so let's go ahead and click the Green Flag.
2651
01:55:33,750 --> 01:55:37,560
Now I go down over here and let go.
2652
01:55:37,560 --> 01:55:41,340
OK, I kind of want it to go into the trash can.
2653
01:55:41,340 --> 01:55:43,620
How do I make it go into the trash can?
2654
01:55:43,620 --> 01:55:47,610
How can we take this high-level idea, put trash into the trash can,
2655
01:55:47,610 --> 01:55:49,950
and make it seem to disappear?
2656
01:55:49,950 --> 01:55:51,520
Logically, what could we do?
2657
01:55:51,520 --> 01:55:52,370
Yeah--
2658
01:55:52,370 --> 01:55:53,245
AUDIENCE: [INAUDIBLE]
2659
01:55:53,245 --> 01:55:56,037
DAVID J. MALAN: OK, so when it touches it, let's have it disappear.
2660
01:55:56,037 --> 01:55:56,910
So I could hide it.
2661
01:55:56,910 --> 01:55:59,490
Or honestly, if the game is going to be ongoing, like it was,
2662
01:55:59,490 --> 01:56:01,980
letting me drop more and more trash, let me just
2663
01:56:01,980 --> 01:56:04,380
have it go ahead and pick a new random location.
2664
01:56:04,380 --> 01:56:05,560
So let me do this.
2665
01:56:05,560 --> 01:56:09,073
Let me go ahead and Copy this puzzle piece up here and Duplicate.
2666
01:56:09,073 --> 01:56:10,740
And I don't want the whole thing, sorry.
2667
01:56:10,740 --> 01:56:12,400
Let me get rid of this.
2668
01:56:12,400 --> 01:56:13,540
Let me just do this.
2669
01:56:13,540 --> 01:56:16,720
Let me go back to some random location at the top.
2670
01:56:16,720 --> 01:56:17,940
So now notice what happens.
2671
01:56:17,940 --> 01:56:19,950
If I click and drag on it-- here it goes--
2672
01:56:19,950 --> 01:56:23,730
and I let go, it looks like it's going into the trash can
2673
01:56:23,730 --> 01:56:26,318
because it snaps back up to some random location.
2674
01:56:26,318 --> 01:56:29,610
Now, the only thing I'm not doing really is keeping track of any kind of score.
2675
01:56:29,610 --> 01:56:33,150
And it turns out, if I full screen this, it's not going to be draggable,
2676
01:56:33,150 --> 01:56:33,940
by default.
2677
01:56:33,940 --> 01:56:35,940
So just as a corner case, so to speak, something
2678
01:56:35,940 --> 01:56:40,860
that you might trip over otherwise, let me go ahead and under, let's
2679
01:56:40,860 --> 01:56:45,960
see, Sensing, it turns out I also need this for the piece of trash.
2680
01:56:45,960 --> 01:56:49,500
There's this way of setting, in Scratch, a sprite
2681
01:56:49,500 --> 01:56:51,540
to be draggable or not draggable.
2682
01:56:51,540 --> 01:56:53,490
I need to explicitly make it draggable so
2683
01:56:53,490 --> 01:56:55,560
that when I do full screen this thing now,
2684
01:56:55,560 --> 01:56:58,320
it still remains draggable and someone like myself
2685
01:56:58,320 --> 01:57:00,240
can play it again and again.
2686
01:57:00,240 --> 01:57:03,120
Well, how about we supplement this with one final flourish?
2687
01:57:03,120 --> 01:57:06,010
Why don't we keep track now of the user score?
2688
01:57:06,010 --> 01:57:10,560
So how about, when the user actually drags the piece of trash
2689
01:57:10,560 --> 01:57:13,020
to the trash can, let me go under Variables here,
2690
01:57:13,020 --> 01:57:16,590
where, in advance, I've already made myself a variable called Score.
2691
01:57:16,590 --> 01:57:20,100
I could have called it x or y or z or ABC, but that's not very descriptive.
2692
01:57:20,100 --> 01:57:23,070
In programming, you typically give things a more descriptive English,
2693
01:57:23,070 --> 01:57:24,450
or some other language, name.
2694
01:57:24,450 --> 01:57:26,200
So I called this one Score.
2695
01:57:26,200 --> 01:57:28,710
So how do I want to do this in my Score?
2696
01:57:28,710 --> 01:57:31,890
Well, let me go ahead and initially set this game
2697
01:57:31,890 --> 01:57:35,880
score to 0 at the very top of one of these scripts--
2698
01:57:35,880 --> 01:57:37,470
one of these programs up here.
2699
01:57:37,470 --> 01:57:42,570
And then any time my piece of trash is touching
2700
01:57:42,570 --> 01:57:49,630
Oscar, let's not just jump to the top, let's change the score by 1 up here.
2701
01:57:49,630 --> 01:57:52,680
So now notice, If Touching Oscar, Change the Score-- that is,
2702
01:57:52,680 --> 01:57:54,000
Add 1 to the Score--
2703
01:57:54,000 --> 01:57:55,920
and then Pick a new Random location.
2704
01:57:55,920 --> 01:57:58,290
And now Green Flag--
2705
01:57:58,290 --> 01:57:59,520
let's do this slowly.
2706
01:57:59,520 --> 01:58:00,540
Here it goes.
2707
01:58:00,540 --> 01:58:01,590
The trashcan opens.
2708
01:58:01,590 --> 01:58:02,520
I let go.
2709
01:58:02,520 --> 01:58:06,660
And now notice, at the top left of my program, notice the score is now 2.
2710
01:58:06,660 --> 01:58:10,140
Notice the score, if I do this again, is about to become 3.
2711
01:58:10,140 --> 01:58:12,870
And so here we have building blocks, literally,
2712
01:58:12,870 --> 01:58:15,910
of making this program better and better and better.
2713
01:58:15,910 --> 01:58:20,610
And so, indeed, that's how you generally approach solving any problem with code,
2714
01:58:20,610 --> 01:58:22,530
be it in Scratch or C or Python or some other.
2715
01:58:22,530 --> 01:58:24,975
You take this vision you might have or some vision
2716
01:58:24,975 --> 01:58:26,850
you've been assigned in a homework assignment
2717
01:58:26,850 --> 01:58:29,490
and try to break it down into these constituent parts
2718
01:58:29,490 --> 01:58:31,530
and just pluck off the easy ones first.
2719
01:58:31,530 --> 01:58:33,390
Put the lamp post there first, and at least
2720
01:58:33,390 --> 01:58:34,980
feel like you're making some progress.
2721
01:58:34,980 --> 01:58:37,188
Then pluck off something like the trash can, and just
2722
01:58:37,188 --> 01:58:38,457
make it do a little thing.
2723
01:58:38,457 --> 01:58:40,540
And it doesn't have to be in some same order here.
2724
01:58:40,540 --> 01:58:42,750
I could have done this in a million different ways.
2725
01:58:42,750 --> 01:58:46,410
But figure out what the small pieces are that, ultimately,
2726
01:58:46,410 --> 01:58:50,760
like a few of the problems we've solved today, assemble into a greater solution
2727
01:58:50,760 --> 01:58:52,380
there too.
2728
01:58:52,380 --> 01:58:56,410
So that you have now a mental model for these types of blocks and others,
2729
01:58:56,410 --> 01:58:58,470
let's return for a moment to this.
2730
01:58:58,470 --> 01:59:02,100
We saw a moment ago that when I started saying, "Hello, David,"
2731
01:59:02,100 --> 01:59:06,670
and nesting those puzzle pieces, we had a whole different paradigm altogether.
2732
01:59:06,670 --> 01:59:10,630
My input for that second version of, "Hello, world," was to now pass in,
2733
01:59:10,630 --> 01:59:12,300
for instance, "What's Your Name?"
2734
01:59:12,300 --> 01:59:15,000
into my function, called Ask.
2735
01:59:15,000 --> 01:59:17,610
That gave me not a side effect, but what I called,
2736
01:59:17,610 --> 01:59:21,690
again, a return value, called Answer, by default, in Scratch.
2737
01:59:21,690 --> 01:59:25,710
And now notice and recall, when I had that same output
2738
01:59:25,710 --> 01:59:31,260
become the input to my next block, it looked a little something like this--
2739
01:59:31,260 --> 01:59:32,100
Say.
2740
01:59:32,100 --> 01:59:36,240
So how does this type of block and this nesting, this stacking of blocks,
2741
01:59:36,240 --> 01:59:37,710
fit into the same mental model?
2742
01:59:37,710 --> 01:59:42,660
Well, same idea-- my input for that part of the story is now taking in not one
2743
01:59:42,660 --> 01:59:43,790
input but two--
2744
01:59:43,790 --> 01:59:47,190
two arguments-- "hello" and the answer from before.
2745
01:59:47,190 --> 01:59:50,010
The function, in this case, is that new block called Join.
2746
01:59:50,010 --> 01:59:53,640
The output thereof is, "Hello, David," which itself became--
2747
01:59:53,640 --> 01:59:55,170
if we sort of animate this--
2748
01:59:55,170 --> 02:00:00,840
the input to my final function, which indeed was still Say.
2749
02:00:00,840 --> 02:00:03,150
And this is only to say-- no pun intended--
2750
02:00:03,150 --> 02:00:06,570
that almost everything that you do with these puzzle pieces,
2751
02:00:06,570 --> 02:00:09,605
be it in the context of Oscartime or the mole whacking
2752
02:00:09,605 --> 02:00:11,730
or even just something simple like, "Hello, world,"
2753
02:00:11,730 --> 02:00:16,320
will ultimately fit into that relatively simple mental model there.
2754
02:00:16,320 --> 02:00:20,220
Now, I thought we'd end by taking a look at just a couple of final examples.
2755
02:00:20,220 --> 02:00:22,950
These ones, too, made by some of your predecessors.
2756
02:00:22,950 --> 02:00:26,010
And for this, I thought we would not write code together,
2757
02:00:26,010 --> 02:00:27,760
but read it instead.
2758
02:00:27,760 --> 02:00:30,960
And so allow me to open up one other example here
2759
02:00:30,960 --> 02:00:34,770
that will show us a few different versions of a program
2760
02:00:34,770 --> 02:00:36,720
that a predecessor made.
2761
02:00:36,720 --> 02:00:38,340
Give me just a moment here.
2762
02:00:38,340 --> 02:00:42,960
And we'll see how we might build up to something even more interactive.
2763
02:00:42,960 --> 02:00:44,940
And in just a moment, we'll see something
2764
02:00:44,940 --> 02:00:51,090
they called Ivy's Hardest Game, focused here on these particular mechanics.
2765
02:00:51,090 --> 02:00:55,110
So here is version 0, so to speak, of this program, wherein
2766
02:00:55,110 --> 02:00:58,560
the goal was to create a game where you have to get out of some kind of maze.
2767
02:00:58,560 --> 02:01:01,800
And you have to get out, in this case, the Harvard crest from this maze.
2768
02:01:01,800 --> 02:01:04,170
Let me go ahead and just hit Play on this Green Flag
2769
02:01:04,170 --> 02:01:08,040
so you can see what the first building block for this program might have been.
2770
02:01:08,040 --> 02:01:12,990
Notice that my hand here is actually on the Arrow keys on my keyboard.
2771
02:01:12,990 --> 02:01:16,800
And it seems that by moving up, down, left, or right,
2772
02:01:16,800 --> 02:01:20,432
this little crest on the screen responds in exactly that way.
2773
02:01:20,432 --> 02:01:22,140
Now, let's hypothesize for just a moment.
2774
02:01:22,140 --> 02:01:24,900
Even though we've not done anything quite like this before,
2775
02:01:24,900 --> 02:01:27,260
how might this code be implemented?
2776
02:01:27,260 --> 02:01:30,740
How do you get a sprite, be it a cat or a crest,
2777
02:01:30,740 --> 02:01:34,220
to respond to keys on a keyboard-- might you think intuitively?
2778
02:01:34,220 --> 02:01:34,730
Yeah--
2779
02:01:34,730 --> 02:01:36,983
AUDIENCE: [INAUDIBLE]
2780
02:01:36,983 --> 02:01:38,900
DAVID J. MALAN: Yeah, there could be something
2781
02:01:38,900 --> 02:01:40,410
sensing what key you're pressing on.
2782
02:01:40,410 --> 02:01:42,230
And if you do it again in forever a loop,
2783
02:01:42,230 --> 02:01:44,600
you'll just constantly be listening for keystrokes.
2784
02:01:44,600 --> 02:01:47,240
And this is how, like, every piece of software nowadays works.
2785
02:01:47,240 --> 02:01:49,910
It's constantly waiting for your phone to be tapped
2786
02:01:49,910 --> 02:01:51,690
or something to be typed on the screen.
2787
02:01:51,690 --> 02:01:55,400
So let me go ahead and look inside of this existing program here.
2788
02:01:55,400 --> 02:01:57,890
And there's more going on, but we'll take a quick glance
2789
02:01:57,890 --> 02:01:59,730
what's actually going on.
2790
02:01:59,730 --> 02:02:02,000
Well, up here at top left, notice, we just
2791
02:02:02,000 --> 02:02:04,850
have Go To x Equals 0 and y Equals 0.
2792
02:02:04,850 --> 02:02:07,850
That means put the Harvard crest dead center in the middle of the stage.
2793
02:02:07,850 --> 02:02:10,460
Then we have Forever two functions that we
2794
02:02:10,460 --> 02:02:13,130
made in advance as custom functions--
2795
02:02:13,130 --> 02:02:15,500
Listen for Keyboard, Feel for Walls.
2796
02:02:15,500 --> 02:02:17,190
So it's doing two things at once.
2797
02:02:17,190 --> 02:02:19,403
It's forever listening for the keyboard-- up,
2798
02:02:19,403 --> 02:02:21,320
down, left, right-- and feeling for the walls,
2799
02:02:21,320 --> 02:02:23,278
in the sense that if I get too far to the left,
2800
02:02:23,278 --> 02:02:26,010
I don't want it to keep moving past that black wall.
2801
02:02:26,010 --> 02:02:28,520
And if it moves too far to the right, I don't want it to blow through that wall
2802
02:02:28,520 --> 02:02:29,060
either.
2803
02:02:29,060 --> 02:02:32,300
So it's going to do two things constantly, listening for keyboard
2804
02:02:32,300 --> 02:02:34,250
and feeling for walls, so to speak.
2805
02:02:34,250 --> 02:02:35,870
And how are those implemented?
2806
02:02:35,870 --> 02:02:37,190
Well, this one's a bit long.
2807
02:02:37,190 --> 02:02:40,080
But on the left here is Listen for Keyboard.
2808
02:02:40,080 --> 02:02:43,760
So this pink puzzle piece, Listen for Keyboard,
2809
02:02:43,760 --> 02:02:47,420
first checks If the Key Up Arrow is Pressed, question mark,
2810
02:02:47,420 --> 02:02:50,570
Boolean expression in a conditional, Change y By 1.
2811
02:02:50,570 --> 02:02:52,040
That means, move it up 1.
2812
02:02:52,040 --> 02:02:56,840
Else If the Key Down Arrow is Pressed, then Change y by -1,
2813
02:02:56,840 --> 02:02:59,540
and similar for Left Arrow, similar for Right Arrow.
2814
02:02:59,540 --> 02:03:02,660
And even though there's not a loop in this pink function,
2815
02:03:02,660 --> 02:03:04,310
there is where I'm using it.
2816
02:03:04,310 --> 02:03:06,860
So it's constantly being asked again and again.
2817
02:03:06,860 --> 02:03:08,420
How about feeling for walls?
2818
02:03:08,420 --> 02:03:10,010
Well, over here to the right--
2819
02:03:10,010 --> 02:03:13,970
it's a little cut off-- but here you have, If Touching Left Wall,
2820
02:03:13,970 --> 02:03:15,260
Change x by 1.
2821
02:03:15,260 --> 02:03:17,200
So if you hit the wall, it's too late.
2822
02:03:17,200 --> 02:03:18,950
You're kind of blowing through it already.
2823
02:03:18,950 --> 02:03:23,000
So I want to move it back one pixel so it's no longer touching that wall.
2824
02:03:23,000 --> 02:03:26,670
Similarly, if it's touching the right wall, I want to back it up one pixel
2825
02:03:26,670 --> 02:03:28,220
so it's no longer touching that wall.
2826
02:03:28,220 --> 02:03:31,070
So it's kind of like bouncing off ever so slightly so that it
2827
02:03:31,070 --> 02:03:33,110
doesn't slip through that actual wall.
2828
02:03:33,110 --> 02:03:34,340
And what are those walls?
2829
02:03:34,340 --> 02:03:38,870
Well, notice down here, it's just a simple sprite with a black line
2830
02:03:38,870 --> 02:03:41,790
that I've oriented vertically instead of horizontally.
2831
02:03:41,790 --> 02:03:45,230
And that's just so that I can ask questions of these other two sprites.
2832
02:03:45,230 --> 02:03:47,570
Now, that gives me that form of interactivity.
2833
02:03:47,570 --> 02:03:49,200
What more can I now do?
2834
02:03:49,200 --> 02:03:52,410
Well, what if we make things a little more interactive here?
2835
02:03:52,410 --> 02:03:55,640
Let me go ahead and see inside version 1 our second.
2836
02:03:55,640 --> 02:03:58,860
And let me propose what's going to happen here.
2837
02:03:58,860 --> 02:04:02,600
Well, how might we add a little something like Yale into the mix?
2838
02:04:02,600 --> 02:04:07,460
Well, what's Yale going to do when I hit the Green Flag now based on this code?
2839
02:04:07,460 --> 02:04:09,920
Any hunches?
2840
02:04:09,920 --> 02:04:13,250
Here is the code for my Yale sprite.
2841
02:04:13,250 --> 02:04:13,820
Yeah--
2842
02:04:13,820 --> 02:04:14,570
AUDIENCE: [INAUDIBLE]
2843
02:04:14,570 --> 02:04:15,650
DAVID J. MALAN: Yeah, it's kind of going to be
2844
02:04:15,650 --> 02:04:18,203
an adversarially by blocking my path, theoretically,
2845
02:04:18,203 --> 02:04:19,370
if I keep writing more code.
2846
02:04:19,370 --> 02:04:20,030
So why?
2847
02:04:20,030 --> 02:04:22,160
It too goes to the middle of the screen.
2848
02:04:22,160 --> 02:04:23,940
It points in direction 90 degrees.
2849
02:04:23,940 --> 02:04:26,520
So similarly, there's a whole degree system as well.
2850
02:04:26,520 --> 02:04:27,710
And it forever asks this.
2851
02:04:27,710 --> 02:04:29,750
If Touching the Left Wall Or--
2852
02:04:29,750 --> 02:04:30,920
notice the green block--
2853
02:04:30,920 --> 02:04:33,900
Touching the Right Wall, then just Turn around 180 Degrees.
2854
02:04:33,900 --> 02:04:35,900
And indeed, if you think this through logically,
2855
02:04:35,900 --> 02:04:38,570
that just means you're bouncing this way and this way
2856
02:04:38,570 --> 02:04:42,860
by just flipping yourself around 180 degrees for just this Yale sprite.
2857
02:04:42,860 --> 02:04:46,550
So if I go ahead and zoom in on this and click the Green Flag,
2858
02:04:46,550 --> 02:04:48,350
I can still move up and down.
2859
02:04:48,350 --> 02:04:52,170
But Yale is just kind of doing this all day long, back and forth
2860
02:04:52,170 --> 02:04:53,490
and back and forth, forever.
2861
02:04:53,490 --> 02:04:55,710
Nothing bad happens if I try to go through it.
2862
02:04:55,710 --> 02:04:58,010
But we could add that, certainly, to the mix.
2863
02:04:58,010 --> 02:05:01,760
In fact, let's add one final feature before we play this particular game.
2864
02:05:01,760 --> 02:05:06,320
And let me go ahead and open up the final version of these building blocks
2865
02:05:06,320 --> 02:05:08,640
that adds MIT to the mix.
2866
02:05:08,640 --> 02:05:10,940
So here is MIT.
2867
02:05:10,940 --> 02:05:13,182
Someone want to explain what this code does?
2868
02:05:13,182 --> 02:05:14,390
And this is what we're doing.
2869
02:05:14,390 --> 02:05:15,470
This itself is a skill.
2870
02:05:15,470 --> 02:05:17,810
Reading someone else's code and understanding it
2871
02:05:17,810 --> 02:05:20,640
is half of the part of programming besides writing.
2872
02:05:20,640 --> 02:05:21,140
Yeah--
2873
02:05:21,140 --> 02:05:22,015
AUDIENCE: [INAUDIBLE]
2874
02:05:22,015 --> 02:05:24,960
DAVID J. MALAN: Yeah, it's chasing down the Harvard logo outline.
2875
02:05:24,960 --> 02:05:26,870
So this is apparently the name of the costume
2876
02:05:26,870 --> 02:05:30,050
that this student made, Harvard logo outline.
2877
02:05:30,050 --> 02:05:32,600
And apparently, it goes to a random position first.
2878
02:05:32,600 --> 02:05:34,700
But then it forever points to Harvard.
2879
02:05:34,700 --> 02:05:36,830
So no matter where I'm moving it, up, down, left,
2880
02:05:36,830 --> 02:05:39,413
or right, MIT is being a little more strategic than Yale,
2881
02:05:39,413 --> 02:05:40,830
bouncing back and forth like this.
2882
02:05:40,830 --> 02:05:43,440
So let's go ahead and play this one in full screen.
2883
02:05:43,440 --> 02:05:45,540
And here we have a Green Flag.
2884
02:05:45,540 --> 02:05:50,750
So if I move up, MIT, rather strategically,
2885
02:05:50,750 --> 02:05:53,210
is following me no matter where I go.
2886
02:05:53,210 --> 02:05:55,190
All right, so still, nothing bad happens.
2887
02:05:55,190 --> 02:05:56,940
But now it's struggling, right?
2888
02:05:56,940 --> 02:05:58,190
It's going up, down, up, down.
2889
02:05:58,190 --> 02:06:00,950
It's trying to follow me even though I'm not moving.
2890
02:06:00,950 --> 02:06:02,630
So we need some final flourishes.
2891
02:06:02,630 --> 02:06:06,740
And so I think, for this, we need perhaps one final volunteer.
2892
02:06:06,740 --> 02:06:09,830
After this, cake awaits for everyone outside,
2893
02:06:09,830 --> 02:06:11,990
as is an end of first lecture CS50 tradition.
2894
02:06:11,990 --> 02:06:14,840
Would you like to come up and be our volunteer?
2895
02:06:14,840 --> 02:06:17,230
[APPLAUSE]
2896
02:06:17,230 --> 02:06:23,450
2897
02:06:23,450 --> 02:06:24,040
All right.
2898
02:06:24,040 --> 02:06:28,330
And so this will be the actual version but written by one of your predecessors
2899
02:06:28,330 --> 02:06:30,040
that I'll full screen here.
2900
02:06:30,040 --> 02:06:33,040
It's going to stitch together all of these same primitives and more,
2901
02:06:33,040 --> 02:06:36,430
but add the notion of scores and lives so that there's actually
2902
02:06:36,430 --> 02:06:41,260
a goal, which in this case is to move the Harvard crest to constantly pursue
2903
02:06:41,260 --> 02:06:44,650
the character on the right-hand side so that your sprite touches that one.
2904
02:06:44,650 --> 02:06:46,533
Would you like to introduce yourself?
2905
02:06:46,533 --> 02:06:47,950
AUDIENCE: Hi, my name is Mohammed.
2906
02:06:47,950 --> 02:06:49,000
DAVID J. MALAN: All right, wonderful.
2907
02:06:49,000 --> 02:06:49,630
Welcome aboard.
2908
02:06:49,630 --> 02:06:51,910
And here we come with some instructions and final
2909
02:06:51,910 --> 02:06:56,048
flourish if we want to keep the lights up but perhaps increase the music.
2910
02:06:56,048 --> 02:06:58,236
[MUSIC PLAYING]
2911
02:06:58,236 --> 02:06:58,736
2912
02:06:58,736 --> 02:07:00,486
[MUSIC - MC HAMMER, "U CAN'T TOUCH THIS"]
2913
02:07:00,486 --> 02:07:02,150
MC HAMMER: (SINGING) You can't this.
2914
02:07:02,150 --> 02:07:03,025
You can't touch this.
2915
02:07:03,025 --> 02:07:05,567
DAVID J. MALAN: Notice he is using the up, down, left, right.
2916
02:07:05,567 --> 02:07:07,320
But there's many more walls now.
2917
02:07:07,320 --> 02:07:08,730
First level's pretty easy.
2918
02:07:08,730 --> 02:07:13,560
But now Yale's in the mix, bouncing back and forth.
2919
02:07:13,560 --> 02:07:15,420
Again, pretty easy.
2920
02:07:15,420 --> 02:07:22,290
Now there's two Yale's at slightly different positions.
2921
02:07:22,290 --> 02:07:23,730
MIT is coming soon.
2922
02:07:23,730 --> 02:07:25,432
But first, we have three Yales.
2923
02:07:25,432 --> 02:07:26,640
MC HAMMER: (SINGING) As such.
2924
02:07:26,640 --> 02:07:28,542
And this is a beat, uh, you can't touch.
2925
02:07:28,542 --> 02:07:29,625
DAVID J. MALAN: Very nice.
2926
02:07:29,625 --> 02:07:30,125
[APPLAUSE]
2927
02:07:30,125 --> 02:07:33,100
MC HAMMER: (SINGING) I told you, homeboy, you can't touch this.
2928
02:07:33,100 --> 02:07:36,820
Yeah, that's how it look when you know you can't touch this.
2929
02:07:36,820 --> 02:07:40,798
Look at my eyes, man, you can't touch this.
2930
02:07:40,798 --> 02:07:43,090
Yo, let me bust the funky lyrics. you can't touch this.
2931
02:07:43,090 --> 02:07:45,171
Fresh new kicks and pants, you got to like that.
2932
02:07:45,171 --> 02:07:46,504
Now, you know you want to dance.
2933
02:07:46,504 --> 02:07:48,570
So move out of your seat and get a fly--
2934
02:07:48,570 --> 02:07:49,290
DAVID J. MALAN: You got to go quick.
2935
02:07:49,290 --> 02:07:51,623
MC HAMMER: (SINGING) Catch this beat while it's rolling.
2936
02:07:51,623 --> 02:07:52,130
Hold on.
2937
02:07:52,130 --> 02:07:55,490
Pump a little bit, and let them know what's going on, like that, like that.
2938
02:07:55,490 --> 02:07:57,056
Cold on a mission, so fall on back.
2939
02:07:57,056 --> 02:08:02,800
Let them know that you're too much and this a beat, uh, they can't touch.
2940
02:08:02,800 --> 02:08:03,627
Yo, I told you.
2941
02:08:03,627 --> 02:08:04,502
You can't touch this.
2942
02:08:04,502 --> 02:08:06,882
[APPLAUSE]
2943
02:08:06,882 --> 02:08:08,790
2944
02:08:08,790 --> 02:08:09,810
Yo, sound the bell.
2945
02:08:09,810 --> 02:08:10,975
School's in, sucker.
2946
02:08:10,975 --> 02:08:11,850
You can't touch this.
2947
02:08:11,850 --> 02:08:13,432
Give me a song, a rhythm.
2948
02:08:13,432 --> 02:08:15,390
Making them sweat, that's what I'm giving them.
2949
02:08:15,390 --> 02:08:17,892
Now they know, you talk about the Hammer,
2950
02:08:17,892 --> 02:08:20,810
you talking about a show that's hyped and tight.
2951
02:08:20,810 --> 02:08:24,018
Singers are sweating, so pass them a wipe or a tape to learn.
2952
02:08:24,018 --> 02:08:25,060
What's it going to take--
2953
02:08:25,060 --> 02:08:27,040
DAVID J. MALAN: Second-to-last level.
2954
02:08:27,040 --> 02:08:28,165
MC HAMMER: (SINGING) Legit.
2955
02:08:28,165 --> 02:08:30,065
Either work hard or you might as well quit.
2956
02:08:30,065 --> 02:08:31,410
That's word because you know--
2957
02:08:31,410 --> 02:08:32,535
DAVID J. MALAN: Last level.
2958
02:08:32,535 --> 02:08:35,652
MC HAMMER: (SINGING) You can't touch this.
2959
02:08:35,652 --> 02:08:37,124
DAVID J. MALAN: Hey!
2960
02:08:37,124 --> 02:08:37,624
[LAUGHS]
2961
02:08:37,624 --> 02:08:39,710
[APPLAUSE]
2962
02:08:39,710 --> 02:08:40,210
Congrats.
2963
02:08:40,210 --> 02:08:45,170
2964
02:08:45,170 --> 02:08:47,570
All right, that's it for CS50.
2965
02:08:47,570 --> 02:08:48,410
Welcome.
2966
02:08:48,410 --> 02:08:49,820
Cake is now served.
2967
02:08:49,820 --> 02:08:51,950
We'll see you next time.
2968
02:08:51,950 --> 02:08:53,450
[PROJECTOR CLICKING]
2969
02:08:53,450 --> 02:08:56,800
[MUSIC PLAYING]
2970
02:08:56,800 --> 02:09:29,000
243811
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.