这是用户在 2024-10-18 17:41 为 https://cdn.cs50.net/2023/fall/lectures/0/lang/en/lecture0.txt 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
[MUSIC PLAYING] [AUDIENCE EXCLAIMING] 
[音乐播放] [观众惊呼]


[APPLAUSE]  [掌声]

SPOT: Hello, world. This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming. Woof, woof.
SPOT:你好,世界。这是 CS50,哈佛大学计算机科学及编程艺术的入门课程。汪,汪。


[LAUGHTER]  [笑声]

I'm sorry, Dave.  对不起,戴夫。

[PINK, "WHAT ABOUT US"] What about us? What about us? La-da-da-da-da-da-da. La-da-da-da-da-da-da. We are searchlights. We can see in the dark. We are rockets pointed up at the stars. We are billions of beautiful hearts. And you sold us down the river too far. What about us? What about all the times you said you had the answers? What about us? What about all the broken happy ever-afters? What about us? What about all the plans that ended in disaster? What about love? What about trust? What about us? La-da-da-da-da-da-da. La-da-da-da-da-da-da. La-da-da-da-da-da-da. La-da-da-da-da-da-da.
[PINK, "我们怎么办"] 我们怎么办?我们怎么办?啦-da-da-da-da-da-da。啦-da-da-da-da-da-da。我们是探照灯。我们能在黑暗中看见。我们是指向星星的火箭。我们是数十亿颗美丽的心。你把我们卖到了太远的地方。我们怎么办?你说你有答案的所有时刻怎么办?我们怎么办?所有破碎的幸福结局怎么办?我们怎么办?所有以灾难告终的计划怎么办?爱怎么办?信任怎么办?我们怎么办?啦-da-da-da-da-da-da。啦-da-da-da-da-da-da。啦-da-da-da-da-da-da。啦-da-da-da-da-da-da。


DAVID J. MALAN: All right. So--
大卫·J·马兰:好的。那么——


[APPLAUSE, CHEERING]  [掌声,欢呼]

This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming. And this is Spot. And our thanks to our friends at 3D Cowboys for bringing him by class today. Perhaps a round of applause for our special Professor [INAUDIBLE].
这是 CS50,哈佛大学计算机科学和编程艺术的入门课程。这是 Spot。感谢我们的朋友 3D Cowboys 今天带他来上课。也许为我们的特别教授[INAUDIBLE]鼓掌。


[APPLAUSE]  [掌声]

My name is David Malan. And I actually took this class, CS50, myself, some years ago. But I almost didn't. And I didn't because when I got to campus, as a first-year, I really gravitated toward things already familiar, things with which I was already comfortable, specifically, government. And I came in here thinking I was going to major or concentrate in government. And it was only once I got to sophomore year, fall semester, that some friends of mine were coming to this class called CS50 that was very much to beware at the time, in that it was an unfamiliar field to so many of us, myself included.
我的名字是大卫·马兰。实际上,我几年前也上过这门课,CS50。但我差点没去上。当我作为大一新生来到校园时,我真的倾向于那些我已经熟悉的事物,特别是政府。我当时以为我会主修或专注于政府。直到大二的秋季学期,我的一些朋友开始上这门叫做 CS50 的课,那时这门课对我们许多人来说都是一个陌生的领域,包括我自己。


But I got up the nerve to go over the threshold, sit in on the first class, just shop it, so to speak. And I was hooked. I found that homework was, for the first time, fun. And this was after having enrolled only because the professor let me enroll pass/fail or [INAUDIBLE] because I didn't really think I could even cut it. But fast forward to five Mondays later in the semester, I actually switched to a letter grade, deciding, wow, this is actually something for me.
但我鼓起勇气走过门槛,旁听第一节课,可以说是随便看看。结果我就被吸引住了。我发现作业第一次变得有趣。这是在我仅仅因为教授让我以及格/不及格的方式注册而报名的情况下,因为我真的不认为自己能行。但快进到学期的五个星期一后,我实际上转为字母评分,决定,哇,这实际上是我想要的东西。


And I say this because computer science, what I didn't realize about it at the time, really is this very general purpose way of thinking and way of solving problems. And even if this is the only CS class you ever take, even if this is the only formal training you have in programming as a practical skill, it's just so darn applicable to so many other fields-- the arts, humanities, social sciences, natural sciences, and beyond, and certainly within the STEM fields, themselves.
我这样说是因为计算机科学,那个时候我没有意识到,它实际上是一种非常通用的思维方式和解决问题的方法。即使这是你唯一上过的计算机科学课程,即使这是你在编程方面的唯一正式培训,实际上它对许多其他领域都是如此适用——艺术、人文学科、社会科学、自然科学等等,当然也包括 STEM 领域本身。


That said, it's going to often feel a little something like this. This is from our friends at MIT, down the road, which is one of their hacks whereby they connected a fire hydrant to a drinking fountain, with a little sign up there that says, "GETTING AN EDUCATION FROM MIT IS LIKE DRINKING FROM A FIRE HOSE," which is to say that it's going to feel, quite often, in this class, too, that there's just a lot of information. And you can't possibly absorb it all, but realize that's to be expected, especially in an introductory class.
话虽如此,这通常会感觉有点像这样。这是我们在麻省理工学院的朋友们的一个创意,他们将一个消防栓连接到一个饮水机上,上面有一个小标志写着:“从麻省理工学院获得教育就像从消防水龙带中饮水”,这就是说,在这门课上,你会经常感觉到信息量非常大。你不可能全部吸收,但要意识到这是意料之中的,尤其是在入门课程中。


The whole point is for so much of it to feel new, but with practice, with time, with years, even, looking back, will you get all the more comfortable with the material. But you're not meant to feel comfortable along the way. And so let me encourage you, invite you, expect you to get comfortable feeling uncomfortable along the way, whether you have or have not prior computer science or programming experience.
整个要点是让很多东西感觉新鲜,但随着练习、时间,甚至是多年回顾,你会对这些材料感到更加舒适。但你在这个过程中并不应该感到舒适。因此,我想鼓励你、邀请你、期待你在这个过程中习惯于感到不适,无论你是否有计算机科学或编程的经验。


Now, back in my day, the class' syllabus was a little bit different. And, really, when I and all of my classmates exited the class, we would say to friends that we learned how to program in C, which is a language that we, ourselves, will still learn in this class. But that was it for languages. But nowadays, as you'll see, we'll explore not just this older language called C, but a more modern language called Python that's very much in vogue for data science, and web applications, and much more.
现在,回到我上学的时候,课程大纲有点不同。实际上,当我和我的同学们下课时,我们会对朋友说我们学会了用 C 语言编程,而这门课我们自己仍然会学习这门语言。但这就是所有语言了。但如今,正如你将看到的,我们不仅会探索这种叫做 C 的旧语言,还会探索一种更现代的语言,叫做 Python,它在数据科学、网络应用等领域非常流行。


But we'll also introduce you, along the way, to another language called S-Q-L or SQL, which is specifically for databases. And SQL is a different type of programming language that's just going to give you different ways of solving problems, different building blocks with which to express problems you want to solve. We'll introduce you, toward the end of the semester, yet another language, JavaScript, often used with markup languages called HTML and CSS, with which maybe you have some experience if you've made websites.
但我们还会在这个过程中向你介绍另一种语言,称为 S-Q-L 或 SQL,它专门用于数据库。SQL 是一种不同类型的编程语言,它将为你提供不同的解决问题的方法,以及不同的构建模块来表达你想要解决的问题。在学期末,我们还会向你介绍另一种语言,JavaScript,通常与标记语言 HTML 和 CSS 一起使用,如果你制作过网站,可能对它们有一些经验。


But we'll do this because so many of today's actual real-world software is web-based. Or it's phone-based, mobile-based, but even then, it's using these same languages, like JavaScript. And so by the end of CS50, in particular, you won't know how to program in X, or Y, or Z, per se. But, period, you'll learn how to program. And, indeed, among the goals of this class is to equip you with enough of a set of concepts, enough practical skills and experience that after CS50, again, if you never take another CS class, you can teach yourself new languages.
但我们这样做是因为今天许多实际的现实世界软件都是基于网络的。或者是基于手机、移动设备的,但即便如此,它们也使用这些相同的语言,比如 JavaScript。因此,到 CS50 课程结束时,特别是,你不会确切地知道如何编程 X、Y 或 Z。但,总之,你会学会如何编程。实际上,这门课的目标之一是让你掌握足够的概念、足够的实践技能和经验,以便在 CS50 之后,即使你不再上其他计算机科学课程,你也能自学新的语言。


And you won't feel-- you won't have been reliant on a class to, indeed, introduce you to each and one of those ideas. And what are we going to do, then, in this class? And, really, what are we going to start doing today? Well, we're going to learn how to solve problems. And that's really what computer science is all about. We'll, very specifically, today, knock off a few to-dos. We'll learn how to represent simple things, like numbers. We'll learn how to represent letters of an alphabet, like English.
你不会觉得——你不会依赖于一门课程来确实向你介绍每一个想法。那么,我们在这门课上要做什么呢?实际上,我们今天要开始做什么?好吧,我们要学习如何解决问题。这正是计算机科学的核心。今天,我们将具体完成一些待办事项。我们将学习如何表示简单的事物,比如数字。我们将学习如何表示字母表中的字母,比如英语字母。


We'll learn how to represent colors-- red, green, blue, and everything in between. We'll learn how to represent, more interestingly, full-fledged images that you might see on the web or your phone. We'll talk about representing videos and even audio files. So, by the end of today, alone, you will exit here with a newfound mental model for how you can represent all of today's media and multimedia that we take for granted and use most every day.
我们将学习如何表示颜色——红色、绿色、蓝色以及介于它们之间的所有颜色。我们将更有趣地学习如何表示完整的图像,这些图像可能出现在网络或您的手机上。我们还将讨论如何表示视频甚至音频文件。因此,到今天结束时,您将带着一种全新的思维模型离开这里,了解如何表示我们理所当然并且几乎每天都在使用的所有媒体和多媒体。


But we'll also focus today, ultimately, on how to write algorithms, like step-by-step instructions for solving some problem, specifically, implementing algorithms with code. And that's what we'll find. An algorithm is just something you can express in English or any human language, but code is a translation of that to, presumably, the 0's and 1's that you've probably heard is all that computers ultimately speak.
但我们今天最终也会关注如何编写算法,就像解决某个问题的逐步指令,特别是用代码实现算法。这就是我们要找到的。算法只是你可以用英语或任何人类语言表达的东西,但代码是将其翻译成你可能听说过的计算机最终只会说的 0 和 1。


And if you're feeling like that's a lot for today, if you're feeling like that's a lot for the whole semester, realize and take comfort in knowing that -thirds of you have never taken a CS course before. So even if you think, like I did all those years ago, that, surely, my friends, the kids I didn't even in the class, must know more than I, have been programming since they were six years old. That's just not the case. You're in very much good company. And within the class will you find different tracks, by way of the homework assignments, called "problem sets," by way of the sections or recitations.
如果你觉得今天的内容很多,或者觉得整个学期的内容很多,请意识到并感到安慰的是,三分之二的同学之前从未上过计算机科学课程。因此,即使你像我多年前那样认为,当然,我的朋友们,那些我在课堂上甚至不认识的孩子们,一定比我知道得多,他们从六岁就开始编程。这根本不是事实。你并不孤单。在课堂上,你会通过作业(称为“问题集”)和小组讨论找到不同的学习路径。


There will be different tracks for those of you less comfortable, more comfortable, somewhere in between. And if you really don't know why you are here today, we'll even have sections for those least comfortable, where you just don't necessarily feel ready to dive into CS or maybe STEM, more generally. But we'll get you there by way of the course's support structure.
将为那些不太自信、比较自信以及介于两者之间的人提供不同的课程。如果你真的不知道今天为什么在这里,我们还会为那些最不自信的人设置部分,你可能还没有准备好深入计算机科学或更广泛的 STEM 领域。但我们会通过课程的支持结构帮助你达到目标。


And it's very much grounded in this mindset. What ultimately matters in this class is not so much where you end up relative to your classmates, but where you end up relative to yourself, when you began. So whether you have or have not prior programming or CSS experience, it's not going to matter when it comes to evaluation, when it comes to the output, be it a grade, or a satisfactory mark, or the like. It's going to depend on, really, where you are today, in this, what we call week zero, versus the very end of the semester, when you will have built something grand, of your very own, in software.
这非常扎根于这种心态。在这门课中,最终重要的不是你相对于同学的位置,而是你相对于自己,与你开始时的位置。因此,无论你是否有编程或 CSS 的经验,在评估时,这都不会成为问题,无论是成绩、满意度评分还是其他。这实际上取决于你今天在我们所称的零周的位置,与学期结束时的你相比,那时你将会构建出属于你自己的宏伟软件。


But CS50 is also characterized, fairly uniquely, by its community, its culture. And along the way, you'll see that your experience is punctuated by a lot of social and academic events alike. CS50 lunches, most every Friday, we'll gather at a nearby restaurant called Changsho where we'll have Chinese lunch together. And as many of you as might want to attend that week will be able to join us, sit down, and chat casually with me, the course's teaching staff, friends of ours, alumni from industry, as well.
但 CS50 也以其社区和文化独特地为人所知。在这个过程中,你会发现你的经历中充满了许多社交和学术活动。CS50 午餐,每个星期五,我们会在附近一家叫 Changsho 的餐厅聚会,一起享用中餐。那一周有兴趣参加的同学都可以加入我们,坐下来与我、课程的教学团队、我们的朋友以及行业校友轻松聊天。


CS50 Puzzle Day, coming up this weekend, will be an opportunity, even if you have no prior CS experience, just to solve problems, not jigsaw puzzles, but puzzles in the logical sense. We'll hand you a packet of puzzles-- logic problems, or riddles, or the like-- that you, as teams, can solve together. And at the very end, we'll walk you through. And along the way, there'll be not only these puzzles, but pizza, and prizes, and so much more, as well.
CS50 拼图日将在这个周末举行,即使你没有任何计算机科学经验,这也是一个解决问题的机会,不是拼图,而是逻辑意义上的难题。我们会给你一包难题——逻辑问题、谜语或类似的东西——你们可以作为团队一起解决。最后,我们会带你们一起走过这个过程。在这个过程中,不仅有这些难题,还有比萨、奖品以及更多的惊喜。


Towards the end of the semester, we'll have a CS50 Hackathon whereby we'll get together around 7:00 PM, at the start of reading period, and we'll finish up around 7:00 AM the next morning. And it will be this opportunity to really bond with your classmates, your project partners, if you work in a team, on your very own final project, which is meant to be a capstone of the course, something you build, yourselves, that we don't hand you a specification for. But it's your final offboarding, so that when you exit CS50, you don't need CS50 anymore.
学期末,我们将举行一次 CS50 黑客马拉松,大家将在晚上 7:00 聚集,在阅读期开始时,直到第二天早上 7:00 结束。这是一个真正与同学、项目伙伴(如果你是团队合作的话)建立联系的机会,专注于你们自己的最终项目,这个项目是课程的顶点,是你们自己构建的,我们不会给你们提供规范。但这是你们的最终离职项目,这样当你们完成 CS50 时,就不再需要 CS50 了。


You don't need me. You don't need your TF. You can actually write code and solve problems on your own. So this picture here is one of our past photos from earlier in the evening. Things get a little quieter as we then, around 5:00 AM, drive anyone who's still awake and energetic to a nearby IHOP for pancakes, around then. But here is how most of the evenings tend to end for at least some of your classmates prior.
你不需要我。你不需要你的 TF。你实际上可以自己编写代码并解决问题。所以这里的照片是我们晚上早些时候的过去照片。大约在早上 5:00,气氛会稍微安静一些,我们会带着仍然清醒和充满活力的同学去附近的 IHOP 吃煎饼。至于你们的一些同学,晚上的大多数时间通常是这样结束的。


But at the very end of the class is the so-called CS50 Fair, an exhibition of all of your final projects for friends, faculty, students, and staff across campus, whereby you'll be invited to a space like this. Bring your laptop. Bring your phone, whatever it is you have built and created. And we'll just show it off for a bit of the afternoon, complete with music, and friends from industry, and candy, and all what makes gathering together at term's end fond. And you'll wear, ultimately, very proudly, we hope, your very own "I took CS50," stating, very matter of factly, what I did some years ago; that, indeed, this was a course I, myself, took.
但在课程的最后,所谓的 CS50 展会将会举行,这是一个展示你所有最终项目的机会,邀请校园内的朋友、教职员工和学生。你将被邀请到这样的空间。带上你的笔记本电脑。带上你的手机,无论你构建和创造了什么。我们将在下午的时间里展示这些,伴随着音乐、行业朋友、糖果,以及所有让学期结束时聚会变得愉快的元素。最终,我们希望你能非常自豪地穿上你自己的“我上过 CS50” T 恤,毫不含糊地表明,几年前我做过的事情;确实,这是我自己上过的一门课程。


So, today, let's focus, then, on computer science, like what is it. Well, it's really the study of information. But, really, it's, more specifically, about solving problems, using certain ideas and techniques, all of which you'll exit the course with. So, as such, problem solving is a goal that we'll approach by way of something called "computational thinking." So computational thinking, you can oversimplistically think about it as thinking like a computer.
所以,今天我们就来关注一下计算机科学,它到底是什么。其实,它是信息的研究。但更具体地说,它是关于使用某些思想和技术来解决问题,而这些你将在课程结束时掌握。因此,解决问题是我们将通过一种叫做“计算思维”的方式来实现的。因此,你可以过于简单地将计算思维理解为像计算机一样思考。


But it's really the application of ideas that we'll dive into today and we'll finish some weeks from now, that you can apply to problems from this field or most any other, but in a computational, that is, a very methodical, very careful way. And that's what CS really did for me and does for a lot of people. It helps clean up your thought processes. Even if you go off into the real world and do nothing in tech, you have an ability, after classes like this, to express yourself a little more correctly, more precisely and, generally, having better command of your own ideas and your language.
但今天我们真正要深入探讨的是思想的应用,我们将在几周后完成这个过程,这些思想可以应用于这个领域或大多数其他领域,但以一种计算的方式,也就是非常有条理、非常谨慎的方式。这就是计算机科学对我以及许多人所做的。它帮助理清你的思维过程。即使你走入现实世界,做一些与技术无关的事情,经过这样的课程后,你也能更准确、更精确地表达自己,并且通常能更好地掌握自己的想法和语言。


So what's problem solving? Let me propose that this is it. This is as simple as we can make today's goals and the semester's goals. Problems to be solved look like this. You've got some input, the problem to be solved. You've got a goal being the output, like the solution there, too. And then somewhere in the middle is the secret sauce, where we'll spend the next several weeks figuring out how we can convert these inputs to outputs.
那么什么是问题解决呢?让我来提议一下,这就是它。这就是我们今天的目标和本学期目标可以简单化的方式。待解决的问题看起来是这样的。你有一些输入,即待解决的问题。你有一个目标作为输出,比如解决方案也是如此。然后在中间的某个地方是秘密武器,我们将在接下来的几周里研究如何将这些输入转换为输出。


But before we can do that, we all just have to agree on how to represent these inputs and outputs, whether it's with English or, really, any type of language. But, as I spoiled earlier, you probably came in here already with a general sense that, yeah, computers somehow only speak or know 0's and 1's, the so-called binary system. But that's just one way of representing information.
但在我们能够做到这一点之前,我们都必须就如何表示这些输入和输出达成一致,无论是用英语还是实际上任何类型的语言。但是,正如我之前提到的,你们可能已经有了一种普遍的认识,即计算机以某种方式只会说或知道 0 和 1,即所谓的二进制系统。但这只是表示信息的一种方式。


Even simpler than binary is unary. So if you've ever, at this age or any prior age, counted on your fingers, this is unary notation, whereby each of your digits, your fingers literally represent some piece of information; taking attendance, like 1, 2, 3, 4, 5. But on your one human hand, how high can you count in this unary notation?
比二进制更简单的是一元制。因此,如果你在这个年龄或任何之前的年龄曾用手指计数,这就是一元表示法,你的每个数字,你的手指实际上代表一些信息;点名,比如 1、2、3、4、5。但是在你的一只手上,你能在这种一元表示法中数到多高?


AUDIENCE: Five.  受众:五。

AUDIENCE: Five.  受众:五。

DAVID J. MALAN: Five, I'm hearing five. Six, I heard one six. But I'm going to go further and say the answer, if you're clever about it, is actually--
大卫·J·马兰:五,我听到五。六,我听到一个六。但我会更进一步说,如果你聪明的话,答案实际上是——


AUDIENCE: 40?  受众:40?

DAVID J. MALAN: Not quite 40. You overbid, but--
大卫·J·马兰:还不到 40。你出价过高,但——


AUDIENCE: 31.  受众:31。

DAVID J. MALAN: 31 is as high as I can actually count. And that's because if I actually-- and if you're thinking this is weirdly painful now, it will be, but this is my hand representing the number 0. Normally, in unary, this is 1, 2, 3, 4, 5, of course, obviously. But what if I take into account the order in which I'm putting my fingers up and down? So maybe this is still 0. Maybe this is still 1. But maybe this is now 2, where it's just the single second finger up, not two of them, total.
大卫·J·马兰:31 是我实际上能数到的最高数字。这是因为如果我真的这样做——如果你觉得现在这很奇怪且痛苦,那确实会是,但这是我的手代表数字 0。通常,在一元计数中,这是 1、2、3、4、5,当然,显而易见。但如果我考虑到我抬起和放下手指的顺序呢?所以也许这仍然是 0。也许这仍然是 1。但也许这现在是 2,只有第二根手指抬起,而不是两根手指,总共。


Maybe this is now 3. Maybe this is now-- often offensive, with just the middle finger up. This is now [LAUGHS] 5. This is now 6. This is now 7. And my hand just hurts too much if I try to count higher than seven. But, theoretically, because each of my fingers can be down or up and I've got five of them, that's actually 32 possible permutations, up and down. But wait a minute. We said, 31, but if you start at 0. You have to subtract 1 from the biggest possible value.
也许现在是 3。也许现在是——经常冒犯,只有中指竖起。这现在是[笑] 5。这现在是 6。这现在是 7。如果我试着数超过七,我的手就太痛了。但是,理论上,因为我的每个手指可以向下或向上,而我有五个手指,这实际上有 32 种可能的排列,上下。但是等一下。我们说 31,但如果从 0 开始。你必须从最大的可能值中减去 1。


So this is to say you and I have been using unary because it's just simple, and it gets the job done. But if we just think about representation a little more cleverly, we can do exactly what computers do, using not what mathematicians call "base-1," where the finger is either there or it's not, but base-2. And in base-2, we just need two digits at our disposal. And we could call these digits 1 and 2, A and B, black or white. We just need two words to describe two pieces of information.
所以这就是说你我一直在使用一元,因为这很简单,而且能完成任务。但如果我们稍微聪明一点地考虑表示法,我们可以做到计算机所做的,使用不是数学家所称的“基数-1”,即手指要么在,要么不在,而是基数-2。在基数-2 中,我们只需要两个数字可供使用。我们可以将这些数字称为 1 和 2,A 和 B,黑或白。我们只需要两个词来描述两条信息。


Computers keep it simple. And just like we humans start counting 0, 1, 2, 3 on up, computers use 0 and 1, and that's it. But that's by convention. But why do they do that? Well, it turns out, when you use base-2, otherwise known as binary, well, it just maps really readily to the real world. Because, at the end of the day, what do we all do, if you've got a laptop, or a phone, or any device? You plug it into the wall because it needs electricity at some point.
计算机保持简单。就像我们人类从 0、1、2、3 开始计数一样,计算机也使用 0 和 1,仅此而已。但这只是约定。那么它们为什么这样做呢?其实,当你使用二进制,也就是基数为 2 时,它与现实世界的映射非常直接。因为,归根结底,我们所有人都在做什么,如果你有一台笔记本电脑、手机或任何设备?你把它插入墙壁,因为它在某个时刻需要电。


And what if you have electricity or not? Well, there's your two possible values. Either it's there, or it's not. And because computers are electrical devices, this is why binary is just useful. It's nice and simple. Either electricity is there, or it's not. So when you plug this device in and you've got all these electrons or whatever flowing, maybe if we just hang on to some of that electricity, we can represent what we'll call a 1. And maybe if we let it dissipate or go away, that's a 0. So on and off maps very readily to this idea of just 0's and 1's.
如果你有电或者没有电呢?好吧,这就是你可能的两个值。要么有,要么没有。因为计算机是电器设备,这就是为什么二进制是如此有用。它简单明了。要么有电,要么没有电。因此,当你插入这个设备并且有这些电子流动时,也许如果我们保留一些电,我们可以表示为 1。也许如果我们让它消散或消失,那就是 0。因此,开和关很容易映射到这个 0 和 1 的概念上。


And if you've ever thought of this, now, as binary digits-- "bi" implying 2, 0 and 1-- well, if you've ever heard this term now, "bit," it just means binary digit. A single bit is just a 0 or 1. But we could have called these things anything we want. Now how does this map to be clear to the real world? Well, we can't see the tiny little switches inside of our Macs, PCs, and our phones that are actually turning the electricity on or off, storing electricity or not. But they're called transistors.
如果你曾经想到这一点,现在作为二进制数字——“bi”意味着 2,0 和 1——那么,如果你听过这个术语“bit”,它只是意味着二进制数字。一个 bit 就是 0 或 1。但我们可以把这些东西叫成任何我们想要的。那么,这如何清晰地映射到现实世界呢?我们看不到我们 Mac、PC 和手机内部那些实际上在打开或关闭电流、存储电流或不存储电流的小开关。但它们被称为晶体管。


They've got millions of them in today's hardware. And they're just on or off, like a switch or a light bulb. So, for instance, if there's no electricity, the switch is off, we would call this, by convention, a 0. If, though, you throw the switch and it actually turns on, we would call this--
他们在今天的硬件中有数百万个。它们就像开关或灯泡一样,仅有开或关。因此,例如,如果没有电,开关是关闭的,我们通常称之为 0。然而,如果你打开开关并且它真的亮了,我们会称之为——


AUDIENCE: On.  受众:开启。

DAVID J. MALAN: --an "on," exactly, a 1. We could have reversed it. But this is just the way the world decided to standardize. And that's it. So you've either got something on or off, a 1 or a 0. And this, then, is this thing we know now as a binary digit or a bit. So once we've got these values, what about how-- how can we go about, perhaps, representing things? Well, you know what? It turns out we've got a lot of light bulbs right here. Let me grab-- thanks. Excuse me, spot.
大卫·J·马兰:--一个“开”,确切地说,是 1。我们本可以反转它。但这就是世界决定标准化的方式。就这样。所以你要么有东西开着,要么关着,1 或 0。那么,这就是我们现在所知道的二进制数字或位。因此,一旦我们得到了这些值,关于如何--我们可以如何表示事物呢?好吧,你知道吗?事实证明,我们这里有很多灯泡。让我拿一下--谢谢。打扰一下,位置。


Let me grab the little music stand here. Let me borrow a couple of these bulbs and see if we can't make clearer than my hand, alone, what's going on here. So I'm going to go ahead and grab two of these. And I'll just put them here. And I can turn these things on or off now. So if I've got two bits, two switches, two transistors, if you will, well, if I go ahead and turn on this one, I'm representing what number in binary, perhaps?
让我拿一下这里的小音乐架。让我借几个灯泡,看看能不能比我单手更清楚地展示这里发生了什么。所以我会先拿两个。然后我就把它们放在这里。现在我可以打开或关闭这些东西。所以如果我有两个部分,两个开关,两个晶体管,如果你愿意的话,那么如果我打开这个,我在二进制中表示的是什么数字呢?


AUDIENCE: 1.  受众:1。

DAVID J. MALAN: So just 1. Now, if I'm using unary, I would turn this one on and be done with it. And that's 2, but not in binary. Binary, it's the permutations, which ones are on and off, that matters. So what, now, am I representing here, perhaps?
大卫·J·马兰:所以只有 1。现在,如果我使用一元运算,我会打开这个,然后就完成了。这是 2,但不是在二进制中。在二进制中,重要的是哪些是开启和关闭的排列。那么,现在,我在这里表示的是什么呢?


AUDIENCE: 2.  受众:2。

DAVID J. MALAN: 2. So this is when I put my single pointer finger up. But then when I did this, in my human hand, this was like representing the number 3. How do I represent the number 4. Yeah, I need another light bulb, so I need more hardware, so to speak. So if I turn-- if I leave this one-- if I turn this one on, this one off, this one off, now I have the number 4. And someone tell me, saying the words "on" and "on" and "on," or "on," or "off," or "on," using combinations of "on," "off" and-- "on" and "off," how do I represent 5, from your left to your right? How about over here?
大卫·J·马兰:2. 所以这是我把我的单个手指竖起来的时候。但当我这样做时,在我的人类手中,这就像代表数字 3。我怎么表示数字 4。是的,我需要另一个灯泡,所以我需要更多的硬件,可以这么说。所以如果我把这个留着——如果我把这个打开,这个关掉,这个关掉,现在我有数字 4。有人告诉我,使用“开”和“开”和“开”或者“开”或者“关”或者“开”的组合,如何从左到右表示 5?那这里呢?


AUDIENCE: On, off, on.
受众:开,关,开。


DAVID J. MALAN: "On, off, on," I heard. And that's exactly right. And how do I represent, maybe, 6? Over here?
大卫·J·马兰:“开,关,开,”我听到。没错。那么我怎么表示,也许,6?在这里?


AUDIENCE: Off, on, on.
受众:关闭,开启,开启。


DAVID J. MALAN: Off, on, on, not quite. From left to right?
大卫·J·马兰:关,开,开,不完全。 从左到右?


AUDIENCE: Off-- the other way.
受众:离开——另一条路。


DAVID J. MALAN: The-- [LAUGHS] OK, so from right to left. So I think we leave this one on. This one, I'm going to claim, represents, now, 6 and 7.
大卫·J·马兰:这个—— [笑] 好的,从右到左。所以我认为我们把这个留着。这个,我要声称,代表现在的 6 和 7。


AUDIENCE: On, off.  受众:开,关。

DAVID J. MALAN: I'm just going to-- it's actually going to be "on, on, on." Now, if you're wondering, where are these people coming up with these combinations, there's actually a system here. It's actually hard for me to do it backwards. But it turns out there's actually a system that's not all that unfamiliar. In fact, let me propose this. Let me propose that we consider what you and I all learned in grade school, which was something like the base-10 system, 10 meaning that you use 10 different digits, not two, 10. So 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, that's the base-10 system, otherwise known as decimal, "dec" implying 10.
大卫·J·马兰:我将要——实际上是“继续,继续,继续”。现在,如果你在想,这些人是从哪里想到这些组合的,实际上这里有一个系统。对我来说,倒着做这件事其实很难。但事实证明,这里实际上有一个并不陌生的系统。实际上,让我提出这个。让我提议我们考虑一下你我在小学时学到的东西,那就是类似于十进制系统,10 意味着你使用 10 个不同的数字,而不是两个,10。所以 0,1,2,3,4,5,6,7,8,9,这就是十进制系统,也称为十进制,“dec”意味着 10。


So that's what you and I use every day. Well, let's think about how we represent numbers in decimal, in the normal human way. Well, here is a number. It's what number, of course?
所以这就是你我每天使用的东西。那么,让我们想想我们如何以十进制的方式表示数字,以人类的正常方式。好吧,这里有一个数字。这是什么数字,当然?


AUDIENCE: 123.  受众:123。

DAVID J. MALAN: 123. So we all just have an intuition for that, obviously. But it's not necessarily 123. You're just assigning meaning to the positions of these digits. This is really the pattern 1-2-3. But you immediately jump, mathematically, to 123, but why? Well, odds are, in grade school, you learned that the rightmost digit is the ones place or the ones column. This is the tens place or the tens column. This is the hundreds place.
大卫·J·马兰:123。所以我们显然都有这种直觉。但这不一定是 123。你只是给这些数字的位置赋予意义。这实际上是模式 1-2-3。但你立即在数学上跳到 123,但为什么呢?好吧,可能在小学时,你学过最右边的数字是个位或个位列。这是十位或十位列。这是百位。


And so why is this relevant? Well, this is like doing 100 times 1 plus 10 times 2 plus 1 times 3 or, if we multiply that out, 100 plus 20 plus 3, ergo, the number, now, that we just take for granted is 123. But that's in base-10, the so-called decimal system, whereby each of these digits is in a different column. And those columns are, again, ones place, tens, hundreds. If we keep going, thousand, ten thousand, and so forth.
那么这有什么相关性呢?这就像是做 100 乘以 1 加上 10 乘以 2 加上 1 乘以 3,或者如果我们把它乘开,100 加 20 加 3,因此,我们现在理所当然认为的数字是 123。但这是在十进制中,也就是所谓的十进制系统,其中每个数字位于不同的列。这些列分别是个位、十位、百位。如果我们继续下去,还有千位、万位等等。


But where did these come from? Well, here's the base. If you remember exponents and all of that, this is just 10 to the 0, 10 to the 1, 10 to the 2, ad infinitum. And so, now, what if we just changed the base from 10, 0 through 9, to just two digits, 0 and 1? Well, now the math is fundamentally the same. But it's 2 to the 0, 2 to the 1, 2 to the 2, which gives us the ones place, twos place, and fours place.
但这些来自哪里呢?好吧,这就是基数。如果你记得指数和所有这些,这只是 10 的 0 次方,10 的 1 次方,10 的 2 次方,依此类推。那么,现在如果我们将基数从 10(0 到 9)改为只有两个数字,0 和 1 呢?好吧,现在数学基本上是一样的。但它是 2 的 0 次方,2 的 1 次方,2 的 2 次方,这给我们提供了个位、二位和四位。


Now, why is this relevant? If you've got three light bulbs or three bits that are off, off, off, what have I done? 4 times 0 plus 2 times 0 plus 1 times 0 is, obviously, the number you and I know, in decimal, as 0. This, though, represents the number you and I know as--
现在,这有什么相关性呢?如果你有三个灯泡或者三个位都是关的,关,关,关,我做了什么?4 乘以 0 加 2 乘以 0 加 1 乘以 0,显然是我们在十进制中知道的数字 0。不过,这代表的是我们知道的数字--


AUDIENCE: 1  受众:1

DAVID J. MALAN: This represents--
大卫·J·马兰:这代表——


AUDIENCE: 2.  受众:2。

DAVID J. MALAN: --3, 4. And just to be clear, this is why, when I grabbed the additional light bulb, we claimed that this now was 4. Because we had "on, off, off," "on, off, off." This now is 5. This now is 6. This now is 7. And if I wanted to count higher, what would the pattern of 0's and 1's be for the number 8?
大卫·J·马兰:--3, 4。为了明确,这就是为什么当我拿起额外的灯泡时,我们声称这现在是 4。因为我们有“开,关,关”,“开,关,关”。这现在是 5。这现在是 6。这现在是 7。如果我想数得更高,数字 8 的 0 和 1 的模式会是什么?


AUDIENCE: 1, 0--  受众:1,0--

DAVID J. MALAN: 1, 0, 0, 0. So we just need more hardware, more bits. So it wasn't arbitrary, even if it was non-obvious, what I was actually doing with turning these light bulbs on and off. Now, it turns out here we are talking about how to represent information, like numbers, but we could also use bits, 0's and 1's, light bulbs to represent instructions, as well. Because, at the end of the day, that's all computers do. They process data, information of some sort, whether it's files, or numbers, or images, or videos, or the like.
大卫·J·马兰:1,0,0,0。所以我们只需要更多的硬件,更多的比特。因此,即使我实际在做的事情——开关这些灯泡——并不是显而易见的,但这并不是任意的。现在,事实证明,我们在谈论如何表示信息,比如数字,但我们也可以使用比特,0 和 1,灯泡来表示指令。因为,归根结底,这就是计算机所做的全部。它们处理数据,某种形式的信息,无论是文件、数字、图像、视频或其他。


And you do things with those files. You open them. You print them. You edit them and the like. So there's this notion of instructions, what the computer can actually do. And I bet we could come up with some pattern of 0's and 1's or, equivalently, light bulbs that tell even Spot what to do. Maybe, go up, or down, or left, or right. And it could certainly do this autonomously by using various sensors. We need to keep things safe today. We're using Wi-Fi in sending Spot these very instructions.
你可以对这些文件进行操作。你打开它们,打印它们,编辑它们等等。因此,有了指令的概念,计算机实际上可以做什么。我敢打赌,我们可以想出一些 0 和 1 的模式,或者等效地,灯泡,告诉 Spot 该做什么。也许,向上,向下,向左,或向右。它当然可以通过使用各种传感器自主完成这些任务。我们今天需要保持安全。我们正在使用 Wi-Fi 向 Spot 发送这些指令。


But what's happening, wirelessly, with our friend Andrew, here, is, essentially, he's sending Spot instructions just encoded, wirelessly, somehow, as patterns of 0's and 1's. And the folks at Boston Dynamics, who built this robot, programmed Spot to recognize certain patterns as meaning "up," a certain pattern as meaning "down," "left," "right," and any number of other things. So, in fact, Spot, come on over here, if you could. Come on, Spot.
但是,发生在我们朋友安德鲁身上的无线情况基本上是,他以某种方式将 Spot 的指令编码为 0 和 1 的模式无线发送出去。而构建这个机器人的波士顿动力公司编程了 Spot,使其能够识别某些模式代表“上”,某种模式代表“下”,“左”,“右”,以及其他许多东西。因此,实际上,Spot,过来这里,如果可以的话。来吧,Spot。


[FOOTSTEPS]  [脚步]

OK. So Spot, for instance, may very well have a pattern of 0's and 1's that represents shake. Shake, Spot. So he could do that and any number of other movements, as well. And maybe, especially with sensors here and also a little human help over here, for today, what if we went ahead and did something-- maybe ask Spot some questions? So let's go ahead, and we'll start simple. Spot, here we have some bits, "off, off, on." Spot, what [LAUGHS]-- OK. Spot, what is this representing, "off, off, on"?
好的。那么,Spot 可能有一个代表摇动的 0 和 1 的模式。摇动,Spot。所以他可以这样做,也可以做其他许多动作。也许,特别是这里有传感器,还有一点人类的帮助,今天我们要做点什么——也许问 Spot 一些问题?那么我们就开始简单的。Spot,这里我们有一些位,“关,关,开。”Spot,这代表什么,[笑]——好的。Spot,“关,关,开”代表什么?


[SPOT TAPS STAGE]  [点触阶段]

Correct? I think so. Very horse-like, OK. Thank you. All right, so a round of applause for Spot here.
正确吗?我想是的。非常像马,好的。谢谢。好了,请为 Spot 鼓掌。


[APPLAUSE]  [掌声]

All right. So, Spot, what if we [LAUGHS] turn-- OK. We'll turn that one off here. How about if we turn this one on? So it's "off, on, off." Spot, what's this number?
好的。那么,Spot,如果我们[笑]关掉这个--好的。我们在这里关掉那个。那如果我们打开这个呢?所以是“关,开,关。”Spot,这个数字是什么?


[SPOT TAPPING STAGE]  [点触阶段]

Is that correct? Nice. OK.
那是正确的吗?很好。好的。


[APPLAUSE]  [掌声]

[LAUGHS] How about one final flourish? How about, Spot, instead of "off, on, off," let's go ahead and do "off, on, on"? So think, in your mind's eye, what the answer should be. All right, Spot, "off, on, on."
[大笑] 最后来个华丽的收尾怎么样?Spot,咱们不说“关,开,关”,而是“关,开,开”吧?所以在你脑海中想象一下,答案应该是什么。好了,Spot,“关,开,开”。


[SPOT LANDING ON THE STAGE]
[舞台上的精准着陆]


OK. And a round of applause for, Spot, as well. [LAUGHS]
好的。也为 Spot 鼓掌。[笑]


[APPLAUSE]  [掌声]

So this is to say, no matter how-- thank you, Spot. No matter how fancy today's hardware or software is, it really just boils down to representing information and instructions. And computers, and phones, and the like really are just operating on those same pieces of information, whether implemented in 0's and 1's or with, really, anything else. All right.
所以这就是说,无论今天的硬件或软件多么华丽,它实际上归结为表示信息和指令。计算机、手机等实际上都是在处理这些相同的信息,无论是用 0 和 1 实现的,还是用其他任何东西。好的。


So, where can we take this, once we have this agreed-upon system for representing [LAUGHS] information? Well, it turns out that using three bits, three 0's and 1's, at a time, isn't actually all that useful. And you and I, even in conversation, don't often say the word "bit." We say the word "byte." And what is a byte, if familiar? Yeah?
那么,一旦我们达成了这个表示[笑]信息的系统,我们可以将其带到哪里呢?实际上,使用三个比特,三个 0 和 1,同时并不是特别有用。你我在对话中也不常说“比特”这个词。我们说“字节”这个词。如果熟悉的话,字节是什么呢?对吗?


AUDIENCE: It's eight bits.
受众:这是八位。


DAVID J. MALAN: So it's just eight bits. It's just a more useful unit of measure. And it happens to be a power of 2, 2 to the third, which just makes math work out cleanly. But it's just a convention, to have more interesting units of measure than individual bits. So a byte is eight bits. So, for instance, this represents, using eight bits, eight light bulbs, the number you and I know is 0. And this byte of all 1 bits-- now you've got to do some quick math-- represents what number, instead? So it's all 1's; eight of them, total. How about over here, on the end?
大卫·J·马兰:所以这只是八位。它只是一个更有用的测量单位。它恰好是 2 的幂,2 的三次方,这使得数学运算变得简单。但这只是一个约定,目的是为了有比单个比特更有趣的测量单位。所以一个字节是八位。例如,这表示使用八位,八个灯泡,您我所知道的数字是 0。而这个全是 1 的字节——现在你得做一些快速的数学运算——表示什么数字呢?所以它全是 1;总共八个。那么这里的末尾呢?


AUDIENCE: 255.  受众:255。

DAVID J. MALAN: So it's, indeed, 255. Now, that's not the kind of math that you need to do in your head for a class like this, but you could. This is the ones place, twos, fours, eight, 16, 32, 64, 128. And because they're all 1, you just have to add up all of those columns' values. And you get 255.
大卫·J·马兰:所以,确实是 255。现在,这不是你在这样的课堂上需要在脑海中计算的那种数学,但你可以。这是个位、十位、四位、八位、十六位、三十二位、六十四位、一百二十八位。因为它们都是 1,你只需将所有这些列的值相加。你得到 255。


But a little mental trick, too, is that, if you've got eight bits and each of them can be two possible values, 0 or 1, that's like 2 possibilities here times 2 times 2 times 2 times 2, eight times. So that's 2 to the eighth, so that is maybe a little easier to do. That's 256. Or easier, in the sense that you get used to seeing these numbers in CS. That's 256. But wait a minute. How do I reconcile this with your 255? Because you start at 0. So you lose one on the high end because we started counting and representing the number, like 0.
但是一个小的心理技巧是,如果你有八个比特,每个比特可以有两个可能的值,0 或 1,这就像是 2 的可能性乘以 2 再乘以 2 再乘以 2,总共八次。所以这是 2 的八次方,所以这可能更容易计算。这是 256。或者说更容易,因为你习惯于在计算机科学中看到这些数字。这是 256。但是等一下。我该如何将这个与你的 255 对齐?因为你是从 0 开始的。所以在高端你损失了一个,因为我们开始计数和表示这个数字,比如 0。


All right. Questions on how we've represented just numbers or, for instance, instructions to Spot, thus far? Any questions on binary, unary, or the like? No? All right, so seeing none, let's let things escalate a bit. So how do you represent letters because, obviously, this makes our devices more useful, whether it's in English or any other human language. How could we go about representing the letter A, for instance?
好的。到目前为止,对于我们如何表示数字或例如对 Spot 的指令,有什么问题吗?关于二进制、一元或类似的内容有问题吗?没有?好的,既然没有问题,那我们就让事情稍微升级一下。那么如何表示字母呢,因为显然这使我们的设备更有用,无论是英语还是其他任何人类语言。我们如何表示字母 A 呢?


If, at the end of the day, all our computers, all our phones have access to is electricity; or, equivalently, switches; or, metaphorically, tiny little light bulbs inside of them that can be on and off-- that's it. There's no more building blocks to give you. How could we represent something like the letter A? Yeah, how about here? Yeah?
如果到头来,我们所有的电脑、所有的手机所能接触的只是电;或者说,开关;或者比喻地说,它们里面有微小的灯泡可以开和关——就这样。没有更多的构建块可以提供给你。我们如何表示像字母 A 这样的东西?是的,那这里呢?是吗?


AUDIENCE: You could assign a number.
受众:您可以分配一个数字。


DAVID J. MALAN: Perfect. So we could just assign every letter a number. And we could do this super simply. Maybe 0 is A, and 1 is B. Or maybe 1 is A, and 2 is B. It doesn't really matter, so long as we all agree and we all use the same types of computers, in some sense, ultimately.
大卫·J·马兰:很好。所以我们可以给每个字母分配一个数字。我们可以非常简单地做到这一点。也许 0 是 A,1 是 B。或者也许 1 是 A,2 是 B。这并不重要,只要我们都达成一致,并且在某种意义上,我们都使用相同类型的计算机。


Well, for various reasons, the humans that designed this system, they went with the number 65. So, nowadays, anytime your computer is showing you the capital letter A on the screen, underneath the hood, so to speak, it's actually storing a pattern of 0's and 1's that represents the number 65. And it tends to use seven bits or, typically, eight bits, total, even if it doesn't need all of those bits in total.
好吧,由于各种原因,设计这个系统的人类选择了数字 65。因此,如今,每当你的计算机在屏幕上显示大写字母 A 时,实际上在背后,它存储的是一个代表数字 65 的 0 和 1 的模式。它通常使用七位或通常是八位,总的来说,即使并不需要所有这些位。


So how do we get there? Well, here, for instance, is that same pattern. Here is that pattern of bits that represents 65. And why? Well, quick check here. This is the ones place, twos, fours, eights, 16, 32, 64's place. OK, so 64 plus 1 gives me 65. So that is to say here's how a computer, using some light switches, so to speak, would represent the number 65. And our Macs, our PCs, our phones just all know this. So whenever they see that in their memory, so to speak, they show a capital letter A on the screen.
那么我们怎么到达那里呢?好吧,这里,例如,就是那个相同的模式。这里是表示 65 的位模式。为什么呢?好吧,快速检查一下。这是个位、十位、四位、八位、十六位、三十二位、六十四位。好的,64 加 1 等于 65。也就是说,这就是计算机如何用某种开关来表示数字 65。我们的 Mac、PC 和手机都知道这一点。因此,每当它们在内存中看到这个,所谓的,它们就在屏幕上显示一个大写字母 A。


So that's it. That's the system known as ASCII, the American Standard Code for Information Interchange. And the A is actually operative there because we're only talking, thus far, about English letters in our alphabet. And, in fact, I claimed, a moment ago, that we only use seven, maybe eight bits to represent letters of an alphabet. So, just to come back to you, if I may, how many possible letters of the alphabet could-- how many possible letters of any alphabet could we represent with eight bits?
所以就是这样。这就是被称为 ASCII 的系统,即美国信息交换标准代码。这里的 A 实际上是有效的,因为到目前为止,我们只在谈论我们字母表中的英语字母。事实上,我刚才声称,我们只使用七个,也许八个比特来表示字母表的字母。那么,回到你那儿,如果可以的话,我们可以用八个比特表示多少个字母表中的可能字母?


AUDIENCE: 256.  受众:256。

DAVID J. MALAN: 256, the numbers 0 through 255. Now, that's more than enough for English because we've got A through Z, uppercase, lowercase, a bunch of numbers, a bunch of punctuation symbols. But in a lot of languages, with accented characters, a lot of Asian characters, this is not nearly enough memory or bits with which to represent all of those possible values. So we need to do a little better than ASCII, but we can build on top of what they did years ago.
DAVID J. MALAN:256,数字 0 到 255。现在,这对于英语来说已经足够了,因为我们有大写字母 A 到 Z、小写字母、一些数字和一些标点符号。但是在很多语言中,带有重音的字符和许多亚洲字符,这远远不够内存或位数来表示所有可能的值。因此,我们需要比 ASCII 做得更好,但我们可以在他们多年前所做的基础上进行构建。


So here is a chart of ASCII codes. It's just a bunch of columns showing us the mapping between letters and numbers. So, for instance, up here is the capital letter A, 65; capital B, 66; capital C, 67; dot, dot, dot. 72 is H. 73 is I and so forth. There's some weird things over here, like special symbols, that we'll learn about over time. But there's a mapping between every English letter of the alphabet and some number, just as you'd propose, both for uppercase and lowercase.
这里是一个 ASCII 码的图表。它只是一些列,展示了字母和数字之间的映射。例如,顶部是大写字母 A,65;大写 B,66;大写 C,67;点,点,点。72 是 H。73 是 I,依此类推。这里有一些奇怪的东西,比如特殊符号,我们会随着时间的推移学习它们。但每个英文字母和某个数字之间都有映射,正如你所提到的,包括大写和小写字母。


So, for instance, if we highlight just a few of these for now and I say that I've just received a text message or an email that, underneath the hood, so to speak, if I have the ability to look at what switches are on and off, I received this message here. Well, first-- and this is not what CS is about, but just fun fact. Does anyone know what number this would represent in decimal, if this is the binary pattern, like ones place, twos place?
所以,例如,如果我们现在只突出其中几个,我说我刚收到了一条短信或电子邮件,从某种意义上说,如果我有能力查看哪些开关是打开的,哪些是关闭的,我在这里收到了这个消息。首先——这不是计算机科学的内容,但只是一个有趣的事实。有没有人知道如果这是二进制模式,像是个位、十位,这在十进制中代表什么数字?


AUDIENCE: 72?  受众:72?

DAVID J. MALAN: 72 is correct. And again, not, intellectually, all that interesting and this is not the kind of math that we spend all day as CS-- a computer scientist doing. But it's just following the same darn pattern, which is to say it might look cryptic, but, conceptually, intellectually, it ultimately is exactly as we did before. So, yes, I'll spoil the rest of the math. It's 72, 73, 33. Now, anyone remember, in your mind's eye, what message we just spelled?
大卫·J·马兰:72 是正确的。而且,再一次,从智力上来说,这并不是特别有趣,这也不是我们作为计算机科学家整天在做的那种数学。但这只是遵循同样的模式,也就是说,它可能看起来很神秘,但在概念上、智力上,它最终与我们之前所做的完全相同。所以,是的,我会剧透剩下的数学。是 72,73,33。现在,有人记得我们刚刚拼出的信息吗?


AUDIENCE: Hi.  观众:你好。

AUDIENCE: Hi.  观众:你好。

DAVID J. MALAN: Yeah, so it is, in fact, "Hi!" Though, no one really said that excitedly. What's the 33, if you noticed?
大卫·J·马兰:是的,实际上是“嗨!”不过,没有人真的兴奋地说过。你注意到 33 了吗?


AUDIENCE: Exclamation point.
受众:感叹号。


DAVID J. MALAN: OK, so a lot of people noticed. Yes, it's an exclamation point. And that's, indeed, noticeable right here. 33 is the exclamation point. And that's just something, eventually, that might sink in. But, for the most part, if you remember capital A is 65, you can figure out at least 25 other answers to these kinds of questions because they're all contiguous like that. So there's the exclamation point. But at the end of the day, we might just have this mapping known as ASCII. And it's how our phones, and computers, and devices, more generally, actually store information.
大卫·J·马兰:好的,很多人注意到了。是的,这是一个感叹号。确实,在这里很显眼。33 就是感叹号。这最终可能会让人领悟。但大多数情况下,如果你记住大写字母 A 是 65,你可以找出至少 25 个其他答案,因为它们都是连续的。所以这是感叹号。但归根结底,我们可能只有这个被称为 ASCII 的映射。这就是我们的手机、电脑和更一般的设备如何存储信息。


So we thought we'd make-- maybe take a little pressure off of me here. And could we maybe flip things around? How about we try applying this newfound knowledge-- if it's, indeed, new to you-- with seven volunteers, seven bits, if we could? OK. I saw your hand first. Come on down. Maybe your hand, there. OK, three, come on down over here. How about four and five? Yep, come on down. Yep, in the black shirt, yep. How about let me go farther back? How about in the green, over there? And how about you, seven, over here? All right. Come on down.
所以我们想我们可以——也许可以减轻我这边的一点压力。我们能不能换个方式?我们试着应用一下这新获得的知识——如果对你来说确实是新的——找七个志愿者,七个部分,可以吗?好的。我先看到你的手。来吧。也许你那只手。好的,三号,过来这边。四号和五号呢?是的,过来吧。对,穿黑衬衫的,没错。让我再往后走一步。那边穿绿色的呢?还有你,七号,过来这边?好的。来吧。


[CHATTER]  [聊天]

Come on down. So a round of applause for our brave volunteers.
来吧。为我们勇敢的志愿者们鼓掌。


[APPLAUSE]  [掌声]

All right. So if you'd like to stand, roughly, side by side, here in the middle of the stage. First of all, thank you. Let's see. 1, 2, 3, 4, 5, 6, 7, perfect. OK. And let's go all the way over to this place, here. If you would like to introduce yourself to the class.
好的。如果你们想大致并排站在舞台中间。首先,谢谢你们。我们来看看。1, 2, 3, 4, 5, 6, 7,完美。好的。现在我们走到这个地方。如果你们想向班级介绍自己。


RACHEL RICHEY: I'm Rachel Richey.
瑞秋·里奇:我是瑞秋·里奇。


DAVID J. MALAN: OK. And what year? Anything about you?
大卫·J·马兰:好的。那么是哪一年?关于你有什么?


RACHEL RICHEY: Oh, first year, concentrating in CS.
瑞秋·里奇:哦,第一年,专注于计算机科学。


DAVID J. MALAN: OK. Welcome to the stage. Next.
大卫·J·马兰:好的。欢迎上台。下一个。


SPEAKER: Hi. I'm [? Kang. ?] Also a first-year concentrating in CS.
发言人:嗨。我是[? Kang. ?] 也是一名专注于计算机科学的第一年学生。


SPEAKER: Hello. My name is [? Lam. ?] I'm a [INAUDIBLE] student from education department.
发言人:你好。我叫[? Lam ?]。我是一名来自教育部门的[听不清]学生。


DAVID J. MALAN: Nice. OK. Next.
大卫·J·马兰:很好。好的。下一个。


JORDAN MITTLER: Hi. I'm Jordan Mittler, concentrating in economics and maybe some CS.
乔丹·米特勒:嗨。我是乔丹·米特勒,专注于经济学,也许还有一些计算机科学。


SPEAKER: So, hi. I'm [? Natalia. ?] First year, and I want to do CS.
发言人:嗨。我是[? Natalia. ?] 大一,我想学计算机科学。


SPEAKER: Hi. I'm [? Khadija. ?] I'm a first-year, and I want to do CS.
发言人:嗨。我是[? Khadija。?] 我是一年级学生,我想学计算机科学。


DAVID J. MALAN: [LAUGHS].
大卫·J·马兰:[笑]。


SPEAKER: Hello. I'm [? Caleb. ?] And, once again, first year, concentrating in CS.
发言人:你好。我是[? Caleb. ?]。再次强调,我是一年级学生,专注于计算机科学。


DAVID J. MALAN: OK. Wonderful. A pattern, yes. Thank you. Thank you.
大卫·J·马兰:好的。太好了。一个模式,是的。谢谢。谢谢。


[APPLAUSE]  [掌声]

So, if you haven't guessed already, each of these volunteers is going to represent a bit, from left to right, or right to left, in this case. So let's see. If you want to represent-- how about the twos place? How about the fours place, the eighths place, 16ths place, 32's, 64, and 128? Although, wait a-- I think I screwed up. We needed one-- eighth volunteer. I think you know-- well, I think-- [CLEARS THROAT] Spot? OK. Come on over. If you guys could step forward a little bit, and then scooch this way, just to give Spot some room.
所以,如果你还没有猜到的话,这些志愿者每个人将代表一个比特,从左到右,或者在这种情况下,从右到左。那么我们来看看。如果你想表示——比如说二位数?四位数呢,八位数,十六位数,三十二位数,六十四位数和一百二十八位数?不过,等一下——我想我搞错了。我们需要一个——八分之一的志愿者。我想你们知道——好吧,我想——[清喉咙] Spot?好的。过来吧。如果你们能稍微向前一步,然后往这边挪一下,给 Spot 留点空间。


[FOOTSTEPS]  [脚步]

So Spot will represent the ones place. Now, what our volunteers have on the back of their sheets of paper are little instructions. We're going to spell out a three-letter word in English by using three bytes, from left to right, because now we have eight volunteers. I'm going to propose that you raise your hand, if you're supposed to represent a 1. Or you just stand there, without raising your hand, if you're meant to represent a 0.
所以 Spot 将代表个位。现在,我们的志愿者在纸张的背面有一些小指示。我们将通过从左到右使用三个字节拼出一个三字母的英文单词,因为现在我们有八个志愿者。如果你应该代表 1,请举手。或者如果你应该代表 0,就站在那里,不举手。


And what we'll have the audience do is do the quick math to figure out-- one, two, three-- each letter, what number is it. What letter is it? And we'll see what word that we have finally spelled. All right. So, in round one-- you have instructions on your back of your sheet that will tell you to-- what your number is. If you're 0, stand there. If you're a 1, raise your hand.
我们会让观众快速计算一下——一、二、三——每个字母对应的数字是什么。它是什么字母?我们将看看最终拼出的单词是什么。好的。在第一轮——你们的纸背面有说明,告诉你们你的数字是什么。如果你是 0,就站在那里。如果你是 1,举手。


[PAPER RUSTLING]  [纸张沙沙声]

What number do these guys seem to be representing?
这些家伙似乎代表什么数字?


AUDIENCE: 68.  受众:68。

DAVID J. MALAN: 66, I think. 64 plus 2, so 66, which is the letter--
大卫·J·马兰:66,我想。64 加 2,所以是 66,这是字母——


AUDIENCE: B.  受众:B。

DAVID J. MALAN: OK, so, B. OK, so, B. All right. Hands down. Second letter is going to be spelled how?
大卫·J·马兰:好的,那么,B。好的,那么,B。好吧。手放下。第二个字母将如何拼写?


[SPOT LANDS ON THE STAGE]
[SPOT 登上舞台]


AUDIENCE: Whoa.  观众:哇。

AUDIENCE: Whoa.  观众:哇。

AUDIENCE: Whoa.  观众:哇。

DAVID J. MALAN: [LAUGHS] All right. What are we spelling now?
大卫·J·马兰:[笑] 好吧。我们现在要拼什么?


[INDISTINCT CHATTER]  [模糊的交谈]

I think-- psst, yep, OK. Yeah, I think you're one.
我想——嘘,是的,好的。是的,我觉得你是一个。


[LAUGHTER]  [笑声]

OK. Now what number are we spelling?
好的。现在我们要拼写哪个数字?


AUDIENCE: 79  受众:79

DAVID J. MALAN: 79, I heard, which is the letter?
大卫·J·马兰:79,我听说,哪个是字母?


AUDIENCE: O.  受众:O。

DAVID J. MALAN: O. OK. So hands down. Thank you, Spot. One final flourish. So we've spelled B-O-- third letter, go ahead.
大卫·J·马兰:好的。谢谢你,Spot。最后一个华丽的动作。我们拼写 B-O--第三个字母,继续。


[SPOT LANDS ON THE STAGE]
[SPOT 登上舞台]


What number, now, is this?
现在,这个数字是什么?


AUDIENCE: 87.  受众:87。

AUDIENCE: 87.  受众:87。

DAVID J. MALAN: I heard it here, 80--
大卫·J·马兰:我在这里听到的,80--


AUDIENCE: Seven.  受众:七。

DAVID J. MALAN: --seven, which is?
大卫·J·马兰:--七,是什么?


AUDIENCE: W.  受众:W。

DAVID J. MALAN: W, which, of course, spells "bow." So if our volunteers could take a bow, Spot included.
大卫·J·马兰:W,这当然拼写为“鞠躬”。所以如果我们的志愿者可以鞠躬,Spot 也包括在内。


[APPLAUSE]  [掌声]

So this will make more sense in week one, when we have an assignment involving a certain someone from the Nintendo World. But we have a lovely parting gift for each of you.
所以在第一周,这会更有意义,当我们有一个涉及任天堂世界某个特定人物的作业时。但我们为每个人准备了一份可爱的告别礼物。


SPEAKER: Thank you. [LAUGHS]
发言人:谢谢。[笑]


DAVID J. MALAN: Thank you for volunteering.
大卫·J·马兰:感谢您的志愿服务。


SPEAKER: Thanks.  发言人:谢谢。

DAVID J. MALAN: You might need to share it with the folks next to you.
大卫·J·马兰:你可能需要与旁边的人分享它。


[CHATTER]  [聊天]

Oop, here we go. There we go. Thank you--
哎呀,来了。好了。谢谢——


SPEAKER: Thank you.  发言人:谢谢。

RACHEL RICHEY: Thank you.
瑞秋·里奇:谢谢。


DAVID J. MALAN: --so much. One more round of applause, if we could, for our volunteers. Thank you.
大卫·J·马兰:--非常感谢。如果可以的话,请再为我们的志愿者鼓掌一次。谢谢。


[APPLAUSE]  [掌声]

Did you lose something? OK. All right. So, [LAUGHS] Spot's had it. So let's see, then, if we've solved, now, the problem of representing English letters of the alphabet, being able to spell out words like "bow," B-O-W. What if we actually do have accented characters? What if we do have other glyphs that we want to represent? Well, here, of course, is a standard US English keyboard, which a lot of you might have. But there's also characters that you can type much more easily if you have a foreign keyboard, relative to the US, or with certain keystrokes on your own Mac, PC, and phone.
你丢了什么吗?好的。没问题。那么,[笑] Spot 已经解决了。那么,让我们看看,我们是否已经解决了表示英文字母的问题,能够拼出像“bow”这样的单词,B-O-W。如果我们实际上有重音字符呢?如果我们有其他我们想要表示的字形呢?当然,这里是一个标准的美国英语键盘,你们很多人可能都有。但是,如果你有一个外文键盘,相对于美国,或者在你自己的 Mac、PC 和手机上使用某些按键组合,你可以更轻松地输入字符。


But, nowadays, too, there's this menu that, probably, you've used in the past hour or two to actually send some emoji. An emoji, even though they look like pictures and they actually are pictures on the screen, they're, technically, just characters, characters of an emoji alphabet that happened to use a certain pattern of 0's and 1's to represent each of these faces, each of these people, and places, and things.
但如今,这个菜单可能是你在过去一两个小时内用来发送一些表情符号的。表情符号虽然看起来像图片,实际上它们在屏幕上确实是图片,但从技术上讲,它们只是字符,是一种表情符号字母表的字符,恰好使用某种 0 和 1 的模式来表示这些面孔、这些人、地方和事物。


And it turns out that one of the reasons that we have just so many [LAUGHS] such characters nowadays is because we now use Unicode instead of ASCII. So Unicode is a superset, so to speak, of ASCII, which is to say that we, humans, realized, some time ago, that just using eight bits to represent letters of the alphabet certainly isn't very good when we want to represent other, non-English languages. So Unicode doesn't just use eight bits. It sometimes uses 16 bits per character, sometimes 24 bits per character, and sometimes even 32 bits per character.
结果发现,我们现在有这么多[笑]这样的字符的原因之一是因为我们现在使用 Unicode 而不是 ASCII。因此,Unicode 可以说是 ASCII 的超集,也就是说,我们人类早在某个时候就意识到,仅仅使用八位来表示字母表的字母在我们想要表示其他非英语语言时显然不是很好。因此,Unicode 不仅仅使用八位。它有时每个字符使用 16 位,有时每个字符使用 24 位,有时甚至每个字符使用 32 位。


Now, why those numbers? That's just one byte, two bytes, three bytes, or four bytes. And that gives us-- does anyone know? That gives us the ability to represent as many as 4 billion possible characters. Because if the longest one is 32 bits, that's 2 to the 32, which, if you do out the math, trust me, is roughly 4 billion. So that's a lot of characters. And we've got a lot of room, then, for these emoji.
现在,为什么是这些数字?这只是一个字节、两个字节、三个字节或四个字节。这给了我们——有没有人知道?这使我们能够表示多达 40 亿个可能的字符。因为如果最长的是 32 位,那就是 2 的 32 次方,计算一下,信我,大约是 40 亿。所以这有很多字符。而且我们还有很多空间来放这些表情符号。


But it's not just about having fun, pictorially, on the screen. Unicode's mission really is to represent and to preserve all human languages digitally, both past, present, and future. So it is really about capturing the entirety of human knowledge, as we've expressed it in language, but also giving this newfound ability that's been used centuries ago, too-- in writings, on walls, and the like-- pictograms via which we can still communicate, even independently of our own human language.
但这不仅仅是关于在屏幕上图像化地玩乐。Unicode 的使命确实是以数字方式表示和保存所有人类语言,无论是过去、现在还是未来。因此,这实际上是关于捕捉人类知识的全部,正如我们在语言中所表达的那样,同时也赋予这种新发现的能力,这种能力在几个世纪前也曾被使用——在书写、墙壁等地方——通过象形文字,我们仍然可以进行交流,甚至独立于我们自己的语言。


So we'll reduce it, today, to just patterns of 0's and 1's, but the problem being solved is much greater and well-beyond CS, itself, there. So here is a pattern of 0's and 1's using Unicode, so more than eight bits, that represents a very popular emoji, which might be a bit of a hint. This is the most popular emoji, as of last year, at least, statistically, internationally.
所以我们今天将其简化为仅仅是 0 和 1 的模式,但所解决的问题远远超出了计算机科学本身。因此,这里有一个使用 Unicode 的 0 和 1 的模式,超过八位,代表一个非常流行的表情符号,这可能是一个小提示。根据统计,至少截至去年,这是最受欢迎的表情符号,国际上也是如此。


[INTERPOSING VOICES]  [插入声音]

DAVID J. MALAN: Does this help? It's, roughly, this number here. No? It's this one here. So this is the most popular emoji, by most measures, as of last year. But it doesn't always look like this. Those of you who have a Mac or an iPhone recognize this symbol, perhaps, immediately. Those of you with Android devices or other platforms might notice that it's the same idea, but it's a little bit different. And this is because, too, emojis, at the end of the day, just represent character, but those characters can be drawn, can be painted in different ways.
DAVID J. MALAN:这有帮助吗?大致上,就是这个数字。不是吗?是这个。所以这是截至去年为止,最受欢迎的表情符号,按大多数标准来看。但它并不总是看起来像这样。拥有 Mac 或 iPhone 的你们可能会立即认出这个符号。拥有 Android 设备或其他平台的你们可能会注意到这是同样的概念,但稍微有些不同。这也是因为,表情符号归根结底只是代表字符,但这些字符可以以不同的方式绘制或呈现。


And reasonable people will interpret differently this emoji, whose official name is "face with tears of joy." And, indeed, Google interprets it a little differently from Apple, versus Microsoft, versus Meta, versus other companies, as well. So you can almost think of those different companies as having different fonts for emoji. And that really starts to connect things to the world of text and characters. So, just so you've seen it. More on this, another time.
合理的人会对这个表情符号有不同的解读,它的官方名称是“流泪的笑脸”。实际上,谷歌对它的解读与苹果、微软、Meta 以及其他公司略有不同。因此,你几乎可以认为这些不同的公司对表情符号有不同的字体。这确实开始将事物与文本和字符的世界联系起来。所以,给你看看。关于这个,改天再说。


It turns out that emoji and, really, characters, in general, we don't use binary 0's and 1's to represent them because no one, myself included, is going to recognize what's what. It's just too much math. It's not interesting. And even decimal numbers-- that was 4 billion or some-- I don't remember which number is which. So we represent things a little more compactly. And this, too, admittedly, still looks cryptic, but this is a Unicode code point that uses another system, mathematically, called base-16 or hexadecimal. More on that, another time.
事实证明,表情符号以及一般字符,我们并不使用二进制的 0 和 1 来表示它们,因为没有人,包括我自己,能够识别出它们是什么。这实在是太复杂了。这并不有趣。即使是十进制数字——那是 40 亿还是其他的——我也不记得哪个数字是什么。因此,我们以更紧凑的方式来表示事物。诚然,这看起来仍然很神秘,但这是一个使用另一种数学系统的 Unicode 代码点,称为基数-16 或十六进制。关于这一点,改天再说。


But it's just a way of representing numbers even more succinctly, writing less on the screen, because you're using not just 0 through 9, as in decimal. But you're using A through F, as well, so a few letters of the English alphabet come into play. But, for now, that's just a little easier to remember, too, for people who care, that that is the number that represents "face with tears of joy."
但这只是以更简洁的方式表示数字,减少屏幕上的文字,因为你不仅使用 0 到 9,如同十进制。你还使用 A 到 F,因此几个英文字母也参与其中。不过,目前对于在意的人来说,这也更容易记住,因为这就是代表“喜极而泣”的数字。


But what if we want a customized emoji? And this, increasingly, is the case. Here, for instance, are the five skin tones that phones, and laptops, and desktops, nowadays, support. It's based on something called the "Fitzpatrick scale," which essentially categorizes human skin tone into six or, in this case, five different categories, from lighter to darker. But this suggests that, wow, if we want to represent people with five different skin tones, like this, that could significantly increase how many unique patterns of 0's and 1's we need for every possible face.
但如果我们想要一个自定义的表情符号呢?而这,越来越多地成为现实。例如,以下是手机、笔记本电脑和台式机如今支持的五种肤色。这是基于一种叫做“菲茨帕特里克量表”的东西,它基本上将人类肤色分为六种,或者在这种情况下,五种不同的类别,从浅到深。但这表明,哇,如果我们想要代表五种不同肤色的人,比如这样,那可能会显著增加我们为每个可能的面孔所需的独特 0 和 1 的模式数量。


But if we think about it from an engineering perspective, we can actually just think of skin tone as modifying some default color, for better or for worse. And yellow is the de facto default, Simpson style. But to modify it to look more genuinely human-like, from lighter to darker, well, maybe we just use the same pattern of bits to represent a human thumb, for instance, thumbs up or thumbs down. And we just, then, modify that character to be displayed with a different skin tone.
但如果从工程的角度考虑,我们实际上可以将肤色视为修改某种默认颜色,无论好坏。而黄色是事实上的默认色,类似辛普森风格。但要使其看起来更像人类,从浅色到深色,或许我们可以使用相同的位模式来表示人类的拇指,例如,竖起大拇指或竖下大拇指。然后,我们只需修改该字符以显示不同的肤色。


So, for instance, here, then, is the "thumbs up" that you might use on various platforms. And let me just stipulate that this is the Unicode code point. That is the number that Macs, PCs, and phones use underneath the hood to represent the default yellow "thumbs up." But if you want to give it a medium skin tone, you still use that same number, that same pattern of 0's and 1's, or switches, underneath the hood. But you use a few more switches that the computer or phone will interpret as, "Oh, you don't want to see the default in yellow because of this second number that's in the computer's memory somewhere. You want me to adjust it to be the medium skin tone or any of the other values, instead."
所以,例如,这里就是你可能在各种平台上使用的“点赞”。让我先说明一下,这是 Unicode 代码点。这个数字是 Mac、PC 和手机在后台用来表示默认的黄色“点赞”。但是如果你想给它一个中等肤色,你仍然使用那个相同的数字,相同的 0 和 1 的模式,或者开关,在后台。但是你会使用更多的开关,计算机或手机会将其解释为:“哦,你不想看到默认的黄色,因为计算机内存中的这个第二个数字。你希望我调整为中等肤色或其他任何值。”


So that's the engineering solution to this problem of just trying to represent different ranges of emoji here. Well, what about something like this? There's a lot more combinatorics, nowadays, on your keyboard for relationships, for instance. So here is a "couple with heart" here. So the couple, here, of course, is represented with, apparently, this number here. But that's it. But if you want to be more specific-- like man and woman, or man-man, woman-woman-- it's the same idea, but we just need to express ourselves a little-- with a little more information.
所以这就是解决这个问题的工程方案,试图在这里表示不同范围的表情符号。那么像这样的情况呢?现在你的键盘上有更多的组合,比如关系。例如,这里有一个“带心的情侣”。所以这里的情侣,显然是用这个数字来表示的。但就是这样。如果你想更具体一点——比如男和女,或者男男、女女——想法是一样的,但我们只需要稍微多表达一些信息。


So, for instance, the way the Unicode folks came up with, years ago, to represent, for instance, a woman with a heart and a man, from left to right, would be using these values. So things just escalated quickly, but only in the sense that we're using more bits, more 0's and 1's, to represent, more expressively, this particular combination. So this happens to be the number in Unicode that represents the woman at left. This is the number that represents the man at right. And this is the pair of numbers that represents the heart in the middle, sometimes red, sometimes pink, displayed here as pink.
所以,例如,Unicode 的开发者多年前提出的表示方式,左边是一个女人,右边是一个男人,使用这些值。因此,事情迅速升级,但仅仅是因为我们使用了更多的位,更多的 0 和 1,以更具表现力的方式表示这个特定的组合。所以,这个数字在 Unicode 中表示左边的女人。这个数字表示右边的男人。这个数字对表示中间的心,通常是红色,有时是粉色,这里显示为粉色。


But if we want to change the combination, for instance, to be, say, woman-- if we want to change the combination to be woman-woman, notice that, now, the left and the rightmost numbers match. Or if we flip it back to man-man, it's just using different numbers on the tail end again. And meanwhile, if I rewind, there's these two identical values here. These are called zero-width joiners or ZWNJ characters. It just is a special number that humans reserve to say, glue the emoji at the left to the emoji on the right and so forth. So it connects ideas in this way.
但是如果我们想要改变组合,比如说,变成女性——如果我们想要将组合改为女性-女性,请注意,现在最左边和最右边的数字匹配。或者如果我们再切换回男性-男性,这只是再次在尾部使用不同的数字。同时,如果我倒回去,这里有两个相同的值。这被称为零宽连接符或 ZWNJ 字符。它只是一个特殊的数字,人类保留它来表示将左侧的表情符号与右侧的表情符号粘合在一起,等等。因此,它以这种方式连接思想。


So there's actually a lot of emojis, nowadays, that are a combination of different things. "Heart on fire" is one that's, technically, the combination of a heart emoji, the fire emoji, joined together, numerically, in this way. So computer scientists who come up with these things are just reducing things to representations. All we have at our disposal are 0's and 1's. So we all just need to agree, ultimately-- whether we're Google, Microsoft, or the like-- how we're going to standardize these kinds of things as information.
所以现在实际上有很多表情符号,它们是不同事物的组合。“燃烧的心”就是一个,它在技术上是心形表情符号和火焰表情符号的组合,以这种方式数字化地结合在一起。因此,想出这些东西的计算机科学家只是将事物简化为表示。我们所拥有的只是 0 和 1。因此,我们最终都需要达成一致——无论我们是谷歌、微软还是类似的公司——我们将如何将这些信息标准化。


Questions, then, on how characters are represented in a computer, be it English or any other language? Yeah.
问题是,字符在计算机中是如何表示的,无论是英语还是其他任何语言?是的。


AUDIENCE: How is the plus a number?
受众:加号是如何表示数字的?


DAVID J. MALAN: How is the what?
大卫·J·马兰:那是什么?


AUDIENCE: The plus, the U+.
受众:加号,U+。


DAVID J. MALAN: Oh, the U+ is just a convention, really. So U+ represents a special Unicode character, which is a U with a plus in the middle. And this is just the convention for saying, hey, everyone, here comes a number that represents a Unicode code point. The U and the 1 have no-- sorry. The U and the plus have no mathematical significance. It's just a visual clue to folks. Other questions on representing text in this way?
大卫·J·马兰:哦,U+ 只是一个约定而已。所以 U+ 代表一个特殊的 Unicode 字符,它是一个中间带加号的 U。这只是一个约定,用来告诉大家,嘿,来了一个表示 Unicode 码点的数字。U 和 1 没有——抱歉。U 和加号没有数学意义。这只是给大家的一个视觉提示。还有其他关于以这种方式表示文本的问题吗?


All right. So what about colors? We've already started looking at pictures. Well, how are those pictures, be it emojis or anything else, represented? One of the most common ways is just with RGB-- red, green, and blue. It turns out that if we just keep track of how much red should be on the screen, and how much green, and how much blue, combined together, that gives us every color of the rainbow, from white to black and everything in between.
好的。那么颜色呢?我们已经开始查看图片了。那么这些图片,无论是表情符号还是其他任何东西,是如何表示的呢?最常见的方法之一就是使用 RGB——红色、绿色和蓝色。事实证明,如果我们只跟踪屏幕上应该有多少红色、多少绿色和多少蓝色,结合在一起,就能得到彩虹的每一种颜色,从白色到黑色以及其中的所有颜色。


So how do we represent an amount of red, and green, and blue? Well, frankly, just with three different numbers. And this is how computers typically represent colors. Every one of the dots on your computer screen or your phone screen is called a pixel. And every single dot underneath the hood has three numbers associated with it, so three numbers, three numbers, three numbers for every little dot. And those three numbers, together, say how much red, green, and blue should the device display at that location.
那么我们如何表示红色、绿色和蓝色的数量呢?坦率地说,就是用三个不同的数字。这就是计算机通常表示颜色的方式。你电脑屏幕或手机屏幕上的每一个点称为像素。每一个点在内部都有三个数字与之相关,因此每一个小点都有三个数字。这三个数字一起表示设备在该位置应该显示多少红色、绿色和蓝色。


So, for instance, if you had a dot on your screen that said, "use this much red, this much green this much blue," because each of these numbers, I'll tell you, are one byte or eight bits, which means the total possible values is 0 to 255-- let me just ballpark that the 72, it feels like a medium amount of red because it's in between 0 and 255. 73 is a medium amount of green, and 33 of blue is just a little bit.
所以,例如,如果你的屏幕上有一个点,上面写着“使用这么多红色,这么多绿色,这么多蓝色”,因为这些数字,我告诉你,都是一个字节或八位,这意味着总的可能值是 0 到 255——让我大致估计一下,72 感觉像是中等量的红色,因为它在 0 和 255 之间。73 是中等量的绿色,而 33 的蓝色则只是稍微一点。


So if you combine a medium amount of red, green, and a little bit of blue, anyone want to guess what color of the rainbow this is?
所以如果你结合适量的红色、绿色和一点蓝色,谁想猜猜这是什么颜色的彩虹?


AUDIENCE: Brown.  受众:棕色。

DAVID J. MALAN: Sorry?
大卫·J·马兰:抱歉?


AUDIENCE: Brown.  受众:棕色。

DAVID J. MALAN: Brown? So, close. It's a little more yellow than it is brown. But if we combine them, it looks a little something like this. This is just CS trivia, not something that even I would be able to eyeball, unless I came up with that particular example. But wait a minute. We've seen these numbers before. 72, 73, 33 represented what--
大卫·J·马兰:棕色?差不多。它比棕色稍微多一点黄色。但如果我们把它们结合起来,它看起来有点像这样。这只是计算机科学的琐事,甚至我也无法凭眼睛判断,除非我想到了这个特定的例子。但等一下。我们之前见过这些数字。72、73、33 代表什么——


AUDIENCE: Hi!  观众:嗨!

DAVID J. MALAN: --a few minutes ago? So it meant "Hi!" but here I am, claiming, no, no, no, no, that means yellow. How do you reconcile this? Well, at the end of the day, this is all we have, 0's and 1's, whether you think of them as numbers, or letters, or even colors now. But it depends on the context. So if you've received a text message or an email, odds are the pattern of 0's and 1's that the computer is showing you are going to be interpreted as text because that's the whole point of a text message or an email.
大卫·J·马兰:--几分钟前?所以这意味着“嗨!”但我在这里声称,不,不,不,这意味着黄色。你怎么调和这个?好吧,归根结底,这就是我们所拥有的,0 和 1,无论你把它们看作数字、字母,甚至现在的颜色。但这取决于上下文。所以如果你收到了短信或电子邮件,计算机向你显示的 0 和 1 的模式很可能会被解释为文本,因为这就是短信或电子邮件的全部意义。


If, though, you opened up MacOS's or iOS's or Windows's or Android's calculator app, the same pattern of 0's and 1's might be interpreted as numbers for some addition, or subtraction, or whatever. If you open the same pattern of 0's and 1's in Photoshop, like a graphics program, they're going to be interpreted, in that context, as colors. So context matters.
如果你打开 MacOS、iOS、Windows 或 Android 的计算器应用程序,那么相同的 0 和 1 的模式可能会被解释为某些加法、减法或其他运算中的数字。如果你在 Photoshop 这样的图形程序中打开相同的 0 和 1 的模式,它们将在那个上下文中被解释为颜色。因此,上下文很重要。


And, indeed, as soon as next week, when you start writing code in that language called C, the onus will be on you, the programmer, to tell the computer, interpret the following sequence of bits as a number, or a letter, or something else. And you won't have to even worry about what the 0's and 1's are, but you need to give the computer a hint as to what type of file or piece of data you're representing.
而且,实际上,等到下周,当你开始用一种叫做 C 的语言编写代码时,责任将落在你这个程序员身上,告诉计算机将以下位序列解释为一个数字、一个字母或其他东西。你甚至不需要担心 0 和 1 是什么,但你需要给计算机一个提示,说明你所表示的文件或数据的类型。


So that gives us bits. And you can actually see these dots, these pixels on the screen. Let me zoom in, zoom in. And here we have it, just with this emoji, which, at the end of the day, is a picture that someone at Apple, in this case, drew. And you can see-- if you really zoom in, or take your phone or TV and really put it close to your face, you'll see all of these dots, depending on the hardware. And each of these dots, these squares, is storing 24 bits or three bytes, 24 bits, 24 bits, 24 bits.
所以这给了我们比特。你实际上可以在屏幕上看到这些点,这些像素。让我放大,放大。这里我们有它,仅仅用这个表情符号,归根结底,这是某个苹果员工画的图。你可以看到——如果你真的放大,或者拿你的手机或电视靠近你的脸,你会看到所有这些点,具体取决于硬件。每一个点,这些方块,存储着 24 个比特或三个字节,24 个比特,24 个比特,24 个比特。


And that's whey, dot, dot, dot, if you've got a photograph, for instance, that's three megabytes, which is 3 million bytes, well, odds are there's 1 million pixels therein because you're using three bytes per pixel to represent each of those colors. That's a bit of an oversimplification, but that's why images and photos are getting bigger and bigger nowadays. Because we're throwing even more pixels into the file. Music--
而这就是为什么,如果你有一张例如三兆字节的照片,也就是 300 万字节,那么其中大约有 100 万个像素,因为你使用每个像素三字节来表示每种颜色。这有点过于简化,但这就是为什么如今图像和照片越来越大的原因。因为我们在文件中加入了更多的像素。音乐——


[MUSIC PLAYING]  [音乐播放]

--how could you represent music, digitally, using just 0's and 1's, or numbers, really? Any instinct, whether a musician or not? Yeah.
--你如何用数字 0 和 1,或者说数字,来数字化地表示音乐呢?你有什么直觉,无论你是音乐家还是不是?是的。


AUDIENCE: The notes could be represented by a number.
受众:这些笔记可以用一个数字表示。


DAVID J. MALAN: Yeah, so we can just represent notes by a number. So A is some number, and B. And maybe sharp or flat is some other number. But note might not be quite enough for some-- yeah?
大卫·J·马兰:是的,我们可以用一个数字来表示音符。所以 A 是某个数字,B 也是。也许升音或降音是另一个数字。但音符可能对某些人来说不够——是吗?


AUDIENCE: [INAUDIBLE].  观众:[听不清].

DAVID J. MALAN: Ah, OK. So one note-- one number to represent the note, itself, the sound or the pitch; one other number to represent the duration. In the context of piano, how long is the human holding the key down? And maybe I can think of a third, the loudness. How hard has the person played that note? So, minimally, with three numbers, you could imagine representing music, as well. And, indeed, that's very well might be what computers are doing when you listen to sound. What about video? How could you represent videos, as well? Yeah?
大卫·J·马兰:啊,好吧。那么一个音符——一个数字来表示音符本身,声音或音高;另一个数字来表示持续时间。在钢琴的上下文中,人类按住琴键的时间有多长?也许我可以考虑第三个,音量。这个人弹奏这个音符的力度有多大?因此,最少用三个数字,你也可以想象表示音乐。实际上,这很可能就是计算机在你听声音时所做的。那么视频呢?你如何表示视频呢?是吗?


AUDIENCE: Through many images.
受众:通过许多图像。


DAVID J. MALAN: Yeah, many images. So if you've ever produced a film or looked at some of the fine print, 30 frames per second, FPS, or 29 frames per second is just how many pictures are flying across the screen. But that's really all a video file is on a computer, lots of pictures moving so quickly in front of us that you and I, our brains, interpolate that as being actual motion. And, in fact, from yesteryear, motion pictures, it's like pictures that are giving the illusion of motion, even though there's only 30 or so of them flying across the screen.
大卫·J·马兰:是的,很多图像。所以如果你曾经制作过电影或查看过一些细节,30 帧每秒(FPS)或 29 帧每秒就是屏幕上飞过的图像数量。但这实际上就是计算机上的视频文件,很多图像快速移动在我们面前,以至于你我大脑将其插值为实际的运动。事实上,从过去的电影来看,这就像是给人运动错觉的图像,即使屏幕上只有大约 30 帧图像在飞过。


So we have a way, now, to represent information, both as input and output, whether it's numbers, letters, images, anything else. Let's now focus on what's inside of that black box, so to speak, wherein we have algorithms, step-by-step instructions for solving some problem. Now, what do I mean by "algorithms" or "step-by-step instructions"? Well, maybe, if we were to turn this into code-- and that's how we'll connect the dots, ultimately, today. Code is just the implementation, in computers, of algorithms.
所以我们现在有了一种方式来表示信息,无论是输入还是输出,无论是数字、字母、图像还是其他任何东西。现在,让我们关注一下那个黑箱子里面的东西,可以说是我们有算法,逐步解决某个问题的指令。那么,我所说的“算法”或“逐步指令”是什么意思呢?好吧,也许如果我们把它变成代码——这就是我们今天最终连接点的方式。代码只是算法在计算机中的实现。


An algorithm can be something we do in the physical world. Code is how we implement that exact same idea, in the context of a computer, instead. And here, for instance, is a very common application inside of a computer, for your context. This is the iOS version of the icon. And, typically, if you click on that icon, you'll see something like all of your contacts here, typically, alphabetical, by first name or last name. And your phone or your computer lets you often search for someone's name at the very top. And it will autocomplete, and it'll be pretty darn fast.
算法可以是我们在物理世界中做的事情。代码是我们在计算机环境中实现相同想法的方式。在这里,例如,这是计算机内部一个非常常见的应用程序。这是图标的 iOS 版本。通常,如果你点击那个图标,你会看到所有的联系人,通常按名字或姓氏的字母顺序排列。你的手机或计算机通常允许你在最顶部搜索某个人的名字。它会自动补全,并且速度非常快。


But it'll be pretty darn fast because the programmers who implemented that application are looking for someone quickly for you. Now, I can do this old school style, whereby we have one of these things from yesteryear, an actual phone book. So, in a physical phone book like this, you might have 1,000 pages. And on every page are a bunch of names and a bunch of numbers. And as I flip through this, I could look for someone specific.
但这会非常快,因为实现该应用程序的程序员正在快速为您寻找某人。现在,我可以用老派的方式来做这件事,我们有一个来自往昔的东西,实际的电话簿。因此,在这样的实体电话簿中,您可能会有 1000 页。每一页上都有一堆名字和一堆号码。当我翻阅时,我可以寻找特定的人。


So suppose I want to call John Harvard, who's-- the first name, of course, starts with a J. Well, I could just turn, page by page, looking for John Harvard. And if he's not there, I keep turning and turning. So this is an algorithm. I'm stepping through the phone book, one page at a time. Is it correct, this algorithm, assuming I'm looking down? So, yeah. I mean, it's stupidly slow because why am I wasting my time with the A's, and the B's, and the so forth? I could probably take bigger bites out of it. But it is correct.
假设我想找到约翰·哈佛,他的名字当然是以 J 开头的。我可以逐页翻阅,寻找约翰·哈佛。如果他不在那儿,我就继续翻页。这是一个算法。我在电话簿中逐页查找。这个算法是正确的吗,假设我在向下看?是的。我的意思是,这个方法非常慢,因为我为什么要浪费时间在 A、B 等字母上?我可能可以更有效率地处理它。但它是正确的。


And that's going to be one of the goals of writing code, is to, of course, solve the problem you care about correctly. So correctness goes without saying, or else what's the point of writing the code or solving-- or implementing the algorithm? All right. Well, let me at least speed things up. So, instead of one page at a time, so, two, four, six, eight-- no, ignore that-- 10, 12, 14, 16, and so forth. It's a little hard to do, physically, but it sounded faster. It was twice as fast, theoretically, but is it correct?
编写代码的目标之一当然是正确地解决你关心的问题。因此,正确性是不言而喻的,否则编写代码或实现算法有什么意义呢?好吧,让我至少加快速度。所以,不是一页一页地来,而是两页、四页、六页、八页——不,忽略这一点——十页、十二页、十四页、十六页,等等。这样做在物理上有点困难,但听起来更快。从理论上讲,这样是快了一倍,但它正确吗?


AUDIENCE: No.  受众:否。

DAVID J. MALAN: So, no. Why? Yeah?
大卫·J·马兰:所以,不。为什么?嗯?


AUDIENCE: Yeah, you might [INAUDIBLE].
观众:是的,你可能[听不清]。


DAVID J. MALAN: Yeah, I might miss John Harvard because, just by accident, he might get sandwiched between two pages. But do I have to throw the algorithm out altogether? Probably not. Once I reach the K section, which is past the J section, I could double back at least one page, at least, [PATS TELEPHONE BOOK] and just make sure I didn't blow past him completely.
大卫·J·马兰:是的,我可能会错过约翰·哈佛,因为他可能恰好夹在两页之间。但我是否必须完全抛弃这个算法?可能不需要。一旦我到达 K 部分,也就是在 J 部分之后,我可以至少回头翻一页,至少,[拍打电话簿],确保我没有完全错过他。


So that is twice as fast, because I'm going two pages at a time, plus one extra step. So it's still an improvement. So the first algorithm, worst case, if it's not John, but someone whose name starts with Z, that might take me a full 1,000 steps. The second algorithm is just 500 steps because I'm going two pages at a time plus one, in case I have to double back, but that's in the worst case.
所以这速度是之前的两倍,因为我每次翻两页,再加一步。所以这仍然是一个改进。第一个算法,最坏情况下,如果不是约翰,而是一个名字以 Z 开头的人,那可能需要我走满 1000 步。第二个算法只需要 500 步,因为我每次翻两页再加一步,以防我需要回头,但这也是在最坏情况下。


But most of us in the-- in yesteryear, and what Apple, and Google, and others are actually doing is, in software or here, physically, we're typically going, roughly, to the middle. Especially if there's no cheat sheet on the side, like A through Z, I'm just going to go to, roughly, the middle. And, oh, here I am, not surprisingly, in the M section. But what do I now know. If this is the M section, where is John Harvard? So, clearly, to the left, alphabetically.
但我们大多数人在过去,苹果、谷歌和其他公司实际上在做的事情是,在软件或这里,实际上我们通常会大致走到中间。特别是如果旁边没有备忘单,比如从 A 到 Z,我就会大致走到中间。哦,我在这里,不出所料,在 M 部分。但我现在知道什么。如果这是 M 部分,约翰·哈佛在哪里?所以,显然,在字母表的左边。


And so here is where we can take a much bigger bite out of the problem. We can really divide and conquer this problem by tearing [TEARS BOOK] the problem in half, throwing half of it away, 500 pages away, leaving me with a smaller problem, half as big, that I can really just now repeat. So I go, roughly, here, and now I'm in the E section. So I went a little too far back. But what do I now know? If this is the E pages, where's John?
所以在这里我们可以更大程度地解决这个问题。我们可以通过将问题一分为二来真正地分而治之,把一半的问题扔掉,500 页扔掉,留下一个更小的问题,只有一半大,我现在可以重复这个过程。所以我大致走到这里,现在我在 E 部分。所以我回溯得有点太远了。但我现在知道什么?如果这是 E 页,约翰在哪里?


AUDIENCE: To the right.
受众:向右。


DAVID J. MALAN: So now he's to the right. So I can-- again, hopefully, he's not on that page. I can tear the problem in half again, throw that 250 pages away. And now I've gone from 1,000 to 500 to 250 pages. Now I'm moving because the first algorithm was one page at a time, second was two. This is hundreds of pages at a time. And if I go, roughly, again, to the middle; roughly, to the middle; roughly, to the middle, hopefully, I'll find John Harvard on one final page. Can only do this once, but one final page.
大卫·J·马兰:所以现在他在右边。所以我可以——希望他不在那一页。我可以再把问题分成两半,把那 250 页扔掉。现在我从 1000 页变成了 500 页,再到 250 页。现在我在移动,因为第一个算法是每次一页,第二个是两页。这是每次几百页。如果我大致再去中间;大致去中间;大致去中间,希望我能在最后一页找到约翰·哈佛。只能这样做一次,但这是最后一页。


So that invites the question, I would think, if the phone book does have 1,000 or so pages, how many times can I divide the problem in half to get down to one last page? So it's, roughly, 10. And the quick math is 1,000 goes to 500 to 250 to 125 to 67 something. So we have to deal with rounding issues, eventually. But assuming we work out the math, it's, roughly, 10 page tears. And that's crazy faster than 1,000 pages and still faster than 500 pages. So it's fundamentally better.
这就引出了一个问题,我想,如果电话簿确实有大约 1000 页,我可以将问题分成多少次一半,直到只剩下一页?所以,大约是 10 次。快速计算是 1000 变成 500,再到 250,再到 125,再到 67 左右。所以我们最终必须处理四舍五入的问题。但假设我们算出这个数学问题,大约是 10 次翻页。这比 1000 页快得多,仍然比 500 页快。所以这从根本上来说是更好的。


And, indeed, if I finally get to that final page, in the software world, you'd see something like this, John Harvard and his number, which you're welcome to call or text. But that's how we now have our answer, much like the single page there. But let's consider just how efficient that actually is. So here's a very rough, broad-- with broad strokes, a chart. So here's an xy plot. So here, on the horizontal, is going to be the size of the problem. And, by that, I mean, how many pages are we trying to go through? This would be zero pages. This would be a lot of pages.
而且,实际上,如果我最终到达最后一页,在软件世界中,你会看到这样的内容,约翰·哈佛和他的号码,你可以随意拨打或发短信。但这就是我们现在得到答案的方式,就像那一页一样。但让我们考虑一下这实际上有多高效。所以这里有一个非常粗略的图表。这里是一个 xy 图。横轴是问题的大小。我的意思是,我们要处理多少页?这将是零页。这将是很多页。


How much time does it take to solve the problem? How long does it take to find John Harvard or anyone else? This means no time. This means a lot of time. So what's the relationship among these algorithms? Well, the first one is actually just a straight line. If there's n pages in the phone book, well, I claim that it's a one-to-one relationship. Because if Verizon or the phone company adds another page next year, that just means I might have one more step next year, as well, to find John Harvard or anyone else.
解决这个问题需要多长时间?找到约翰·哈佛或其他人需要多长时间?这意味着没有时间。这意味着很多时间。那么这些算法之间有什么关系呢?其实,第一个算法只是一个直线。如果电话簿中有 n 页,我声称这是一种一对一的关系。因为如果 Verizon 或电话公司明年增加了一页,这只是意味着我明年可能还需要多一步来找到约翰·哈佛或其他人。


But the second algorithm, it's also a straight line, but it's lower, even though it might not look obvious. And what do I mean by that? Well, let me just draw some temporary lines. If this is how many pages are in the phone book-- dot, dot, dot-- notice that it takes this much time, on the red line, to solve the problem. But if I, instead, use the second algorithm, it takes me half as much time. So, even though they're both straight lines, one is strictly lower than the other, which means it's faster. It's better.
但第二个算法也是一条直线,但它更低,尽管这可能看起来不明显。我是什么意思呢?让我画一些临时的线。如果这是电话簿中的页数——点、点、点——注意在红线上解决问题需要这么多时间。但如果我使用第二个算法,所需时间只有一半。因此,尽管它们都是直线,但一条严格低于另一条,这意味着它更快。这更好。


But the third algorithm is a fundamentally different shape, and it looks a little something like this. And it looks like it's going to flatten, flatten, flatten out, but it never does. It just rises ever so slowly. And what this means is that if maybe Cambridge and Allston, here in Massachusetts, merge next year, so we go from 1,000 page phone book to a 2,000 page phone book, that means, if we're here this year, we're over here next year. It's not all that much higher.
但第三种算法的形状是根本不同的,它看起来有点像这样。它看起来会逐渐平坦,但实际上并不会。它只是缓慢上升。这意味着,如果明年剑桥和奥尔斯顿在马萨诸塞州合并,那么我们将从 1000 页的电话簿变成 2000 页的电话簿,这意味着,如果我们今年在这里,明年我们就在这里。并没有高出太多。


But it would be much higher if we were using the first two algorithms. Why? It'd be an extra 1,000 steps to find someone in that combined phone book or maybe another 500 steps. But, to be clear, if we're using my third and final algorithm, how many more steps will it take me next year, when Cambridge and Allston merge into one 2,000-page phone book? Just one more step, no big deal. It's one more page tear.
但如果我们使用前两种算法,结果会高得多。为什么?在那个合并的电话簿中找到某个人将多出 1,000 步,或者可能再多 500 步。但要明确的是,如果我们使用我第三个也是最后一个算法,明年当剑桥和奥尔斯顿合并成一本 2,000 页的电话簿时,我还需要多多少步?只需多一步,没什么大不了的。这只是多撕了一页。


And this is what we mean, ultimately, about not just writing code or implementing algorithms that are correct, but, now, that are well-designed or efficient, specifically. And this is what makes someone a better programmer, perhaps, than someone else, or a better problem-solver than someone else, is acquiring, over time, these skills. So that, sure, you could solve the problem quickly and dirtily, so to speak, but if you're going to have a lot of data eventually, be it in your phone book or maybe your Google website index, if you're searching the web, you're going to want to try to think about how to design algorithms that look like this.
这就是我们最终所指的,不仅仅是编写正确的代码或实现正确的算法,而是现在要特别关注设计良好或高效的代码。这使得某人成为更好的程序员,或许比其他人更优秀,或者成为更好的问题解决者,是随着时间的推移获得这些技能。因此,当然,你可以快速而粗糙地解决问题,但如果你最终会有大量数据,无论是在你的电话簿中,还是在你搜索网络时的谷歌网站索引中,你会想要考虑如何设计看起来像这样的算法。


Mathematically, this is called a logarithm. And it's log base 2 because I'm halving, halving, halving, again and again. But, for now, just know that it's a fundamentally faster and different shape. So, among our goals in CS50, ultimately, is not just to write and solve problems correctly, but, ultimately, ever more efficiently, as well. Any questions, then, on these ideas of efficiency and design? Any questions here? Yeah, in back?
在数学上,这被称为对数。它是以 2 为底的对数,因为我在不断地减半。但现在,只需知道它是一种根本上更快和不同的形状。因此,在 CS50 的目标中,最终不仅是正确地编写和解决问题,而且最终是越来越高效。关于这些效率和设计的想法,有什么问题吗?这里有问题吗?是的,后面有问题吗?


AUDIENCE: [INAUDIBLE].  观众:[听不清].

DAVID J. MALAN: A good question. Just to repeat it, can a graph like this capture the accuracy of the algorithm? Short answer, no. For instance, if I drew a straight line that is super low on this graph, which would imply that the algorithm takes the same amount of time, no matter how many pages there are, my algorithm might actually be to just pull a random page from the phone book, one step, and say, here it is. But that's not necessarily going to be accurate, unless I get really, really lucky.
大卫·J·马兰:一个好问题。为了重复一下,像这样的图表能否捕捉到算法的准确性?简短的回答是,不可以。例如,如果我在这个图表上画一条非常低的直线,这将意味着无论有多少页,算法所需的时间都是相同的,我的算法实际上可能只是从电话簿中随机抽取一页,一步完成,然后说,找到了。但这不一定是准确的,除非我真的非常幸运。


So the graph really only speaks to efficiency and the design of the algorithm, not the correctness of it. To analyze the correctness, you need to use another mechanism altogether, namely, logic. Other questions now, on efficiency, in this way? No? All right. So, with that in mind, let's begin to translate this to some actual code. And, in fact, before we look at, today, one actual programming language, albeit a graphical one, let's consider something called pseudocode.
所以图表实际上只涉及算法的效率和设计,而不是它的正确性。要分析正确性,你需要使用另一种机制,即逻辑。现在还有其他关于效率的问题吗?没有?好的。那么,考虑到这一点,让我们开始将其翻译成一些实际的代码。事实上,在我们今天查看一种实际的编程语言之前,尽管是一种图形化的语言,让我们先考虑一下所谓的伪代码。


So pseudocode has no formal meaning. Generally, you write it in English or whatever your own human language is. But you write your thoughts down tersely, succinct, but precisely. You try to really convey your thoughts, not with a wave of the hand, metaphorically, but step by step, precisely. So what do I mean by this? Here might be some representative pseudocode via which I describe that third and final algorithm in a way that I could hand it to you and you could do the same at home. Or I could hand it to someone at Google, and they could implement it in Android. Or I could hand it to someone at Apple, and they could implement it in iOS.
伪代码没有正式的含义。通常,你用英语或你自己的任何人类语言来编写它。但你要简洁、精确地写下你的想法。你试图真正传达你的想法,而不是随意挥手,隐喻地,而是一步一步,精确地。那么我这是什么意思呢?这里可能有一些代表性的伪代码,通过它我描述那个第三个也是最后一个算法,以一种我可以交给你,你也可以在家里做到的方式。或者我可以把它交给谷歌的某个人,他们可以在 Android 上实现它。或者我可以把它交给苹果的某个人,他们可以在 iOS 上实现它。


So, step one, I claimed, was "Pick up phone book." Step two was "Open to the middle of the phone book." Step three, "Look at the page," as I did. And now things get a little more interesting. Step four, "If person is on page," I have to make a decision. Presumably, what should I do if John Harvard is on the page I'm looking at? So stop and probably make the call, or email, or whatever the goal might be. And so I'm going to actually indent this, visually, by a few spaces, just to make clear that you should only do line five if the answer to line four is yes. Otherwise, you don't bother.
所以,第一步,我认为是“拿起电话簿。”第二步是“翻到电话簿的中间。”第三步,“查看页面,”就像我所做的那样。现在事情变得有趣起来。第四步,“如果页面上有这个人,”我必须做出决定。假设,如果我正在查看的页面上有约翰·哈佛,我应该怎么做?所以停下来,可能打电话,或者发邮件,或者无论目标是什么。因此,我实际上会在视觉上缩进几格,只是为了明确只有在第四行的答案是“是”的情况下,才应该执行第五行。否则,你就不必费心。


The next thing I'm going to do, line six, is consider another possibility. "If the person I'm looking for is earlier in the book," what do I want to do? Well, I could write-- I could describe this in a bunch of ways. I'm going to do this tersely, as follows. "Open to the middle of the left half of the book, so it's open to the middle of the left half of the book. And then, what am I going to do? Well, I've just divided the problem into something smaller. But it's fundamentally the same problem. It's just a fewer number of pages.
接下来我要做的,第六行,是考虑另一种可能性。“如果我正在寻找的人在书的前面,”我想做什么?好吧,我可以用很多方式来描述这一点。我将简洁地这样做:“打开书的左半部分的中间,所以它打开在书的左半部分的中间。那么,我要做什么?好吧,我刚刚把问题分成了更小的部分。但这在根本上是同一个问题。只是页数更少了。


So I'm just going to go back to line three and do it again because the problem is just getting smaller and smaller, presumably. Else, if the person I'm looking for is later in the book, open to the middle of the right half of the book, and, also, "Go back to line 3." But there's a fourth possibility and its failure to realize, sometimes, that there's other possible outcomes that make computers crash, or spinning beach balls, or the like, if a programmer doesn't anticipate some situation. What's the fourth possible situation, when looking for John Harvard?
所以我将回到第三行再做一次,因为问题似乎越来越小。否则,如果我寻找的人在书的后面,就打开书的右半部分中间,并且,“回到第三行。”但是还有第四种可能性,有时未能意识到还有其他可能的结果,这会导致计算机崩溃,或者出现旋转的沙滩球,或者类似的情况,如果程序员没有预见到某些情况。那么,在寻找约翰·哈佛时,第四种可能的情况是什么?


AUDIENCE: If they're not in the book.
受众:如果他们不在书中。


DAVID J. MALAN: If they're not in the book, at all. And, indeed, I might get to the very last page and John Harvard's not even on that one. I'd better handle that and say, else, as a catchall, just quit altogether. And, truly, often, in your Macs, PCs, phones, when they freeze, or, again, spinning beach ball, or the like, and just weird things happen, that's just because some human made a dumb mistake. And they didn't realize that you could somehow get your phone or your laptop into a configuration that they didn't anticipate. So we're going to try to handle that here.
DAVID J. MALAN:如果他们根本不在书中。实际上,我可能会翻到最后一页,约翰·哈佛甚至不在那一页上。我最好处理一下这个问题,或者说,作为一个总括,干脆放弃。实际上,通常在你的 Mac、PC、手机上,当它们卡住,或者再次出现旋转的沙滩球,或者其他奇怪的事情发生时,这只是因为某个人犯了一个愚蠢的错误。他们没有意识到你可以以某种方式将你的手机或笔记本电脑配置成他们没有预料到的状态。所以我们将在这里尝试处理这个问题。


Now this is just one way of writing pseudocode. There's no one way to do this. All of us in this room could come up with slightly different pseudocode. But I think you'll find characteristic are certain building blocks in all of our answers. Here, in yellow, are what, as of today, we're going to start calling "functions," technically speaking. These are like actions or verbs that literally, in this case, tell me what to do.
现在这只是编写伪代码的一种方式。没有一种固定的方法来做到这一点。我们在座的每个人都可以提出稍微不同的伪代码。但我认为你会发现我们所有答案中都有某些特征的基本构件。在这里,黄色部分是我们今天开始称之为“函数”的内容,从技术上讲。这些就像动作或动词,字面上在这种情况下告诉我该做什么。


Next, we're going to have these things, conditionals, forks in the road, so to speak, that take me down this path or another, metaphorically. So I do this thing or something else. But how do I decide if I want to go this way, or this way, or this way, or this way? I need to ask a question. And in programming, we'll typically use what are called Boolean expressions, named after a mathematician, Boole. And a Boolean expression is essentially just a question with a yes/no answer, a true or false answer, a 1 or 0 answer. It doesn't matter how you think about it, but it's got two possible answers.
接下来,我们将会有这些东西,条件,岔路口,可以说是把我带到这条路或另一条路,隐喻地说。所以我做这个事情或其他事情。但是我怎么决定是走这条路,还是那条路,还是这条路,还是那条路?我需要问一个问题。在编程中,我们通常会使用所谓的布尔表达式,以数学家布尔的名字命名。布尔表达式本质上就是一个有是/否答案的问题,一个真或假的答案,一个 1 或 0 的答案。无论你怎么理解,它都有两个可能的答案。


And so you can think of these as being expressions with question marks, even though I didn't draw such. Person on page, person earlier in book, person later in book, those are Boolean expressions. And they tell me whether I should go down one fork in the road or another. And, lastly, there's this, "Go back to line 3," in two different places. That represents what we call a "loop," some kind of cycle that's doing something again and again.
因此,您可以将这些视为带有问号的表达式,尽管我没有画出这样的表达式。页面上的人、书中早些时候的人、书中稍后的人,这些都是布尔表达式。它们告诉我是否应该走一条路的分叉。最后,还有这个“返回到第 3 行”,在两个不同的地方。这代表了我们所称的“循环”,某种反复执行某个操作的周期。


Now these are just a few building blocks here-- functions, conditionals, Boolean expressions, loops. But you'll find that they're characteristic of so many different languages, one of which we'll look at today, another of which we'll look at next week. And those include, indeed, C, and Python, and other languages still. And so this is why we focus on these basics and fundamentals in these early days because we're going to see them again and again. So even if you feel like that fire hose is hitting you sometime, we'll give you, today, ultimately, more visuals by which you can hang onto, so as to actually write code, ultimately, in different languages and solve all sorts of problems.
现在这些只是一些构建块——函数、条件语句、布尔表达式、循环。但你会发现它们是许多不同语言的特征,其中一种我们今天会看,另一种我们下周会看。这些语言确实包括 C、Python 以及其他语言。因此,这就是为什么我们在早期阶段专注于这些基础和基本概念,因为我们将一次又一次地看到它们。所以即使你觉得那股水流有时冲击着你,今天我们会给你更多的视觉材料,让你能够抓住,从而最终能够用不同的语言编写代码,解决各种问题。


Now, we'd be remiss in not bringing up what's behind characters like Spot, and ChatGPT, and other software, artificial intelligence. And it turns out, to get to the point of AI, we're actually going to need more building blocks than just functions, and loops, and conditionals. It's not going to be quite that simple. But this has been a lot, so far. Let's go ahead, here, and take a five-minute break. And when we resume, we'll take a look not only at AI, but also a specific language called Scratch. So no cake, just yet, but we'll come back in five.
现在,我们不能不提到像 Spot、ChatGPT 以及其他软件、人工智能背后的东西。事实证明,要达到人工智能的水平,我们实际上需要比仅仅函数、循环和条件语句更多的构建块。这并不会那么简单。但到目前为止,这已经很多了。我们先休息五分钟。当我们恢复时,不仅会看看人工智能,还会看看一种叫做 Scratch 的特定语言。所以现在还没有蛋糕,但我们五分钟后再见。


Before we dive back in, I just wanted to call out a special guest that we have here today, beyond Spot, someone who's come from even farther away. And, in fact, if any of you have taken CS50x, the OpenCourseWare version of the class, or dabbled in it over the past few years in some of CS50's online social communities, you might have had your questions answered by a certain human from New Zealand. And she's come all this way, today, to spend this lecture with us. This is CS50's own Brenda Anderson. If you might come up for acknowledgment from all of us here.
在我们重新开始之前,我想特别提到今天在这里的一个特别嘉宾,除了 Spot 之外,还有一个来自更远地方的人。事实上,如果你们中的任何人参加过 CS50x,即该课程的开放课程版本,或者在过去几年中在 CS50 的一些在线社交社区中接触过它,你们可能会有问题得到来自新西兰的某位人士的解答。她今天特意来到这里,与我们一起度过这次讲座。这是 CS50 的布伦达·安德森。如果你能上来接受我们所有人的认可。


[APPLAUSE]  [掌声]

It's not much, but just a little token of our thanks. Brenda has helped, truly, thousands of students online for the past many years. And, in fact, her own daughter has been the artist behind the duck that's about to loom large in CS50 this year and beyond. So, thank you to Brenda.
这不算什么,但只是我们感谢的一点小心意。布伦达在过去的许多年里,确实帮助了成千上万的学生在线学习。事实上,她自己的女儿是今年及以后在 CS50 中即将大放异彩的鸭子背后的艺术家。所以,感谢布伦达。


[APPLAUSE]  [掌声]

All right. So it's hard to escape a discussion of artificial intelligence, nowadays, but we thought we'd use this as an opportunity to connect some of these dots. Because, indeed, over the course of the semester, we'll be-- not only be talking about artificial intelligence or AI, but really using it all the more constructively to help you solve problems, help you get unblocked when you hit a wall, cognitively or syntactically, when writing code. And, indeed, it's no accident that we have this duck here, looming large, which is really the embodiment of the sort of AI that you'll experience within CS50, itself, this year.
好的。因此,今天很难避免讨论人工智能,但我们认为可以借此机会将这些点连接起来。因为,实际上,在整个学期中,我们不仅会讨论人工智能或 AI,而是会更加建设性地使用它,帮助你解决问题,帮助你在编写代码时遇到认知或语法障碍时解除阻碍。实际上,我们这里有这只大鸭子并非偶然,它真正体现了你在今年 CS50 课程中将体验到的那种 AI。


So let's talk about the so-called chatbots that inspired some of those headlines with which we began class, that weren't quite on the nose. So the class will still be taught by us humans, but helped by this CS50 duck, a chatbot of sorts. Now what do I mean by this? Well, it turns out that, when it comes to implementing something like an artificial intelligence, we don't quite have all of the building blocks yet, certainly after just today's week zero, to implement something like that.
所以让我们谈谈那些激发了我们开始课程时一些标题的所谓聊天机器人,这些标题并不完全准确。课程仍然会由我们人类教授,但会有这个 CS50 小鸭子的帮助,它是一种聊天机器人。那么我这是什么意思呢?实际上,当涉及到实现像人工智能这样的东西时,我们还没有所有的构建块,当然在今天的零周之后,更是如此,无法实现这样的东西。


But I think we can skate in that direction, intellectually. So, for instance, if we were to take a stab at implementing our own chatbot-- some interactive text-based program that talks to us, and answers questions, and the like-- we could try borrowing some of today's ideas already, those functions, conditionals, loops, and more. And I could write something like this. If I am writing code or pseudocode for a chatbot, I could program the chatbot to do something like this.
但我认为我们可以在智力上朝那个方向滑行。因此,例如,如果我们尝试实现自己的聊天机器人——一些与我们对话、回答问题等的互动文本程序——我们可以尝试借用一些今天的想法,比如那些函数、条件语句、循环等等。我可以写出这样的东西。如果我正在为聊天机器人编写代码或伪代码,我可以编程让聊天机器人做这样的事情。


If the student says, hello, to the chatbot, then the chatbot should say, hello, back. Else, if the student says, goodbye, well, the chatbot should say, goodbye, back. Else, if the student asks how you are, the chat bot should say that it's well. But things get a little harder when you start asking more interesting questions, like, else, if the student asks why 111 in binary is 7 in decimal. Now, you could imagine that we just have a conditional, with this Boolean expression, that programs the chatbot to just give you the answer and explain, in an English sentence, why that, in fact, is.
如果学生对聊天机器人说你好,那么聊天机器人应该回复你好。否则,如果学生说再见,聊天机器人应该回复再见。否则,如果学生问你好吗,聊天机器人应该说它很好。但是当你开始问更有趣的问题时,事情就变得有点困难,比如,如果学生问为什么二进制的 111 在十进制中是 7。现在,你可以想象我们只有一个条件,带有这个布尔表达式,程序让聊天机器人给你答案并用一句英语解释为什么实际上是这样。


But what if you, the student, asks why 110 is 6 in decimal or why 010 is 2? I mean, you can just imagine the explosion in the amount of code that we would have to write to just anticipate every darn question that you might ask about today and every other class, not to mention all of the knowledge in the universe.
但是如果你,学生,问为什么 110 在十进制中是 6,或者为什么 010 是 2 呢?我的意思是,你可以想象一下,我们需要编写多少代码来预见你今天和其他课程中可能提出的每一个问题,更不用说宇宙中的所有知识了。


So, surely, there are other ways to implement algorithms that allow something like a chatbot or AI, more generally, to be fed input, still, like all of the internet, all of the worldwide web, all of the pages and textual content therein, but to let it just figure out how to answer our questions based on those kinds of inputs, assuming the inputs, themselves, are accurate. So "large language models" is a term you might have heard bandied about over the past several months, or LLMs.
所以,肯定还有其他方法来实现算法,使得像聊天机器人或更广泛的人工智能能够接收输入,仍然像整个互联网、整个全球网络、其中的所有页面和文本内容一样,但让它根据这些输入来找出如何回答我们的问题,前提是输入本身是准确的。因此,“大型语言模型”是一个你可能在过去几个月中听到过的术语,或者LLMs。


And a large language model really is an implementation, in software, of code that actually takes, as input, lots and lots of language-- like the text of lots and lots of web pages, dictionaries, encyclopedias, Wikipedias, and the like-- and infers, from the patterns of English words or any human language, what a typical human might actually say when asked a question. And some of these questions are easy, right? Probably, on the internet, alone, not to mention everyday life, if someone extends their hand and says, "Hi. How are you," odds are, with 90% probability, you're going to say, "Good. Thanks. How are you?"
一个大型语言模型确实是软件中对代码的实现,该代码实际上以大量语言作为输入——比如大量网页、字典、百科全书、维基百科等的文本——并从英语单词或任何人类语言的模式中推断出,当被问到一个问题时,典型的人类可能会说什么。其中一些问题很简单,对吧?可能在互联网上,仅仅是日常生活中,如果有人伸出手说:“嗨,你好吗”,那么你有 90%的概率会回答:“很好,谢谢。你好吗?”


So I bet we could write software that just infers what it should say, probabilistically, statistically, based on those kinds of patterns online. And that's, indeed, where the world is starting to go, when it comes to the most sophisticated of algorithms, where you and I, the humans, we don't try to anticipate every possible input. Rather, we give it a more general purpose input, like all human knowledge, and ideally just let it figure things out.
我敢打赌,我们可以编写软件,根据这些在线模式以概率和统计的方式推断它应该说什么。实际上,这正是世界在最复杂的算法方面开始发展的方向,我们人类不再试图预测每一个可能的输入。相反,我们提供一个更通用的输入,比如所有人类知识,理想情况下让它自己去解决问题。


Now, we're not quite there, yet. And odds are you've heard of hallucinations or just mistakes that these large language models make. But their inputs are imperfect. And sometimes there's a bit of randomness sprinkled in because you don't want the thing to always say the exact same thing. Even you and I might say, "Eh, I'm not that great today" 10% of the time. So you need to perturb the output in some way. But within CS50 and within this world of large language models, we do have these tools like ChatGPT, and Bing, chat, and others.
现在,我们还没有完全到达目标。而且你可能听说过这些大型语言模型所产生的幻觉或错误。但它们的输入并不完美。有时会有一些随机性,因为你不希望它总是说完全相同的话。即使你我也可能会有 10%的时间说:“嗯,我今天不太好。”所以你需要以某种方式扰动输出。但在 CS50 和这个大型语言模型的世界中,我们确实有像 ChatGPT、Bing 聊天和其他工具。


And we'll stipulate that, for CS50's purposes, the direction we're going this year is that this is what's in the syllabus, dot, dot, dot; that it will not be allowed, it will be considered not reasonable to use AI-based software, other than CS50's own. So the goal here is not to simply take away tools that are clearly inevitable, in my view, and clearly helpful and productivity savers. But we'd like there to be some guardrails, intellectually, on just how helpful these chatbots are.
我们将规定,对于 CS50 的目的,今年我们要走的方向是,这就是课程大纲中的内容,点,点,点;不允许使用基于 AI 的软件,除了 CS50 自己的软件。因此,这里的目标并不是简单地剥夺那些在我看来显然不可避免且显然有帮助的生产力工具。但我们希望在智力上对这些聊天机器人有一些保护措施,以限制它们的帮助程度。


Because as you've probably seen, if you ask it a question, these chatbots are already pretty good at not just helping you finish your current thought, but it'll hand you your second and your third thought and do the assignment for you. But I think, through prompting, so to speak, we'll be able to coax some of our own tools, being computer scientists, ourself, in a direction that you actually find to be the right balance, akin to having a good tutor by your side 24/7, who doesn't just hand you answers, but, indeed, tries to lead you to the same.
因为正如你可能已经看到的,如果你问它一个问题,这些聊天机器人不仅能帮助你完成当前的想法,还能给你提供第二个和第三个想法,甚至为你完成任务。但我认为,通过提示,我们能够引导一些我们自己的工具,作为计算机科学家,朝着你认为合适的方向发展,就像有一个好的导师在你身边 24/7,不仅仅给你答案,而是确实试图引导你达到同样的结果。


So you actually get something out of the experience, and, ideally, three-plus months from now, those training wheels can come off, too. And you're able to still stand on your own. So it will be reasonable to use CS50's own AI-based software which will actually take the form of a CS50 duck, which is actually available now-- and we'll use it throughout the term-- at CS50.ai, a web-based application that's quite similar to ChatGPT, but that has the personality of a CS50 teaching fellow or TF, or teaching assistant, TA, that also happens to think of itself as a duck.
所以你实际上可以从这个经历中获得一些东西,理想情况下,三个月以上之后,这些辅助工具也可以去掉。你仍然能够独立站立。因此,使用 CS50 自己的基于 AI 的软件是合理的,这实际上将以 CS50 鸭子的形式出现,现在已经可以使用了——我们将在整个学期中使用它——在 CS50.ai,这是一个与 ChatGPT 非常相似的基于网络的应用程序,但它具有 CS50 教学助理(TF)或助教(TA)的个性,同时也认为自己是一只鸭子。


And for reasons we'll get to in a couple of weeks time, but rubber ducks, in particular, are a thing in programming. But more on that, before long, as you can even see from the one there on my desk. With that said, too, well, I'm going to call out CS50's own Brenda Anderson, whose daughter, Sophie, kindly not only created the first incarnation, digitally, of this duck, but also, most recently, once it actually did more than quack a random number of times in response to questions, has now been virtually brought to life, too.
而出于我们将在几周内讨论的原因,橡皮鸭在编程中尤其是一种现象。但关于这一点,稍后会详细说明,正如你可以从我桌子上的那只看到的那样。话虽如此,我还要提到 CS50 的 Brenda Anderson,她的女儿 Sophie 不仅首次数字化地创造了这只鸭子,而且最近,当它不仅仅是随机叫几声以回应问题时,它也被虚拟地赋予了生命。


So, all that and more, over the coming weeks, but you'll find, ultimately, that the goal is to really bring to life the availability of an AI-based tutor that you have access to, a friend in your-- next to you, that will help guide you through a lot of the course's challenges along the way. And we actually rolled it out this past summer, already, with some of your predecessors, through the Harvard Summer School.
所以,所有这些以及更多内容,在接下来的几周内,但你会发现,最终的目标是让一个基于人工智能的辅导员真正活跃起来,你可以随时访问,像一个朋友在你身边,帮助你应对课程中的许多挑战。实际上,我们已经在这个夏天推出了这个功能,已经与一些你的前辈们通过哈佛夏季学校进行了合作。


One student wrote, at summer's end, that this duck "felt like having a personal tutor-- I love how AI bots will answer questions without ego and without judgment generally entertaining even the stupidest of questions without treating them like they're stupid. It has, as one could expect, an inhuman level of patience." So there's actually something really there because as many teachers as there are in CS50-- myself, the course's preceptors, teaching fellows, teaching assistants, and course assistants-- there's only so many of us. And we're only awake so many hours of the day.
一位学生在夏末写道,这只鸭子“感觉就像有一个私人导师——我喜欢 AI 机器人会回答问题,没有自我意识和评判,通常会娱乐即使是最愚蠢的问题,而不会把它们当作愚蠢。它的耐心,正如人们所预期的,达到了非人类的水平。”所以实际上这里确实有一些东西,因为在 CS50 课程中,教师人数虽然很多——包括我自己、课程的助教、教学研究员、教学助理和课程助理——但我们的人数是有限的。而且我们一天中清醒的时间也有限。


And I think you'll find, too, that we're on the cusp of something pretty remarkable, in the coming years, where it's going to get a lot more enabling, now, to learn material not only within the confines of a class, but on your own, ultimately, as well. And as one other student put it, at summer's end, with respect to the duck, "Love love loved the duck. We're friends now." So that, too, awaits. But, first, we're going to need to start with the basics. And we started today by talking about binary. And darn it, here it is again.
我想你也会发现,我们正处于一个相当显著的转折点,在未来几年,学习材料将变得更加便利,不仅限于课堂内,也可以在自己的时间里进行学习。正如另一位学生在夏季结束时提到的关于鸭子的话:“我爱死这只鸭子了。我们现在是朋友。”所以,这也在等待着。但首先,我们需要从基础开始。我们今天开始讨论二进制。天哪,这又来了。


So we can't actually get to the point of using, or solving, or implementing AI until we understand this. And odds are most of you won't know, at a glance, what this piece of software does. But these are the 0's and 1's that represent, perhaps, the first program that any programmer writes, which is now a clue to some of you who have dabbled in code before. What does this pattern of 0's and 1's tell a typical computer to do? Might someone guess?
所以在我们理解这一点之前,我们实际上无法使用、解决或实现人工智能。而且你们中的大多数人可能一眼就看不出这段软件的功能。但这些是代表,也许是任何程序员编写的第一个程序的 0 和 1,这对一些曾经接触过代码的人来说是一个线索。这组 0 和 1 的模式告诉典型计算机做什么?有人能猜到吗?


AUDIENCE: "Hello, world."
受众: "你好,世界。"


DAVID J. MALAN: It's going to have it say, "hello, world," which is one of the very first programmer-- programs that most any programmer writes. Should you be able to recognize these 0's and 1's? Do I recognize these 0's and 1's? No, not at all. I just happen to know that they are the same. And that was a leading question. But they are representing data and instructions, ultimately, data like H-E-L-L-O, comma, W-O-R-L-D and an instruction like, "Print that data to the screen."
大卫·J·马兰:它将显示“你好,世界”,这是几乎所有程序员编写的第一个程序之一。你应该能够识别这些 0 和 1 吗?我能识别这些 0 和 1 吗?不,完全不能。我只是恰好知道它们是相同的。这是一个引导性问题。但它们代表的是数据和指令,最终,数据像 H-E-L-L-O,逗号,W-O-R-L-D,以及像“将该数据打印到屏幕上”的指令。


As for what these patterns of 0's and 1's are, this is not something that a computer scientist or programmer worries about. We just stipulate that, OK, someone, somewhere knows how to do this. And it's probably someone like Intel, who makes the hardware inside of the computers. But you and I, starting now, already, in week zero, can start to view binary more abstractly. We don't care about the 0's and 1's. We only care that you can use 0's and 1's to represent more useful quantities, like numbers, and letters, colors, and more.
至于这些 0 和 1 的模式是什么,这并不是计算机科学家或程序员需要担心的事情。我们只需规定,好吧,有人,在某个地方,知道如何做到这一点。可能是像英特尔这样的公司,他们制造计算机内部的硬件。但你我,从现在开始,已经在零周,可以开始以更抽象的方式看待二进制。我们不关心 0 和 1。我们只关心你可以使用 0 和 1 来表示更有用的量,比如数字、字母、颜色等等。


So this, next week, is going to be the code we actually start writing at a keyboard. And this is that language called C. It's the same language I, myself, learned years ago, when taking CS50, when all we learned at the time was C. But this, too, has some crypticness to it. And if you've never programmed before, you can probably wrap your mind around, OK, I'm guessing the "printf" prints out the "hello, world." But what's with the semicolon, the quotes, the backslash, and the curly braces, the #include, and all of this stupid syntax?
所以下周,我们将开始在键盘上实际编写的代码。这就是被称为 C 的语言。这是我多年前在学习 CS50 时学到的同一种语言,当时我们只学习了 C。但是,这也有一些晦涩的地方。如果你从未编程过,你可能会理解,好的,我猜“printf”是打印“hello, world。”但分号、引号、反斜杠、大括号、#include,以及所有这些愚蠢的语法又是怎么回事呢?


A lot of this, in the beginning of any class, is really a distraction. It is not intellectually interesting. But it's the stupid kind of stuff that's going to trip you up quite often. And so, today, what we'll do is focus not on syntax, characters on the keyboard, but ideas because what really matters in this program is that "printf" is a function here for our purposes. And that function is to display information on the screen. Everything else, as we'll soon see, has value and will be understood by you, before long, but for now, it's a distraction.
在任何课程的开始,很多内容实际上都是一种干扰。这并不是智力上有趣的东西。但这正是那些愚蠢的东西,常常会绊倒你。因此,今天我们要关注的不是语法、键盘上的字符,而是思想,因为在这个程序中,真正重要的是“printf”在这里是一个函数。这个函数的目的是在屏幕上显示信息。其他一切,正如我们很快会看到的,都是有价值的,并且你很快就会理解,但现在,这只是一个干扰。


Let's focus on those building blocks. When it comes time to write code, though, for the curious, especially if you've programmed before, we'll use a very popular free and open-source tool called Visual Studio Code, or VS Code.
让我们关注这些构建块。不过,当需要编写代码时,对于好奇的人,尤其是如果你之前编程过,我们将使用一个非常流行的免费开源工具,叫做 Visual Studio Code,或 VS Code。


We'll use a cloud-based version of it that we pre-install everything you need in the cloud for you so you don't have to deal with headaches like getting your Mac or PC to work. You'll use instead this URL, cs50.dev, but more on that in next week, week one.
我们将使用一个基于云的版本,我们会为您预先安装所需的一切,这样您就不必处理像让您的 Mac 或 PC 工作这样的麻烦。您将使用这个网址,cs50.dev,但下周第一周会详细介绍。


For now, we'll use another cloud-based editor called Scratch. And odds are some number of you use this probably as early as like middle school or the like. And that was fine to create your own animations, games, interactive art, or the like.
目前,我们将使用另一个基于云的编辑器,叫做 Scratch。你们中的一些人可能早在中学时就开始使用它。这非常适合创建自己的动画、游戏、互动艺术等。


But we'll use it today for just a bit. And we'll use it in the course's first homework, AKA problem set 0, to explore now some of these same ideas. And among the goals today for the remainder of today is not to focus so much on Scratch and the particulars because this is not a language that you're going to use often but to give you very visual representations of ideas so that when things do escalate next week to C, to the more cryptic, it's the same ideas just typed out instead of dragged and dropped.
但我们今天会用它一点点。我们将在课程的第一次作业,也就是问题集 0 中使用它,来探索一些相同的想法。今天剩下的目标之一不是过多关注 Scratch 及其细节,因为这不是你会经常使用的语言,而是给你非常直观的概念表示,这样当下周事情升级到 C 语言,变得更加晦涩时,这些想法只是以文本形式呈现,而不是拖放。


So by that, I mean this. I'm going to go ahead and share in just a moment the user interface of Scratch. But what's nice about Scratch is that this is how we're going to implement that same program today. These are two blocks or puzzle pieces on the screen, and they've been interconnected to tell the computer to say "hello, world" on the screen.
所以我的意思是这样。我将很快分享 Scratch 的用户界面。但 Scratch 的好处在于,今天我们将以这种方式实现相同的程序。这是屏幕上的两个块或拼图,它们已经互相连接,以告诉计算机在屏幕上显示“你好,世界”。


The user interface that we're about to use will look generally something like this. It's a web-based editor that you can also download it locally to use offline. And you'll see that at the left here are a whole bunch of puzzle pieces or blocks.
我们即将使用的用户界面大致如下所示。这是一个基于网页的编辑器,您也可以将其下载到本地以离线使用。您会看到左侧有一堆拼图块或模块。


They're categorized by color, and the blue ones tend to relate to motion, for instance. The purple ones represent looks. The pink one represents sounds. The yellow one represents events. More on that soon.
它们按颜色分类,蓝色的通常与运动有关,例如。紫色的代表外观。粉色的代表声音。黄色的代表事件。稍后会有更多信息。


The orange ones represent control, and then there's operators, variables, my blocks, and even some extensions we can install as well. So it just categorizes things visually and colorfully so that you can find what you're looking for.
橙色的代表控制,然后是运算符、变量、我的模块,甚至还有一些我们可以安装的扩展。它以视觉和色彩的方式对事物进行分类,以便你可以找到你所寻找的内容。


But we're going to use these puzzle pieces to drag and drop them onto this area here. And we're going to connect them when we want them to do something. What can they do? Well, by default, Scratch comes with this cat here, otherwise known as a sprite, which is a character in a game or in a graphics context.
但是我们将使用这些拼图块将它们拖放到这里的区域。我们将在需要它们执行某些操作时将它们连接起来。它们能做什么呢?默认情况下,Scratch 附带了这个猫,也称为精灵,它是游戏或图形上下文中的一个角色。


And this cat lives in this two-dimensional world in which the cat can go up, down, left, right. And you can actually change the cat's costume to be a dog, a bird, or anything else. It really is more of an idea than it is a specific animal in this case.
这只猫生活在这个二维世界中,猫可以上下左右移动。你实际上可以把猫的服装换成狗、鸟或其他任何东西。在这种情况下,它更像是一个概念,而不是特定的动物。


But the world that Scratch lives in looks a little something like this. It's like a Cartesian plane with x-coordinates and y-coordinates. And the numbers don't so much matter fundamentally, except that when you want the cat or any character to go up, down, left, or right by some amount, it's useful to know, for instance, that 0, 0 is the middle, 0 comma 0 for x comma y.
但 Scratch 所处的世界看起来有点像这样。它就像一个笛卡尔平面,有 x 坐标和 y 坐标。数字本身并不是那么重要,除了当你想让猫或任何角色向上、向下、向左或向右移动一定距离时,知道例如 0, 0 是中心点,0 逗号 0 是 x 逗号 y 是很有用的。


All the way up is a y value of 180. All the way down is -180. All the way to the left is -240. All the way to the right is 240. And generally, you're not going to have to worry about numbers. You're going to use these relatively-- go right, go left, go up, or down. But that's just the world that Scratch itself lives in here.
一路向上是 y 值 180。一路向下是 -180。一路向左是 -240。一路向右是 240。一般来说,你不需要担心数字。你将相对地使用这些——向右、向左、向上或向下。但这就是 Scratch 本身所处的世界。


So let's go about using Scratch here. I'm going to change over to my cloud-based editor here, where I've gone to scratch.mit.edu. And I've clicked Create in order to create a project. And that gives me this blank canvas here.
所以我们来使用 Scratch。我将切换到我的云编辑器,在那里我访问了 scratch.mit.edu。我点击了“创建”以便创建一个项目。这给了我这个空白画布。


And I'm going to do these somewhat quickly because I kind of know what I'm looking for. But part of the process with problem set zero is going to be clicking, and dragging, and sort of scrolling around to see what building blocks exist.
我将会稍微快速地进行这些,因为我大致知道我在寻找什么。但问题集零的过程部分将包括点击、拖动,以及四处滚动以查看存在哪些构建块。


But I know under Events there's this puzzle piece here when green flag clicked. Why is that germane? Well, if I zoom out, and go back to Scratch's world at the right here, notice that above Scratch's world there's a green flag, which is going to mean go, and a red stop sign, which, of course, is going to mean stop.
但我知道在事件下有这个拼图块,当绿色旗帜被点击时。为什么这很相关?好吧,如果我缩小视图,回到 Scratch 的世界,注意在 Scratch 的世界上方有一个绿色旗帜,这意味着开始,还有一个红色的停止标志,这当然意味着停止。


So if I drag this puzzle piece anywhere into the middle, it's just going to plop where I put it. But what that means semantically is when someone clicks that green flag, I want Scratch the cat to do this other thing. Well, what do I want it to do?
所以如果我把这个拼图块拖到中间的任何地方,它就会落在我放置的位置。但这在语义上意味着,当有人点击那个绿色的旗帜时,我希望 Scratch 猫做其他事情。那么,我希望它做什么呢?


Well, let me go under Looks. And looks here in purple have some puzzle pieces like this. Well, I can say hello for some number of seconds, or I can just go ahead and say hello. So let's do that. I'm going to drag this say block.
好吧,让我去外观部分。在这里,紫色的外观有一些像这样的拼图块。好吧,我可以说你好几秒钟,或者我可以直接说你好。那么我们就这样做。我将拖动这个说块。


And notice that as soon as I get close enough, it's going to magnetically want to connect. So I can let go, and they snap together automatically because they're the right shape. I don't have to say hello, exclamation point. I can change it to the more canonical, hello comma world.
注意,一旦我靠得足够近,它就会磁性地想要连接。所以我可以放手,它们会自动吸附在一起,因为它们的形状是正确的。我不需要说你好,感叹号。我可以把它改成更标准的,你好,逗号,世界。


So anything in this white oval is editable that you can change as just text there. If I now zoom out, let me go ahead and click the green flag. And voila-- this should be my first program in Scratch. Hello, world.
所以这个白色椭圆中的任何内容都是可编辑的,您可以将其更改为文本。如果我现在缩小,让我点击绿色的旗帜。瞧,这应该是我在 Scratch 中的第一个程序。你好,世界。


Without any of the distractions of syntax or weird characters on the screen, it's just done what I want it to do. All right. Let me go ahead and click Stop there. And let me make it a little more connected to what we've discussed thus far.
没有任何语法或屏幕上奇怪字符的干扰,它只是完成了我想要它做的事情。好的。让我点击停止。让我把它与我们迄今讨论的内容联系得更紧密一些。


So this puzzle piece here, say hello, world, represents what type of building block using the vocabulary of today? So it's a function. So these purple blocks are functions, say, hello, world. And let me give you another piece of terminology.
所以这个拼图块,这里说“你好,世界”,代表今天使用的什么类型的构建块?所以它是一个函数。这些紫色的块是函数,比如“你好,世界”。让我给你另一个术语。


These white ovals that take textual input-- in general, those are called parameters or arguments. And they customize the behavior of a function. So a parameter or an argument customizes, modifies the default behavior of a function, like in this case, say.
这些白色椭圆形框接受文本输入——一般来说,这些被称为参数或论据。它们自定义函数的行为。因此,参数或论据自定义、修改函数的默认行为,就像在这种情况下,举例来说。


Similarly, in the C code from earlier that we'll see next week, the printf function took a quoted phrase like, hello, world, similarly as input. But more on that in the future. So how does this connect to the mental model we presented earlier?
同样,在我们下周将看到的早期 C 代码中,printf 函数接受了一个带引号的短语,例如“hello, world”,作为输入。但关于这一点我们将来会详细讨论。那么这与我们之前提出的心理模型有什么联系呢?


Well, here's problem-solving as I described it earlier, inputs to outputs with algorithms or code in the middle. Well, what we've got here really is an input of, hello, world, the white oval. The function or algorithm that it's going into as input is the say block.
好吧,这就是我之前描述的问题解决过程,输入到输出之间有算法或代码在中间。我们这里实际上是一个输入,hello, world,白色椭圆。它作为输入进入的函数或算法是 say 块。


And what is the output of using this say block, the say function? It's the visual effect of having the cat have the little speech bubble appear above its head, saying, hello, world. So everything we do, in fact, can be mapped back to that very simple idea of inputs and outputs.
使用这个说块,say 函数的输出是什么?它是猫的头上出现小对话框的视觉效果,里面说着“你好,世界”。所以我们所做的一切,实际上都可以映射回这个非常简单的输入和输出的概念。


Well, let's make it a little more interesting, though. It's a little boring to just say "hello, world" all the time. Let me go ahead and drag this away. And if you just drag it to the left and let go, it gets automatically deleted.
好吧,让我们让它更有趣一点。总是说“你好,世界”有点无聊。让我先把这个拖走。如果你把它拖到左边然后放开,它会自动被删除。


Let me go under Sensing in light blue here. And you'll see there's a bunch of blocks, one of which is an ask block, an ask function, which is going to prompt the human, me, for some input. So let me go ahead and drag that over here, and it snaps together.
让我在这里的浅蓝色下进入感知。你会看到有一堆模块,其中一个是询问模块,一个询问函数,它将提示人类,也就是我,输入一些内容。所以让我把它拖到这里,它会自动对接。


I could change the question, but I'm OK with that question. We'll use what's your name. But notice that this block, ask, is a little special. It is not just going to display like a speech bubble on the screen. It's actually going to return a value, and this is another term of art today and onward.
我可以更改问题,但我对这个问题没意见。我们将使用“你叫什么名字”。但请注意,这个块,ask,有点特别。它不仅仅会像气泡一样在屏幕上显示。它实际上会返回一个值,这也是今天及以后另一个专业术语。


A return value is some value that can be handed back to you conceptually from a function so that you can do something with it. It's as though the ask function asks someone for their name, writes it down on a piece of paper, and hands you the piece of paper.
返回值是一个可以从函数概念上返回给你的值,以便你可以对其进行某些操作。就好像 ask 函数向某人询问他们的名字,把它写在一张纸上,然后把这张纸递给你。


You can do anything now that you want with that name. And here is how you access the name in this special block called answer, which, again, will start calling a return value. So if I want to say "hello" to someone specific, I'm going to do this.
你现在可以用那个名字做任何你想做的事情。这里是如何在这个叫做 answer 的特殊块中访问这个名字的方法,它将开始调用一个返回值。所以如果我想对某个特定的人说“你好”,我将这样做。


Let me zoom out. Let me go back to Looks, and let me go back to Say. And I'm going to change the say block here to "hello, comma." Then I'm going to zoom out. Well, I need two inputs, I think. So I'm going to grab another say block, and I'm going to put it below.
让我缩小一下。让我回到外观,然后让我回到说。我要把这里的说块改成“你好,逗号。”然后我要缩小一下。嗯,我想我需要两个输入。所以我要再抓一个说块,把它放在下面。


And I could just type "David," but this is going to defeat the whole point of asking me for the name. And it will only work for me. So I don't want to do that. So let me go back to Sensing, and notice the shape is important here.
我可以直接输入“David”,但这会违背询问我名字的整个目的。而且这只对我有效。所以我不想这样做。让我回到感知,注意这里形状的重要性。


Even if it's not quite the same size, the shape matters. And I can actually drag this and change the input of this say function to be whatever that return value is, that piece of paper that has the person's name on it. And it grows to fill, but now we have a program that I think when I click the green flag-- watch-- is going to prompt me.
即使它的大小不完全相同,形状也很重要。我实际上可以拖动这个并将这个 say 函数的输入更改为任何返回值,也就是那张写有名字的纸。它会扩展以填充,但现在我们有一个程序,我想当我点击绿色旗帜时——看——它会提示我。


What's your name? And now I have room to type down here. So I'm going to type D-A-V-I-D. I'm going to hit Enter, and it should say "hello, Dave." Wait. Hmm. Huh. Maybe it was-- didn't work. D-A-V-I-D. Here we go.
你叫什么名字?现在我有空间在这里输入。所以我要输入 D-A-V-I-D。我将按 Enter,应该会显示“你好,Dave。”等等。嗯。哈。也许是——没成功。D-A-V-I-D。来吧。


Hello, David. Hmm. It's missing the hello, but I'm quite sure we have a hello right there. So what explains this bug or mistake? Yeah.
你好,大卫。嗯。缺少了问候,但我很确定我们那里有一个问候。那么这是什么导致的错误或失误呢?是的。


AUDIENCE: [INAUDIBLE]. So they overlap.
受众:[听不清]. 所以它们重叠。


DAVID J. MALAN: Exactly. Put another way, my Mac, my PC, it's just so darn fast that it did exactly what it was supposed to. But it said "hello, David" so fast that we didn't even see, we being the human, the slowest part of the puzzle, see the actual hello.
大卫·J·马兰:没错。换句话说,我的 Mac,我的 PC,实在是太快了,完全按照预期运行。但它说“你好,大卫”的速度太快,以至于我们人类,作为这个谜题中最慢的部分,甚至没看到实际的问候。


So there's a few different ways to fix this, as you know. We could have it say "hello" for some number of seconds. So I could kind of do that. So let me do this. I can decouple these by just dragging and letting it go so that they're magnetically far apart.
所以有几种不同的方法可以解决这个问题,正如你所知道的。我们可以让它显示“hello”一段时间。所以我可以这样做。让我来做这个。我可以通过拖动并放开来解耦它们,使它们在磁性上远离。


Let me go ahead and drag this one, say hello for two seconds. I'm going to change the grammar here to be hello comma again. I'm going to go ahead and disconnect these two. I'm going to throw away the old one that I don't want to use.
让我继续拖动这个,打个招呼两秒钟。我将把这里的语法改为 hello, again。我将断开这两个连接。我将丢弃我不想使用的旧的那个。


And I'm going to reconnect this so that now-- OK. It's going to say hello for two seconds and then my name, hopefully. So let me click Stop and Start. D-A-V-I-D. Enter. OK. So it's better, but it's kind of poorly implemented.
我将重新连接这个,所以现在——好的。它会说你好两秒钟,然后是我的名字,希望如此。让我点击停止和开始。D-A-V-I-D。输入。好的。这样更好了,但实现得有点差。


Like, come on. I just wanted to say hello comma David. Why is that hard? Well, maybe we can actually combine these a little differently. And let me propose this. Let me actually get rid of these blocks again. And let me go ahead and just say one thing.
就像,来吧。我只是想说你好,逗号,大卫。为什么这那么难?好吧,也许我们可以以不同的方式组合这些。让我提议这个。让我实际上再把这些块去掉。让我先说一件事。


But can I somehow combine this to say hello comma David all in one breath? Well, it turns out if I go under Operators, I know from having played with this before that there's this puzzle piece down here called join.
但是我能否以某种方式将这句话合并为“你好,戴维”,一口气说出来呢?好吧,事实证明,如果我去操作符下,我知道之前玩过这个,有一个叫做 join 的拼图块在这里。


It's an oval. It's a little big, but, again, it will grow to fill. And by default, it wants to join two words, "apple" and "banana." But those are just placeholders. So let me go ahead and drag this over the default hello. Let me change "apple" to hello comma space and then banana.
这是一个椭圆。它有点大,但它会生长以填充。默认情况下,它想要连接两个词,“apple”和“banana”。但这些只是占位符。所以让我把这个拖到默认的 hello 上。让我把“apple”改为 hello,逗号,空格,然后是 banana。


Let me go back to Sensing. Let me grab answer and drag and drop that. So now notice that I'm kind of layering my ideas. And put another way, the output of this join block is presumably going to join two things together, apple and banana or hello comma David.
让我回到感知。让我获取答案并拖放它。所以现在注意到我正在逐层叠加我的想法。换句话说,这个连接块的输出大概是将两个东西连接在一起,苹果和香蕉,或者你好,戴维。


And then the output of join is going to become the input to say. So aesthetically, it just looks better. It's still correct, but it's just better. So if I type "David," and hit Enter, hello, David. This is what a normal program would presumably do, not show you part of the phrase and then the rest of the phrase.
然后,join 的输出将成为 say 的输入。因此,从美学上讲,它看起来更好。它仍然是正确的,但就是更好。所以如果我输入“David”,然后按 Enter,您好,David。这是一个正常程序可能会做的事情,而不是先显示短语的一部分,然后再显示其余部分。


Like, it's just better in this way. So let's connect this now to this same puzzle piece and this methodology. So here's that same puzzle piece, ask. How do we fit it into this input and output flow with algorithms? Well, the input to that puzzle piece is something like, what's your name, question mark.
就像,这样更好。那么现在让我们将其连接到这个相同的拼图块和这个方法论上。所以这是那个相同的拼图块,问。我们如何将其融入这个输入和输出流与算法中?那么,那个拼图块的输入是类似于“你叫什么名字?”的问题。


Then the algorithm or the code implementation thereof is this ask block and wait so the human has a moment to type their response in. The output of that function recall is a return value. You don't see anything on the screen by default because we've not used say yet, but we get this return value.
然后算法或其代码实现是这个请求块并等待,因此人类有时间输入他们的响应。该函数调用的输出是一个返回值。默认情况下,您在屏幕上看不到任何内容,因为我们还没有使用说,但我们得到了这个返回值。


And let me scooch everything over now so that we can now join those inputs together. So here's this puzzle piece. Let me go ahead and propose that the inputs now to the join block are two arguments or two parameters, so to speak, hello and answer.
现在让我把所有东西挪过来,这样我们就可以将这些输入结合在一起。这里是这个拼图块。让我提议现在连接块的输入是两个参数,或者说两个参数,hello 和 answer。


They go into that join puzzle piece, whose purpose in life is to return one joined version thereof. Let me slide this all over logically now so that now that output becomes the input to the say block and now is why the cat has the speech bubble saying all at once, hello comma David.
它们进入那个连接拼图块,其生命的目的是返回一个连接的版本。让我现在逻辑上把这一切滑动过来,这样输出就成为了说块的输入,这就是为什么猫有一个气泡同时说,您好,戴维。


So what we've done here is kind of composed the output and from one function into the input of another. And you can think of this in a couple of different ways, left to right, as I did there, or kind of like stacking these things on top of one another.
所以我们在这里所做的就是将一个函数的输出组合到另一个函数的输入中。你可以从几种不同的方式来理解这一点,像我刚才那样从左到右,或者像是将这些东西堆叠在一起。


But at the end of the day, even as programming gets more and more powerful for us, it's just inputs and outputs, inputs and outputs. And thankfully, with built-in functionality from our friends at MIT who designed Scratch, I can even do something playful like this.
但归根结底,即使编程对我们变得越来越强大,它仍然只是输入和输出,输入和输出。幸运的是,得益于我们在麻省理工学院的朋友们设计的 Scratch 的内置功能,我甚至可以做一些像这样的有趣的事情。


I can go to that Extensions button at the bottom. And there's a lot of fancy things I can add here, like text-to-speech. So let me go ahead and choose text-to-speech. And let me go ahead here and change the say block in purple.
我可以去底部的扩展按钮。这里有很多我可以添加的炫酷功能,比如文本转语音。所以让我选择文本转语音。接下来我在这里更改紫色的说块。


Let me get rid of the say block, and let me borrow this. Let me get the speak block like this. And now let me drag and drop this oval. It's going to grow to fill. And I think it's just about to be a little more interesting. Let me click Play now, and hopefully this isn't too loud.
让我去掉说的块,让我借用这个。让我像这样获取说的块。现在让我拖放这个椭圆。它会扩展以填充。我觉得这将变得更有趣。让我现在点击播放,希望声音不会太大。


D-A-V-I-D. Enter.  D-A-V-I-D。输入。

SPEAKER: Hello, David.  发言人:你好,David。

DAVID J. MALAN: OK.
大卫·J·马兰:好的。


[APPLAUSE]  [掌声]

Thank you. Thank you. That's a low bar. [CHUCKLES] Let me go ahead and set the voice too. And you might now remember how we began class, where we had a robotic, computerized voice. Well, we didn't use Scratch at the time, but we could change this in Scratch alone to be a little different.
谢谢。谢谢。这是一个低标准。[笑] 让我先设置一下声音。你可能还记得我们是如何开始上课的,当时我们有一个机器人、计算机化的声音。好吧,我们当时没有使用 Scratch,但我们可以仅在 Scratch 中将其稍作不同。


So D-A-V-I-D.  所以 D-A-V-I-D。

SPEAKER: Hello, David.  发言人:你好,David。

DAVID J. MALAN: OK. Little creepy, but we can play all day long with that. But the point is that these functions are just now doing something a little different. But it's just these inputs and outputs. Well, let's make the cat more like a cat.
大卫·J·马兰:好的。有点 creepy,但我们可以整天玩这个。但重点是这些函数现在只是在做一些稍微不同的事情。只是这些输入和输出。好吧,让我们让猫更像一只猫。


Let me go ahead and throw away all of this asking question stuff. Let me go up to Sound, and let me go ahead and drag the play sound meow until done. And here too it comes with meow. You can add your own sounds as well.
让我先把这些提问的东西扔掉。让我去声音那里,然后拖动播放声音的喵,直到完成。在这里也有喵。你也可以添加自己的声音。


But I'm just going to use the default meow and here too. Hopefully, this won't be too loud. Let's make the cat meow by clicking Play.
但我只是要使用默认的喵声,这里也是。希望这不会太吵。我们通过点击播放来让猫咪叫。


[MEOWING]  [喵喵声]

OK. It's a little piercing, but it's cute. And if I want the cat to meow twice, I could just play the game twice.
好的。虽然有点刺耳,但很可爱。如果我想让猫叫两次,我可以只玩两次游戏。


[MEOWING]  [喵喵声]

All right. But it would be nice to just get it to meow automatically two, or three, or more times. So you know what I could do? I could just drag a second one of these. Actually, you know what? I could even just right-click or Control-click and duplicate them.
好的。不过如果能让它自动喵两次、三次或更多次就好了。那么你知道我可以做什么吗?我可以再拖一个这样的。其实,你知道吗?我甚至可以右键点击或按住 Control 点击并复制它们。


But I'll just keep dragging and dropping. There's different ways to solve problems. And now let me click Play.
但我会继续拖放。有不同的方法来解决问题。现在让我点击播放。


[MEOWING]  [喵喵声]

OK. Cat does not sound particularly happy. So we could go under-- how about Control? We could wait one second. Now, there's no room, but it will sort of expand to give room for me. So let me try this.
好的。猫听起来并不是特别高兴。那么我们可以往下走——控制怎么样?我们可以等一秒钟。现在没有空间,但它会稍微扩展以给我留出空间。所以让我试试这个。


And now it's going to wait one second in between meows.
现在它将在每次喵叫之间等待一秒钟。


[MEOWING]  [喵喵声]

OK. Let me stipulate that is correct. If my goal is to get the cat to meow three times, it meowed three times. But per our discussion earlier of algorithms and the design thereof, this is not the best design.
好的。让我规定这是正确的。如果我的目标是让猫叫三次,它叫了三次。但根据我们之前关于算法及其设计的讨论,这并不是最佳设计。


[MEOWING]  [喵喵声]

OK?  好的?

[LAUGHTER]  [笑声]

Thank you for playing along at home. Yeah. In what sense is this arguably not well-designed? Yeah.
感谢您在家参与。是的。在什么意义上这可以说设计得不好?是的。


AUDIENCE: You repeated yourself.
观众:你重复了自己。


DAVID J. MALAN: I repeated myself, which actually in programming tends not to be a good thing. Now, it was easy. I almost resorted to copy-paste, which saves me time upfront. But just imagine a contrived scenario.
大卫·J·马兰:我重复了自己,这在编程中实际上往往不是一件好事。现在,这很简单。我几乎 resorted to copy-paste,这样可以节省我前期的时间。但想象一下一个人为的场景。


Now, what if I want it to wait like two seconds in between? All right. It's not that big a deal. I change it here, and I change it here. But what if the program is meant to meow 10 times?
现在,如果我想让它在两次之间等待两秒钟呢?好吧。这没什么大不了的。我在这里更改它,然后在这里更改它。但是如果程序是要喵 10 次呢?


Then I have to change it here, and here, and here, and here, and here. And eventually I'm going to screw up. Humans are fallible. I'm going to overlook one of them. One time, it's going to be one second. Another is going to be two, and just stupid things will happen.
然后我必须在这里、这里、这里、这里和这里进行更改。最终我会搞砸。人是有缺陷的。我会忽视其中一个。有一次,可能是一秒钟。另一次可能是两秒,愚蠢的事情会发生。


You're setting yourself up for failure if you design things poorly. And so I would propose that we use another type of building block to solve this instead. Yeah.
如果你设计得不好,你就是在为自己设定失败。因此,我建议我们使用另一种构建模块来解决这个问题。是的。


AUDIENCE: [INAUDIBLE]  观众:[听不清]

DAVID J. MALAN: Yeah. So we could use a loop and just write minimal code but to accomplish even more functionality. So let me throw away most of these blocks. And let's go and grab this repeat block, which we haven't used yet, but it's right there.
大卫·J·马兰:是的。所以我们可以使用循环,只需编写最少的代码,但可以实现更多功能。所以让我丢掉大部分这些模块。我们去拿这个重复模块,我们还没有使用过,但它就在那儿。


And as the name suggests, this will allow me to repeat something some number of times. Let me put these two puzzle pieces inside. It'll grow to fill. Let me reconnect it to the green flag. I'll change the default 10 to a 3. And now--
正如名称所示,这将允许我重复某些操作若干次。让我把这两个拼图块放进去。它会扩展以填充。让我把它重新连接到绿色标志。我将默认的 10 改为 3。现在——


[MEOWING]  [喵喵声]

It's just sort of better because if now you want it to meow more times, you change it one place. If you want it to slow down, you change it in one place. There's no room for error. And that's generally a good thing.
这只是更好一些,因为如果你现在想让它喵更多次,你只需在一个地方更改。如果你想让它慢下来,你也只需在一个地方更改。没有出错的余地。这通常是件好事。


But this is silly. Like, Scratch comes with a cat. Why didn't MIT give us a puzzle piece called "meow?" Like, why am I implementing the idea of meowing myself? Like, that took me what? 1, 2, 3, 4 puzzle pieces.
但这太傻了。比如,Scratch 自带一只猫。为什么麻省理工学院不给我们一个叫“喵”的拼图块?为什么我自己要实现喵的这个想法?这花了我多少时间?1、2、3、4 个拼图块。


Why isn't there just one puzzle piece that meows for me? This too we can do in code, be it in Scratch, or C, or other languages too. I'm going to go down to these pink my blocks here, where I can create my own puzzle piece.
为什么没有一个拼图块可以为我喵喵叫呢?这我们也可以用代码来实现,无论是在 Scratch、C 还是其他语言中。我将去到这里的粉色我的积木,在那里我可以创建我自己的拼图块。


And I'm going to call this literally "meow." And I'm going to go ahead and just click OK. And notice that it's given me this new type of start connector. It's a start puzzle piece that nothing goes above it. But you can put anything you want below it.
我将这个称为“喵”。我将继续点击确定。请注意,它给了我这种新的开始连接器。这是一个开始拼图块,上面没有任何东西。但你可以在下面放任何你想要的东西。


And I'm going to go ahead and cheat here. I'm just going to grab my existing code, so to speak. This is code I'm writing, even though it's puzzle pieces. And now let me just claim, and I'll move this aside. Here is now an implementation of my own function, my own block called "meow," whose purpose in life is to meow until done and then wait one second.
我将直接作弊。我将抓取我现有的代码,可以这么说。这是我正在编写的代码,尽管它是拼图块。现在让我声明一下,我会把这个移开。现在这是我自己实现的一个函数,我自己的块,叫做“meow”,它的目的是喵喵叫直到完成,然后等待一秒钟。


But what's powerful now is notice at top left, now that I've made the block, I can use it any number of times. So I can grab this meow block, drag it over here, and you know what? Now that "meow" exists as an idea, I can abstract that away.
但现在强大的是注意左上角,现在我已经创建了这个块,我可以使用它任意次数。所以我可以抓住这个“喵”块,把它拖到这里,你知道吗?既然“喵”作为一个概念存在,我可以将其抽象化。


And I'm just going to arbitrarily drag it way to the bottom. I'm not deleting it. I'm just putting it out of sight, out of mind so that we can focus now on this idea. And I claim that this implementation of meowing is sort of better because it's more compact, it does what it says, and I don't care about the implementation details of "meow."
我只是随意把它拖到最底部。我并没有删除它,只是把它放在视线之外,以便我们现在可以专注于这个想法。我声称这个喵叫的实现更好,因为它更紧凑,做到了它所说的,我不在乎“喵”的实现细节。


So this idea of abstraction, something we're going to use frequently. To abstract something away is to simplify. Don't think about the underlying implementation details. Just care about what it does or what it's called. Someone has to care about the implementation details, like me 30 seconds ago. But here on out, I don't need to care.
所以这个抽象的概念是我们将频繁使用的。抽象某样东西就是简化。不要考虑底层的实现细节。只关心它的功能或名称。总会有人关心实现细节,比如我 30 秒前的样子。但从现在开始,我不需要关心。


And so in fact, you and I are using the abstraction that is Scratch because I don't know how to put a speech bubble on the screen. I don't know how to create that sound, meow. MIT did that, and they abstracted those pieces of functionality away already for us by just giving us these puzzle pieces we see here.
实际上,你和我正在使用 Scratch 这个抽象,因为我不知道如何在屏幕上放置一个对话框。我不知道如何创建那种声音,喵。麻省理工学院做到了这一点,他们已经为我们抽象了这些功能,只是给了我们这些我们在这里看到的拼图块。


So the code will work the exact same.
所以代码将完全一样地工作。


[MEOWING]  [喵喵声]

But it's sort of better designed now because now I've abstracted away the idea of meowing. But I bet I can improve this further. Can I get rid of the repeat block altogether? And let me just tell the meow block how many times to meow.
但现在它的设计更好了,因为我已经抽象出了喵的概念。但我敢打赌我可以进一步改进这个。我能完全去掉重复块吗?让我告诉喵块要喵多少次。


Well, let me go down to the bottom and look at the implementation details. I'm going to right-click or Control-click on this, and I'm going to edit it. So I'm going to make a change. And I didn't do this before, but I'm going to call it "meow," as before.
好的,让我往下看一下实现细节。我将右键点击或按 Control 键点击这个,然后我将进行编辑。所以我将做一个更改。我之前没有这样做,但我将把它命名为“meow”,和之前一样。


I'm going to add an input. And just so I know what it says what it does, I'm going to add the word "times" here. And I'm going to change this placeholder to n. n for "number" is the go-to placeholder any time we want to talk about a number in CS.
我要添加一个输入。为了让我知道它的意思,我将在这里添加“times”这个词。我将把这个占位符改为 n。n 代表“number”,是我们在计算机科学中谈论数字时的首选占位符。


So now notice the puzzle piece looks a little different. It takes an argument or a parameter called n, which represents the number of times you want the thing to meow. Now, that doesn't do that yet. So let me go back to my other code.
现在注意到拼图块看起来有点不同。它接受一个参数,叫做 n,表示你希望这个东西喵多少次。现在,它还没有做到这一点。所以让我回到我的其他代码。


Let me just decouple these temporarily. I'm going to move my loop into my implementation of meowing. But I don't want to hard code, that is literally write the number 3 anymore. I'm going to grab this oval and put it there.
让我暂时解耦这些。我将把我的循环移到我实现的喵叫中。但我不想再硬编码,也就是字面上写数字 3。我将抓住这个椭圆并把它放在那里。


So now I've generalized the function. So now it will meow any number of times, 0 on up, by executing that loop and now more powerfully. Out of sight, out of mind. Notice that my code just became so darn simple. Like, my function is called "meow."
所以现在我已经将函数进行了泛化。现在它可以喵任意次数,从 0 开始,通过执行那个循环,而且现在更强大了。眼不见,心不烦。注意到我的代码变得如此简单。我的函数叫做“meow”。


It meows some number of times. All I have to do is type a number there, and it just works. And I don't care any more about those lower-level, so to speak, implementation details. So here, no surprise. If I type in the number 3, zoom out, and hit Play--
它喵了几次。我只需在那输入一个数字,它就能正常工作。我不再关心那些所谓的低级实现细节。所以在这里,没什么意外。如果我输入数字 3,缩小,然后点击播放——


[MEOWING]  [喵喵声]

--it still works just fine. So any questions on what we've just done here? It's still just meowing, but that's besides the point. It's this creation of our own functions, this modularity, this abstraction that's going to be the idea that keeps coming back to us.
--它仍然运行得很好。那么对于我们刚刚做的事情有什么问题吗?它仍然在喵喵叫,但这不是重点。正是我们自己创建函数的过程,这种模块化,这种抽象,将是不断回归的理念。


No? All right. So let's make this a little more cat-like. Let me throw away all of this code. And let me go ahead-- oops-- let me throw away this code first and then the rest of this code. And let me go ahead and give myself another green flag block.
不?好吧。那么让我们让这个更像猫一点。让我把所有这些代码扔掉。然后让我继续——哎呀——先把这段代码扔掉,然后再扔掉其余的代码。接下来让我再给自己一个绿色标志块。


And let me go ahead, and let's create a cat that allows us to pet it by moving my cursor over the cat. And maybe it likes that, so it'll meow when I do that. So let me go under Control, and let me grab this if conditional, which we talked about as a building block earlier.
让我继续,我们来创建一只猫,当我将光标移动到猫身上时,可以抚摸它。也许它会喜欢这样,所以我这样做时它会喵喵叫。现在让我进入控制部分,抓取这个我们之前讨论过的条件语句,作为构建块。


Let me go to Sensing, and we haven't used this before. But here is a weird sort of diagonal shape that says touching mouse pointer question mark. So that's a Boolean expression. So I'm going to drag that, and it's definitely the wrong size. But it is the right shape, so it will grow to fill.
让我去感应,我们之前没有使用过这个。但是这里有一个奇怪的对角形,显示“触摸鼠标指针问号”。所以这是一个布尔表达式。我将把它拖动,但它的大小显然是错误的。不过它的形状是正确的,所以它会扩展以填充。


And the question I want to ask is if the cat is touching the mouse pointer, then go ahead and meow happily. So let me grab the meow sound, put it in there. And so I think when I click the green flag to start the program, and now I let the mouse pointer touch the cat, we should hear-- huh. huh.
我想问的问题是,如果猫碰到了鼠标指针,那么就去高兴地喵喵叫。所以让我抓取喵的声音,把它放进去。因此,我认为当我点击绿色旗帜开始程序时,现在我让鼠标指针碰到猫,我们应该听到——哈。哈。


Doesn't seem to be working. There's a bug or a mistake in this program too. What did I do wrong? Yeah.
似乎不起作用。这个程序中也有一个错误或失误。我做错了什么?是的。


AUDIENCE: You didn't specify the sprite [INAUDIBLE].
受众:您没有指定精灵[听不清]。


DAVID J. MALAN: I don't need to specify the sprite explicitly because a detail I didn't discuss earlier. In the bottom right of the screen, notice that the cat is already selected. So this code relates to that cat. So your instinct is good if we had two or more sprites, but I just have one, so I'm covered there.
DAVID J. MALAN:我不需要明确指定精灵,因为有一个我之前没有讨论的细节。在屏幕的右下角,注意到猫已经被选中了。所以这段代码与那只猫有关。如果我们有两个或更多的精灵,你的直觉是对的,但我只有一个,所以我在这里没问题。


Other thoughts. Yeah.  其他想法。是的。

AUDIENCE: It only checks once.
受众:它只检查一次。


DAVID J. MALAN: It only checks?
大卫·J·马兰:它只是检查吗?


AUDIENCE: Once.  受众:一次。

DAVID J. MALAN: Once. So I click the green flag. The computer did what I told it to do. The mouse pointer was not touching the cat at that moment because it was touching the green flag. So, of course, it didn't meow.
大卫·J·马兰:一次。所以我点击了绿色旗帜。计算机按照我告诉它做的事情执行了。此时鼠标指针并没有接触到猫,因为它接触的是绿色旗帜。因此,当然它没有喵。


So what maybe is the fix here? What puzzle piece can we add?
那么这里的解决方案是什么呢?我们可以添加哪个拼图块?


AUDIENCE: After the green flag is [INAUDIBLE].
观众:在绿旗之后[听不清]。


DAVID J. MALAN: OK. OK. Interesting solution. So let me go ahead, and under Control let me grab a-- wait one second. I'm going to change the 1 to 5, and now I'm going to click the green flag. So here we go.
大卫·J·马兰:好的。好的。有趣的解决方案。那么让我继续,在控制下让我抓一个——等一下。我将 1 改为 5,现在我将点击绿色的旗帜。好了,我们开始吧。


1, 2, 3, 4, 5. Damn it.
1, 2, 3, 4, 5. 该死。


[MEOWING]  [喵喵声]

OK. That was yours, not mine.
好的。那是你的,不是我的。


[LAUGHTER]  [笑声]

It didn't work.  它没有工作。

AUDIENCE: [INAUDIBLE]  观众:[听不清]

DAVID J. MALAN: Sorry?
大卫·J·马兰:抱歉?


AUDIENCE: [INAUDIBLE]  观众:[听不清]

DAVID J. MALAN: Oh, maybe forever. So your approach would work, but it's very much a hack, if you will. Like, I would have to time it perfectly so that the mouse pointer is touching it, or, conversely, I have to drag it there and just leave it there for five seconds.
大卫·J·马兰:哦,也许是永远。所以你的方法是可行的,但这非常像一种黑客手段。如果你愿意的话。我必须完美地把握时机,让鼠标指针正好触碰到它,或者,反过来,我必须把它拖到那里,然后就让它在那里停留五秒钟。


And that's a little weird because you don't just touch a cat and then five seconds later it meows at you. Like, presumably, we want it to be more interactive. So I like this idea of a loop. Right? Why don't we just tell the cat to forever listen for the cursor as by using not repeat but forever?
这有点奇怪,因为你不可能只是碰一下猫,然后五秒钟后它就对你喵喵叫。显然,我们希望它更具互动性。所以我喜欢这个循环的想法。对吧?我们为什么不告诉猫永远监听光标,而不是使用不重复,而是永远呢?


Let me move this in here. So now the cat's going to be told when the green flag is clicked just forever, if touching, if touching, if touching. Oh, meow when actually touched. So now if I zoom out and hit Play, nothing's happening.
让我把这个移到这里。所以现在当点击绿色旗帜时,猫会被告知永远,如果接触,如果接触,如果接触。哦,实际被触碰时喵。现在如果我缩小并点击播放,什么也没有发生。


I don't have to wait any number of seconds. But when I do touch the cat--
我不需要等待任何秒数。但是当我触摸猫时——


[MEOWING]  [喵喵声]

[APPLAUSE]  [掌声]

[CHUCKLES] Fan section here. Thank you.
[笑] 粉丝专区在这里。谢谢。


[MEOWING]  [喵喵声]

So now it's actually working quite well. So there we have sort of a logical bug. But it does make sense if you think about what it was you told the computer to actually do. Well, let's make things even more interesting by using one of these extensions.
现在它实际上运行得很好。所以我们有一个逻辑错误。但如果你考虑一下你告诉计算机实际要做的事情,这就有意义了。好吧,让我们通过使用这些扩展中的一个来让事情变得更加有趣。


In this bottom left corner, this is how I got to text-to-speech earlier. Let me go to Video Sensing over here too. And I can actually-- there we go-- literally, the video has come on. Let me go ahead and do this. Get rid of this code, and let me step out of the frame.
在左下角,这就是我之前如何进入文本转语音的。让我也去这里的视频感应。我实际上可以——看,视频已经打开了。让我继续这样做。去掉这段代码,让我走出画面。


When video motion is greater than-- well, this is CS50, so let's just type in 50, which is just a measure of motion. Let me go and play sound meow. OK. And let me stop and restart. All right. So there's the cat.
当视频运动大于——好吧,这是 CS50,所以我们就输入 50,这只是一个运动的度量。让我来播放声音“喵”。好的。然后让我停止并重新开始。好的。猫来了。


[MEOWING]  [喵喵声]

OK. It worked. The cat's a little insensitive.
好的。它成功了。猫有点迟钝。


[MEOWING]  [喵喵声]

There we go. Actually, you know what? Maybe it's just-- let me put-- let's change it. 20. Oh, my God. Oh, that's OK.
好了。其实,你知道吗?也许只是——让我来——我们换一下。20。哦,我的天。哦,没关系。


[MEOWING]  [喵喵声]

There we go. All right. There we go. So now it's a little more sensitive to the petting by using the camera. Now, this is kind of a weird example. And if I just start moving around crazily, like, it's just going to meow incessantly, which was what was happening--
好了。现在它对用相机抚摸的反应更敏感了。现在,这有点奇怪的例子。如果我开始疯狂地移动,它就会不停地喵喵叫,这正是之前发生的情况——


[MEOWING]  [喵喵声]

Stop.  停止。

[LAUGHTER]  [笑声]

OK. When in doubt, this is when you reload the page.
好的。当有疑问时,这就是你重新加载页面的时候。


[LAUGHTER]  [笑声]

All right. So now we're back to where we wanted to be. But where can we now use these kinds of building blocks? Like, we were doing such tiny little programs. But even that we could turn into a whole game, I bet.
好的。现在我们回到了我们想要的地方。但是我们现在可以在哪里使用这些构建块呢?就像,我们之前做的都是一些非常小的程序。但即使这样,我敢打赌我们也可以把它变成一个完整的游戏。


Could we get like one volunteer to come on up? One volunteer? Everyone's looking down. OK. On the end here. Come on down. Yeah. All right. Round of applause for our one brave volunteer here.
我们能找一个志愿者上来吗?一个志愿者?大家都在低头。好的。在这里的尽头。来吧。是的。为我们这位勇敢的志愿者鼓掌。


[APPLAUSE]  [掌声]

All right. These Super Mario cookies are on the line for you, depending on how this goes. So I'm going to have you come over here. And in advance on Scratch's website, we have some pre-made games, one of them written by one of your predecessors, a former student, that they implemented this sort of "Whac-A-Mole" game.
好的。这些超级马里奥饼干就看你的表现了。所以我让你过来。在 Scratch 的网站上,我们有一些预制的游戏,其中一个是由你们的前任,一个前学生编写的,他们实现了这种“打地鼠”游戏。


So what you're about to see is the camera turn on on you. And you're going to see four moles above, below, left, and right. And using only your head-- up, down, left, right-- the goal is to whack a mole to get a point every time your head touches one of these sprites.
所以你即将看到的是摄像头开启。你会看到四个鼹鼠,分别在上、下、左、右。只需使用你的头部——上、下、左、右——目标是击打鼹鼠,每次你的头触碰到这些精灵时就能得分。


So you're about to see things get very interesting very quickly. But using these building blocks, just those simple blocks but have four sprites, not four cats but four moles in this case. We can actually turn these into actual games.
所以你将很快看到事情变得非常有趣。但使用这些构建块,仅仅是这些简单的块,但在这种情况下有四个精灵,不是四只猫,而是四只鼹鼠。我们实际上可以将这些变成真正的游戏。


[MUSIC PLAYING]  [音乐播放]

So here we go. Click Beginner. OK. And we just need you to center your head first.
好的,我们开始吧。点击初学者。好的。我们只需要你先把头部居中。


[MUSIC PLAYING]  [音乐播放]

[INDISTINCT CHATTER]  [模糊的交谈]

[CHUCKLES]  [轻声笑]

[MUSIC PLAYING]  [音乐播放]

Nice. Ten seconds.  很好。十秒钟。

[MUSIC PLAYING]  [音乐播放]

Nice. Two seconds.  好。两秒。

AUDIENCE: [LAUGHS]  观众:[笑]

DAVID J. MALAN: All right. A round of applause.
大卫·J·马兰:好吧。请大家鼓掌。


[APPLAUSE]  [掌声]

Thank you. You want to introduce yourself?
谢谢。你想自我介绍一下吗?


AUDIENCE: Hi, everybody. My name is [? Vanilla. ?] I'm a first year, and I'm going to be majoring in computer science and economics.
观众:大家好。我叫[? Vanilla. ?],我是一年级学生,我将主修计算机科学和经济学。


DAVID J. MALAN: Nice to meet you. Here we go. Thank you.
大卫·J·马兰:很高兴见到你。我们开始吧。谢谢。


AUDIENCE: Nice to meet you.
观众:很高兴见到你。


[APPLAUSE, CHEERING]  [掌声,欢呼]

DAVID J. MALAN: So we won't look at the code for that actual game. It was written by one of your predecessors. And you can see it online if you're curious. But you can think about now with our functions, conditionals, Boolean expressions, loops how you could kind of compose that kind of program.
大卫·J·马兰:所以我们不会查看那个实际游戏的代码。它是由你们的前任之一编写的。如果你感兴趣,可以在网上查看。但你可以考虑一下,现在利用我们的函数、条件语句、布尔表达式和循环,你如何能够组合出那种程序。


So odds are there was a loop that was just constantly listening for that kind of connectivity, or it was one of those extensions that was waiting for motion to go touch one of those sprites. Notice that there's these numbers up here.
所以很可能有一个循环在不断监听这种连接,或者它是那些扩展之一,等待动作去触摸其中一个精灵。注意这里有这些数字。


And we haven't talked about this yet. But just like in math, where you can have variables x, y, and z, in programming, you can create variables using other puzzle pieces in Scratch that just keep track of how many seconds are left, that keeps track of how many times her head hit one of the moles.
我们还没有谈论过这个。但就像在数学中,你可以有变量 x、y 和 z,在编程中,你可以使用 Scratch 中的其他拼图块创建变量,这些变量可以跟踪剩余的秒数,跟踪她的头撞到其中一个鼹鼠的次数。


And so you can implement the mechanics of games using very simple building blocks. But how do you go about building something that's more interesting and interactive like that? Let me go ahead and bring up, for instance, one of the very first things I wrote years ago when I was in graduate school and Scratch had just come out.
因此,您可以使用非常简单的构建块来实现游戏的机制。但是,您如何构建更有趣和互动的东西呢?让我举个例子,提到我在研究生院时写的第一件事情,那时 Scratch 刚刚发布。


I was taking a class at MIT's Media Lab. And they invited us to beta test-- that is, try out the software before it then became part of the world. And the game I made was this one here. Let me find the right version of "Oscartime."
我在麻省理工学院的媒体实验室上课。他们邀请我们进行 beta 测试——也就是说,在软件成为世界一部分之前试用它。我制作的游戏就是这个。让我找一下“Oscartime”的正确版本。


So "Oscartime" is a game that took me tens of hours probably in the end. It was a little bit addictive, but let me go ahead and full-screen it. And I won't play the whole game. But it looked a little something like this.
所以“Oscartime”是一个游戏,最终可能花了我几十个小时。它有点上瘾,但让我全屏显示一下。我不会玩整个游戏。但它看起来大致是这样的。


OSCAR: (SINGS) Oh, I love--
奥斯卡:(唱)哦,我爱——


DAVID J. MALAN: Where trash is falling from the sky. And I can click on it and drag it. And notice as I get close, the lid opens up like this. And if I let it keep falling, it goes in, and Oscar gives me a point.
大卫·J·马兰:垃圾从天而降。我可以点击并拖动它。注意,当我靠近时,盖子会像这样打开。如果我让它继续掉落,它就会进去,奥斯卡给我一个积分。


And I can do this again.
我可以再做一次。


OSCAR: If you really want to see something trashy--
OSCAR:如果你真的想看一些低俗的东西——


DAVID J. MALAN: All right. Here comes something else.
大卫·J·马兰:好的。接下来是其他内容。


OSCAR: I have here a sneaker that--
OSCAR:我这里有一双运动鞋——


DAVID J. MALAN: So now there's two pieces of trash.
大卫·J·马兰:所以现在有两件垃圾。


OSCAR: It's all full of holes, and the laces are--
OSCAR:它满是洞,鞋带是——


DAVID J. MALAN: And it just keeps going, and going, and going. And if we can lower the volume for just a moment, we'll let more trash fall. But to your comment earlier about one sprite or more sprites, that's what we're seeing here.
大卫·J·马兰:它就这样不断地继续,不断地继续。如果我们能稍微降低音量,我们会让更多的垃圾掉落。但对于你之前提到的一个精灵或多个精灵,这正是我们在这里看到的。


Even though our examples thus far are just one cat, one or two puzzle pieces, or a few puzzle pieces. Here is, I claim, a sprite. Here is another sprite. Here is another sprite. And by toggling among them in that bottom right-hand corner, I can just associate different puzzle pieces with each of these sprites.
尽管到目前为止我们的例子只有一只猫、一两个拼图块或几个拼图块。这里是,我声称,这是一个精灵。这里是另一个精灵。这里是另一个精灵。通过在右下角之间切换,我可以将不同的拼图块与这些精灵关联起来。


Now, I didn't start off by implementing this whole game. And in just a moment, if we can raise the volume a little bit, we'll see even more trash is falling. So this is what-- I hate this song now, like 10 plus hours listening to this song on loop just to get the timing right.
现在,我并不是一开始就实现了整个游戏。再过一会儿,如果我们能把音量调高一点,我们会看到更多的垃圾掉落。所以这就是——我现在讨厌这首歌,听了超过 10 个小时的循环,只是为了把节奏调整正确。


But it brings to life all of these different sprites. And if you play it again and again, it's always a little bit different because I'm using some randomness. So this newspaper doesn't always fall from there. Sometimes it's here. Sometimes it's here.
但它让所有这些不同的精灵栩栩如生。如果你一次又一次地玩,它总是会有一点不同,因为我使用了一些随机性。所以这张报纸并不总是从那里掉下来。有时它在这里。有时它在这里。


And so here, again, we have mechanics of a game where things are being perturbed a little, randomized a little bit to keep things more interesting. And let me go ahead.
因此,在这里,我们再次看到游戏的机制,其中事物被稍微扰动,随机化一点,以保持更有趣。让我继续。


OSCAR: (SINGS) I love trash.
OSCAR: (唱) 我爱垃圾。


DAVID J. MALAN: There we go. How. about raise a little volume? One more piece of trash. So a clock. It just goes on forever, this kind of game. But let's go ahead and consider. Let me close that. Let me go ahead and consider.
大卫·J·马兰:好了。再调高一点音量怎么样?再来一段废话。所以一个时钟。这个游戏就这样永无止境。但让我们继续考虑。让我关闭这个。让我继续考虑。


How I went about implementing that from the get-go. So I will stipulate-- let me open a few of these versions here-- that the very first thing I did was pretty much just implement the stage. Right?
我从一开始就如何实现这一点。所以我会说明——让我在这里打开几个版本——我所做的第一件事基本上就是实现这个阶段。对吧?


I was kind of procrastinating, so I poked around. I found the Sesame Street lamppost, and I dragged it into the world. And done. Version one is done. It didn't do anything, but at least I had the world sort of laid out. That is to say I took a baby step toward my goal.
我有点拖延,所以我四处看看。我找到了芝麻街的路灯,然后把它拖到了世界里。完成了。第一版完成了。它没有做任何事情,但至少我把世界大致布局好了。也就是说,我朝着我的目标迈出了第一步。


Then I started thinking about, all right, how do I bring the trash to life, even if it doesn't do much of anything else? And so I created another sprite by clicking the appropriate button in that bottom right-hand corner.
然后我开始思考,好的,我该如何让垃圾复活,即使它不做其他任何事情?于是我通过点击右下角的相应按钮创建了另一个精灵。


And I thought about, well, what do I want this trash to do? I want it to just kind of fall from the sky. And so what I did here was the following. If I go to this trash piece here or-- actually, sorry. Out of order.
我想了想,我想让这个垃圾做什么?我希望它能从天而降。所以我在这里做了以下事情。如果我去这个垃圾片段这里,或者——实际上,抱歉,顺序错了。


What I actually did first was I didn't even have the trash fall. If I play this game, the trash just stays there in the air. But I can do this. I can drag it, and as before, as I touch the other sprite, I get the trash can lid to go up. So how do I do that?
我实际上首先做的是我甚至没有让垃圾掉落。如果我玩这个游戏,垃圾就会停留在空中。但我可以这样做。我可以拖动它,和之前一样,当我触碰到另一个精灵时,我让垃圾桶的盖子抬起来。那么我该怎么做呢?


Well, let me click on Oscar down there, my first sprite. And here are the puzzle pieces via which I implemented this idea. I changed Oscar's costume, his appearance, to be just number one, which was one of the images I imported into the program.
好吧,让我点击下面的奥斯卡,我的第一个精灵。这里是我实现这个想法的拼图块。我把奥斯卡的服装和外观改成了数字一,这是我导入到程序中的图像之一。


And then I forever did this. If Oscar is touching the mouse pointer, then change Oscar's costume to number two, otherwise change it back to one. So it's super simple animation. I'm just toggling between lid up, lid down, lid up, lid down, but it kind of feels interactive.
然后我永远这样做。如果奥斯卡正在触摸鼠标指针,则将奥斯卡的服装更改为二号,否则将其更改回一号。所以这是一种超级简单的动画。我只是来回切换盖子打开、盖子关闭、盖子打开、盖子关闭,但这感觉有点互动。


And if I wanted to really make this pretty, I could have 30 different costumes where the lid is ever so slightly higher. Then it would look even more like a movie or fluid motion. But this was enough to get the job done, which is to say I didn't try to implement all of "Oscartime" together. I just took a second baby step toward my goal.
如果我想让这个更漂亮,我可以有 30 种不同的服装,每个盖子稍微高一点。那样看起来会更像电影或流动的动作。但这已经足够完成任务,也就是说我没有尝试将所有的“Oscartime”结合在一起。我只是朝我的目标迈出了第二个小步。


And then my next version of "Oscartime" might have looked a little something like this, where now the trash-- there's more going on here. Let's look at two of these blocks of code. The first thing I did was I enabled drag mode to draggable, and I had to Google to figure this out because otherwise it didn't let me drag the trash while playing the game.
然后我下一个版本的“Oscartime”可能看起来像这样,现在垃圾桶——这里发生了更多的事情。我们来看这两段代码。我做的第一件事是将拖动模式启用为可拖动,我不得不在谷歌上查找这个,因为否则在玩游戏时它不让我拖动垃圾桶。


But once I figured that out, I tell the trash to go to a random x-coordinate between 0 and 240 from left to right and then the y location 180 because I always want the trash falling from the sky. And then what do I do?
但一旦我弄明白了这一点,我就告诉垃圾在 0 到 240 之间的随机 x 坐标(从左到右)移动,然后 y 位置为 180,因为我总是希望垃圾从天空掉下来。那么我该怎么做呢?


I told the trash to forever change its y-coordinate, its vertical coordinate, by negative 1, negative 1, negative 1, one pixel at a time, which creates the illusion of it falling from the sky. But I needed to do one other thing, and let me scroll up.
我告诉垃圾永远将其 y 坐标,即垂直坐标,按负 1、负 1、负 1,每次一个像素地改变,这创造了它从天空中掉落的错觉。但我还需要做一件事,让我向上滚动。


Each of your sprites can have multiple programs, multiple scripts, so to speak, that are not attached to one another. They will happen in parallel for you. The second one is saying this. Forever if the trash is touching Oscar, what should it do?
每个精灵可以有多个程序,可以说有多个脚本,它们彼此之间并不相连。它们会并行运行。第二个脚本在说这个。如果垃圾碰到奥斯卡,它应该做什么?


Go to a completely different x and y location at the very top. Why? Well, as soon as I drag the trash over the Oscar, I want it to disappear as though it's going into the can. And I then want it to reappear at the top so more trash falls.
去一个完全不同的 x 和 y 位置,位于最顶部。为什么?因为当我把垃圾拖到奥斯卡上时,我希望它消失,就像进入垃圾桶一样。然后我希望它在顶部重新出现,这样更多的垃圾就可以掉下来了。


So I just thought about what would it mean for the trash to go into the trash can. Well, who cares? What really matters to the human user is that it just disappears and teleports elsewhere. And so that's the idea I implemented here.
所以我刚刚想到了垃圾放进垃圾桶意味着什么。好吧,谁在乎呢?对人类用户来说,真正重要的是它只是消失并传送到其他地方。因此,这就是我在这里实现的想法。


So if you can reduce intuitive ideas to just basic building blocks like this, you can start to make things much more interactive. And lastly, if I look at this version here, you'll see that we've combined these. And so indeed, if I actually go ahead and play this now, not only is it falling. I can let it fall right on top of Oscar and watch it disappear.
所以如果你能将直观的想法简化为像这样的基本构建块,你就可以开始让事物变得更加互动。最后,如果我查看这里的这个版本,你会看到我们将这些结合在一起。因此,实际上,如果我现在去播放这个,不仅它在下落。我可以让它直接落在奥斯卡身上,看看它是如何消失的。


But notice Oscar doesn't pop out yet because that was the fourth version and then the fifth version. And then I added the annoying music and so forth but sort of composed this program step by step by step so as to accomplish my larger goal.
但请注意,奥斯卡还没有出现,因为那是第四个版本,然后是第五个版本。接着我添加了烦人的音乐等等,但我一步一步地构建这个程序,以实现我的更大目标。


And this is going to be true of all of the code you write, be it in Scratch, or C, or Python, or in the like, trying to come up with-- or rather, trying to reduce your ideas, your grand vision to just baby steps, building blocks so that you start with version one, and maybe you submit version 10 or 20.
这将适用于你编写的所有代码,无论是 Scratch、C 还是 Python,或者类似的语言,试图将你的想法、宏伟愿景简化为小步骤、构建模块,以便你从版本一开始,可能最终提交版本 10 或 20。


But you don't try to implement version 10 or 20 at the get-go. You take those incremental steps. All right. How about one other? Well, let me propose this. Let me go ahead and open three games that represent one that your predecessors also implemented, which looks a little something like this in version zero.
但你不会一开始就尝试实现版本 10 或 20。你会采取这些渐进的步骤。好吧。还有其他的呢?让我提议这个。让我继续打开三个游戏,它们代表了你们的前任也实现过的一个,版本零看起来有点像这样。


Suppose I wanted to implement a game that simply has these kinds of mechanics. I'm touching my arrow keys on my keyboard-- up, down, left, and right. And I'm moving the Harvard logo. Let me zoom in a bit. So if I hit the up arrow, the Harvard shield goes up.
假设我想实现一个简单具有这些机制的游戏。我正在按键盘上的箭头键——上、下、左和右。我正在移动哈佛的标志。让我放大一点。所以如果我按上箭头,哈佛盾牌就会上升。


If I hit the down arrow, the shield goes down. If I go all the way to the left, it goes left until it hits the wall and same thing on the right. So this is like the beginnings of a game or a beginning of a maze, something like that. Well, how might I implement this?
如果我按下箭头向下,护盾就会下降。如果我一直向左走,它会向左移动,直到碰到墙壁,右边也是一样。所以这就像是一个游戏的开始或迷宫的开始,类似这样的东西。那么,我该如何实现这个呢?


Well, let me look inside this one. And there's a lot going on, but, again, I sort of took simple steps. Notice that I've got three sprites-- a left wall, which is just a straight line, the right wall, which is a straight line. And just intuitively, why did I implement those as sprites?
好吧,让我看看这个里面。里面有很多内容,但我还是采取了简单的步骤。注意我有三个精灵——左墙,它只是一个直线,右墙,也是一个直线。直观地说,我为什么要将它们实现为精灵?


Why do they need to exist as entities themselves? Yeah, in front.
他们为什么需要作为实体存在?是的,在前面。


AUDIENCE: [INAUDIBLE]  观众:[听不清]

DAVID J. MALAN: Yeah. I want it to interact with the shield. So I need to be able to ask that Boolean expression, like, not touching mouse pointer but touching shield, for instance, a different type of yes/no question. And so what is the code for the shield actually doing?
大卫·J·马兰:是的。我希望它与护盾互动。所以我需要能够询问那个布尔表达式,比如,不触碰鼠标指针但触碰护盾,例如,另一种类型的是/否问题。那么护盾的代码实际上在做什么?


Well, there's a lot of duplication, and let me focus on the abstraction first. Notice that I've got this one green flag clicked. I want the shield to go dead center, 0 comma 0, and then forever listen for the human's keyboard, and feel for the wall.
好吧,这里有很多重复的内容,让我先专注于抽象。注意我点击了这个绿色旗帜。我希望盾牌位于正中心,0,0,然后永远监听人类的键盘,并感知墙壁。


So I decided to implement my own pink puzzle pieces that implement the two separate ideas. One, just listen for the keyboard-- up, down, left, right-- and then do something. And then feel for walls is this idea of whenever I go up, down, left, or right, if I touch a wall, I need to stop doing whatever the keystrokes are telling me to do.
所以我决定实现我自己的粉色拼图块,来实现这两个独立的想法。一个是,只需监听键盘——上、下、左、右——然后做一些事情。另一个是感知墙壁的想法,即每当我向上、向下、向左或向右移动时,如果我碰到墙壁,我需要停止执行键击所指示的任何操作。


So now if we dive into those implementation details, listen and feel are abstractions, custom puzzle pieces. Let's look at the implementation details. Well, here's the keyboard. If the key arrow up is pressed, change y by 1. If key down arrow is pressed, change y by negative 1.
现在,如果我们深入这些实现细节,监听和感知是抽象的,自定义的拼图块。让我们看看实现细节。好吧,这里是键盘。如果按下向上箭头键,y 增加 1。如果按下向下箭头键,y 减少 1。


And you can probably see where this is going. Right arrow is x by 1. Left arrow is x by negative 1, and that's sort of all that's involved with up, down, left, right. But wait a minute. Why is there no loop in this listen for keyboard puzzle piece?
你可能已经看到了这将如何发展。右箭头是 x 乘以 1。左箭头是 x 乘以 -1,这就是上下左右所涉及的全部内容。但等一下。为什么在这个监听键盘的拼图中没有循环呢?


I needed a loop last time so it constantly works.
我上次需要一个循环,这样它才能持续工作。


AUDIENCE: [INAUDIBLE]  观众:[听不清]

DAVID J. MALAN: Exactly. I put the loop in my main part of my program up top so something is telling me to keep listening again and again. So it's got to be somewhere, but it doesn't have to be in the actual implementation. And lastly, how about this feel for walls?
大卫·J·马兰:没错。我把循环放在程序的主部分上面,这样就有东西在告诉我不断地监听。所以它必须在某个地方,但不一定要在实际的实现中。最后,这种对墙壁的感觉怎么样?


Well, if touching left wall, which is just another sprite, change x by 1. So that is to say if I'm touching the left wall, I want to bounce it back the other direction and same thing on the right wall.
好吧,如果碰到左墙,那只是另一个精灵,将 x 改变为 1。也就是说,如果我碰到左墙,我想要将其反弹回另一个方向,右墙也是同样的情况。


If I'm touching the right wall, I want to bounce it back to the left, which effectively means that even if the human's hitting the key, it's like fighting with this code, but it's never going to go through the wall based on that math. It's going to stop it right there.
如果我碰到了右侧的墙,我想把它反弹回左侧,这实际上意味着即使人类在按键,这就像在与这段代码作斗争,但根据这个数学原理,它永远不会穿过墙壁。它会在那儿停下来。


All right. Let's add something else to the mix. Suppose I want the game to change to be a little something like this, where Yale is some kind of block in between me and the exit. So some dramatic race here. OK. I just got by, but the Yale logo doesn't seem to be doing all that much except bouncing.
好的。让我们再添加一些内容。假设我想让游戏变成这样,耶鲁大学是我和出口之间的一个障碍。所以这里有一场戏剧性的竞赛。好的。我刚刚过去,但耶鲁大学的标志似乎除了弹跳之外并没有做太多事情。


So I'm guessing there's a loop, maybe a conditional checking for those walls too. So let's go ahead and zoom out, see inside. Let's not worry about Harvard because it's pretty much the same. Let's look at the Yale puzzle pieces.
所以我猜这里有一个循环,也许还有一个条件检查那些墙。我们先放大一下,看看里面。我们不必担心哈佛,因为它基本上是一样的。我们来看看耶鲁的拼图块。


And sure enough, go to the middle. 0 comma zero. Point in direction 90, so point horizontally on the grid. And then if touching left wall or touching right wall-- I'm kind of cheating this time, but cleverly, just spin around and do 180 so you effectively bounce off the wall.
而且果然,去中间。0,0。朝 90 度方向指向,所以在网格上水平指向。然后如果碰到左墙或碰到右墙——这次我有点作弊,但聪明地,只需旋转并做 180 度,这样你就有效地从墙上反弹。


This just tightened up my code. I don't need to do the negative 1 or the plus 1. I just say bounce in this form of code, otherwise just constantly move one step. Now, if this is a game where Yale is supposed to be better and faster, well, let's change the 1 to 5.
这只是让我的代码更紧凑了。我不需要做负 1 或加 1。我只需在这种代码形式中说“反弹”,否则就不断移动一步。现在,如果这是一个耶鲁应该更好更快的游戏,那么我们就把 1 改成 5。


Move 5 pixels at a time. Let's move it 10 back and forth. Let's maybe 100. Uh-oh. So what just happened? That is a bug, which we can avoid by just not doing that. But why did it break out of the wall? Yeah.
每次移动 5 个像素。我们来回移动 10 次。也许移动 100 次。哎呀。那么刚刚发生了什么?这是一个错误,我们可以通过不这样做来避免。但为什么它会突破墙壁?是的。


AUDIENCE: [INAUDIBLE]. At first it was [INAUDIBLE].
观众:[听不清]。起初是[听不清]。


DAVID J. MALAN: Exactly. Because I'm doing 100 steps at a time, I'm never actually touching the other sprite because I'm sort of stepping way over it. So there's never a moment where they're actually touching.
大卫·J·马兰:没错。因为我一次走 100 步,所以我实际上从未接触到另一个精灵,因为我有点越过了它。因此,它们之间从未有过接触的时刻。


So previously, I was just getting lucky by doing fewer steps because it's gradually going over the wall, which gives me just enough time to detect as much. So I would have to kind of tinker, and he'll handle this a little bit differently.
所以之前,我只是通过减少步骤来运气好,因为它逐渐越过墙壁,这给了我足够的时间去检测尽可能多的内容。所以我需要稍微调整一下,他会以不同的方式处理这个。


So it's a bug if it's too fast. But at least if I keep it slow and reasonable the math actually does work out, so long as it starts again in the middle. Well, let's do one final flourish here, whereby let's bring MIT into the mix. Right? They're super smart, so maybe they can kind of track us and follow wherever I'm going.
所以如果太快的话就是个 bug。但至少如果我保持慢而合理,数学实际上是可行的,只要它在中间重新开始。好吧,让我们在这里做一个最后的华丽动作,把 MIT 也带进来。对吧?他们超级聪明,所以也许他们可以跟踪我们,跟随我去的任何地方。


So how might this work? All right. So nothing happens yet because we haven't finished composing the game. And notice here-- OK. Now MIT is struggling. It's kind of twitching there because it's going just above, and below, and then above, and below.
所以这可能是怎么回事呢?好的。现在还没有发生什么,因为我们还没有完成游戏的构建。请注意这里——好的。现在麻省理工学院正在挣扎。它有点抖动,因为它在上下波动。


So we could fix that too if we want. But that's just a function of my math, one pixel at a time. Let me open up this one, see inside, and click on MIT. And it doesn't take much to implement MIT, it seems.
所以我们也可以修复这个,如果我们想的话。但这只是我数学的一个函数,一次一个像素。让我打开这个,看看里面,然后点击 MIT。实现 MIT 似乎并不需要太多。


So go to random position, forever point towards the Harvard logo outline, AKA the shield, and then move one step. So if I wanted to make MIT even smarter, even faster, what do I change here?
所以去随机位置,永远指向哈佛标志轮廓,也就是盾牌,然后移动一步。那么如果我想让麻省理工学院变得更聪明、更快,我该在这里改变什么?


AUDIENCE: [INAUDIBLE]  观众:[听不清]

DAVID J. MALAN: Yeah. Change one step to two steps to double their speed or five steps, 10 steps, or 100 steps. And the game is going to be over like that. But that's all it takes to now make these kinds of elements.
大卫·J·马兰:是的。将一步改为两步以加倍他们的速度,或者五步、十步,甚至一百步。游戏就会这样结束。但这就是现在制作这些元素所需的一切。


So if you are a game player on your phone, or consoles, or computer, or whatever, if you think about almost any game, you can probably now start to think about how they implemented those mechanics because it's just being reduced to functions, conditionals, loops, variables, and the like in this case.
所以如果你是手机、游戏机、电脑或其他设备上的游戏玩家,如果你考虑几乎任何游戏,你现在可能可以开始思考他们是如何实现这些机制的,因为在这种情况下,它们只是被简化为函数、条件语句、循环、变量等。


So let's go ahead here and have maybe one final volunteer. We've got one more bag of Oreos here. OK. That was super fast. Do you want to come on up? All right. Brave volunteer. Come on up.
那么我们继续,来找最后一位志愿者吧。我们这里还有一袋奥利奥。好的。真快。你想上来吗?好的。勇敢的志愿者,快上来。


[APPLAUSE]  [掌声]

All right. Let me find the full-fledged version of this that one of your predecessors made. And let me get the right one. OK. Here we go. We'll see some instructions on the screen in just a moment.
好的。让我找到你们的前任制作的完整版本。让我找到正确的版本。好的。我们开始吧。稍后屏幕上会显示一些说明。


And when we hit Play, you'll see that the mechanics are all combined now into one full-fledged game. But first, an introduction. It's on.
当我们点击播放时,你会看到所有的机制现在都结合成一个完整的游戏。但首先,介绍一下。开始吧。


SAM: Hi, everyone. I'm Sam. I live in [INAUDIBLE]. I'm a freshman, and I'm from Nepal.
SAM:大家好,我是 Sam。我住在[无法听清]。我是一名大一新生,来自尼泊尔。


DAVID J. MALAN: All right. Welcome to the stage.
大卫·J·马兰:好的。欢迎上台。


[APPLAUSE]  [掌声]

SAM: Thank you.  SAM: 谢谢。

DAVID J. MALAN: All right. So here we go. Yep. Go ahead and click the green flag.
大卫·J·马兰:好的。那么我们开始吧。是的。请点击绿色的旗帜。


[MUSIC PLAYING]  [音乐播放]

[MC HAMMER, "U CAN'T TOUCH THIS"] You can't touch this.
[MC HAMMER, "U CAN'T TOUCH THIS"] 你无法触碰这个。


DAVID J. MALAN: You see the grid is just bigger this time.
大卫·J·马兰:你看,这次网格只是更大了。


(SINGING) You can't touch this.
(唱歌)你无法触碰这个。


DAVID J. MALAN: Nice. Now there's that Yale element.
大卫·J·马兰:很好。现在有了耶鲁的元素。


(SINGING) You can't touch this. My, my, my my music hits me so hard. Makes me say, oh, my Lord. Thank you for blessing me with a mind to rhyme and two hyped feet. It feels good when you know you're down, a super dope homeboy from the Oaktown.
(唱歌)你无法触碰这个。我的音乐打击得我如此强烈。让我说,哦,我的主。感谢你赐予我一个押韵的头脑和两只充满活力的脚。当你知道自己很棒时,这感觉真好,一个来自奥克镇的超级酷兄弟。


DAVID J. MALAN: OK. Third Yale. All started at slightly different positions.
大卫·J·马兰:好的。第三个耶鲁。所有人都从稍微不同的位置开始。


(SINGING) I told you, homeboy. You can't touch this.
(唱歌)我告诉过你,兄弟。你碰不了这个。


DAVID J. MALAN: Nice. All right. There's MIT.
大卫·J·马兰:很好。好的。那是麻省理工学院。


(SINGING) And ya know you can't touch this.
(唱歌)你知道你碰不到这个。


[CHEERING, APPLAUSE]  [欢呼,掌声]

DAVID J. MALAN: Oh.
大卫·J·马兰:哦。


(SINGING) You can't touch this. Yo. Let me bust the funky lyrics.
(唱歌)你不能碰这个。哟。让我来唱这段酷炫的歌词。


DAVID J. MALAN: Gotta go fast. Oh. No. Oh. [CHUCKLES]
大卫·J·马兰:得快点。哦。不。哦。[笑]


(SINGING) Cold on a mission, so fall on back. Let them know that you're too much, and this is a beat they can't touch.
(唱)任务艰巨,所以退后。让他们知道你太出色了,这是一段他们无法触及的节拍。


DAVID J. MALAN: Nice.
大卫·J·马兰:不错。


[EXCLAIMING]  [惊叹]

(SINGING) You can't touch this.
(唱歌)你无法触碰这个。


DAVID J. MALAN: No more walls but two MITs.
大卫·J·马兰:不再有墙,只有两个麻省理工学院。


(SINGING) Yo, sound the bell. School's in, sucker.
(唱歌)哟,响铃了。上课了,傻瓜。


DAVID J. MALAN: Princeton now.
大卫·J·马兰:普林斯顿现在。


(SINGING) Give me a song or rhythm. Making them sweat, that's what I'm giving them. Now, they know you talk about the Hammer when you're talkin' 'bout a show that's hyped and tight. Singers are sweatin', so pass them a wipe or a tape to learn.
(唱歌)给我一首歌或节奏。让他们出汗,这就是我给他们的。现在,他们知道当你谈论一个被炒作和紧凑的表演时,你在谈论锤子。歌手们在出汗,所以给他们递一块擦拭布或一卷胶带来学习。


DAVID J. MALAN: Second to last level.
DAVID J. MALAN: 倒数第二级。


(SINGING) The chart's legit. Either work hard, or you might as well quit. That's word because you know you can't touch this. You can't touch this. Break it down.
(唱歌)这个图表是真的。要么努力工作,要么你不如放弃。这是事实,因为你知道你碰不到这个。你碰不到这个。分解一下。


DAVID J. MALAN: All right. Clear. There we go. Nice. Oh, oh. Oh. Few more lives. Oh.
大卫·J·马兰:好的。清楚。来吧。不错。哦,哦。哦。再来几条命。哦。


(SINGING) Stop. Hammer time.
(唱)停。锤子时间。


DAVID J. MALAN: Here we go. There we go.
大卫·J·马兰:开始吧。好了。


[EXCLAIMING]  [惊叹]

All right. Couple more tries. Yes! Oh, no.
好的。再试几次。是的!哦,不。


(SINGING) This is it for a winner. Dance to this, and you're gonna get thinner. Now move, slide your rump.
(唱歌)这就是赢家的节奏。跟着这个舞动,你会变得更瘦。现在动起来,滑动你的臀部。


DAVID J. MALAN: Starts getting stressful.
大卫·J·马兰:开始变得紧张。


(SINGING) Just for a minute, let's all do the bump. Bump, bump, bump. Yeah. You can't touch this. Look, man. You can't touch this. You'll probably get hyped, boy.
(唱歌)就一分钟,让我们都来跳舞。碰,碰,碰。是的。你碰不到这个。看,伙计。你碰不到这个。你可能会兴奋,伙计。


DAVID J. MALAN: One more try.
大卫·J·马兰:再试一次。


(SINGING) You can't touch this. Ring the bell. School's back in.
(唱歌)你不能碰这个。铃声响了。学校又开学了。


DAVID J. MALAN: All right. A round of applause, nonetheless.
大卫·J·马兰:好吧,还是给大家鼓掌。


[APPLAUSE]  [掌声]

Nicely done. Thank you. So as you might have noticed if your eyes started to wander to the light bulbs here, there's actually 64 of these light bulbs. And I'm wondering if you divide 64 by 8, that's 8 bytes of light bulbs.
做得很好。谢谢你。所以你可能注意到了,如果你的目光开始游移到这里的灯泡上,实际上有 64 个这样的灯泡。我在想,如果你把 64 除以 8,那就是 8 个字节的灯泡。


And we now have some Unicode in our vocabulary. So might very well be the case that we've been spelling something out on the stage here for you all of this time. But before we adjourn for cake to be served in the Transept, allow me to introduce some of CS50's human friends, the Harvard Krokodiloes and the Radcliffe Pitches, to give us this exciting ending, "This is CS50."
我们现在的词汇中有了一些 Unicode。因此,很可能我们一直在这里为大家拼写某些内容。但在我们前往横廊享用蛋糕之前,请允许我介绍一些 CS50 的人类朋友,哈佛 Krokodiloes 和 Radcliffe Pitches,来为我们带来这个激动人心的结尾,“这就是 CS50。”


[APPLAUSE, CHEERING]  [掌声,欢呼]

[HARMONICA NOTE]  [口琴音符]

[VOCALIZING]  [发声]

SPEAKER: (SINGING) There's a certain someone who I'm indebted to. And since the old [? BNC ?] has 50, I have this friend for you. A two, three, four.
发言人:(唱歌)有一个人我欠他人情。既然老[? BNC ?]已经 50 岁,我就为你准备了这个朋友。二,三,四。


[VOCALIZING]  [发声]

Oh, rubber ducky, you're the one. You make [INAUDIBLE] so much fun. Rubber ducky, I'm awfully fond of you. [SCATTING] Rubber ducky, you make me smile, and you help my code compile.
哦,橡皮鸭子,你是唯一。你让[听不清]变得如此有趣。橡皮鸭子,我非常喜欢你。[即兴演唱] 橡皮鸭子,你让我微笑,你帮助我的代码编译。


Rubber ducky, you're my very best friend. It's true. When I'm at a standstill, your debugging abilities stun me. When I'm at the end of my rope, you just snap, and my code's up and running. Rubber ducky, you're so fine, and I'm lucky that you're mine.
橡皮鸭,你是我最好的朋友。是真的。当我停滞不前时,你的调试能力让我震惊。当我快要崩溃时,你只需一声响,我的代码就能运行。橡皮鸭,你真棒,我很幸运你是我的。


Rubber ducky, you're my very best friend. It's true. You're my best friend. It's true. Rubber ducky, I'm awfully fond of you.
橡皮鸭,你是我最好的朋友。是真的。你是我最好的朋友。是真的。橡皮鸭,我非常喜欢你。


[CHEERING, APPLAUSE]  [欢呼,掌声]

SPEAKER: Good afternoon, CS50. We are the Harvard Krokodiloes, Harvard's oldest a cappella group, founded way back in 1946 at the historic Hasty Pudding Club. We'd love to make a big thank you to CS50 staff and to David Malan for having us perform here at Sanders Theater.
发言人:下午好,CS50。我们是哈佛 Krokodiloes,哈佛最古老的无伴奏合唱团,成立于 1946 年,地点在历史悠久的 Hasty Pudding Club。我们非常感谢 CS50 的工作人员和 David Malan 让我们在桑德斯剧院演出。


And you enjoyed this performance, please come audition for us this weekend at Farkas Hall.
如果您喜欢这个表演,请在这个周末来 Farkas 大厅为我们试镜。


[CHEERING, APPLAUSE]  [欢呼,掌声]

SPEAKER: Hello, everyone. We are some of the Radcliffe Pitches, and we are also hosting auditions this weekend. You can find more information at our Instagram, @radcliffepitches. Now, let me tell you a little bit about just about a year ago today, when I was sitting in your very seats on my first day of CS50 lecture.
发言人:大家好。我们是 Radcliffe Pitches 的一部分,这个周末我们也在举办试音。您可以在我们的 Instagram 上找到更多信息,@radcliffepitches。现在,让我告诉您大约一年前的今天,当我坐在你们的座位上,参加 CS50 讲座的第一天。


And this is just about how I was feeling.
这只是关于我当时的感受。


[HARMONICA NOTE]  [口琴音符]

[VOCALIZING]  [发声]

(SINGING) It's the first day of class, and I'm brand new to code. Is this for me? So many people around. Can I get through the workload? But it's my dream. I tend to stick to English, not science. But my [INAUDIBLE] friends told me to try this.
(唱歌)今天是上课的第一天,我对编程还是个新手。这适合我吗?周围有这么多人。我能应对这份工作量吗?但这是我的梦想。我倾向于坚持英语,而不是科学。但我的[听不清]朋友告诉我试试这个。


Hey, dancing robot dog, you kind of look like you have your life together, I guess. I really need some advice.
嘿,跳舞的机器人狗,你看起来好像生活得不错,我想。我真的需要一些建议。


(ALL SINGING) We know you're feeling unsure, but this is really the right call. In CS50, you'll meet new friends, get free food. You'll be all set for this fall in CS50.
(所有人合唱)我们知道你感到不确定,但这真的是正确的选择。在 CS50 中,你会结识新朋友,获得免费食物。你将在这个秋季为 CS50 做好准备。


You have a thousand TAs who will help you. You'll get cupcakes, duckies, Chinese food. And you can always take this class and set aside.
你有一千个助教会帮助你。你会得到杯子蛋糕、小鸭子、中餐。你总是可以参加这门课并留出时间。


SPEAKER: This is CS50. Fist bump.
发言人:这是 CS50。击掌。


[LAUGHTER]  [笑声]

[APPLAUSE, CHEERING]  [掌声,欢呼]

DAVID J. MALAN: Thank you to the Kroks. Thank you to the Pitches. Cake is now served. Come on up to say hi if you'd like or meet Spot. See you next time.
大卫·J·马兰:感谢克罗克一家。感谢皮奇一家。蛋糕现在可以享用了。如果你愿意,可以上来打个招呼或者见见斯波特。下次见。


[APPLAUSE, CHEERING]  [掌声,欢呼]

[MUSIC PLAYING]  [音乐播放]