|
| 1 | +--- |
| 2 | +id: 3 |
| 3 | +title: "[译] [101] 我们如何与计算机对话" |
| 4 | +--- |
| 5 | + |
| 6 | +## 1.1 How we talk to computers |
| 7 | + |
| 8 | +## 1.1 我们如何与计算机对话 |
| 9 | + |
| 10 | +Would you be happy if we started by asking you to read and understand this? |
| 11 | + |
| 12 | +如果我们开始时请您阅读并理解以下内容,您是否会感到满意? |
| 13 | + |
| 14 | +```assembly |
| 15 | +section .text |
| 16 | +global _start |
| 17 | +_start: |
| 18 | + mov ecx, 10 |
| 19 | + mov eax, '0' |
| 20 | + l1: |
| 21 | + mov [num], eax |
| 22 | + mov eax, 4 |
| 23 | + mov ebx, 1 |
| 24 | + push ecx |
| 25 | + mov ecx, num |
| 26 | + mov edx, 1 |
| 27 | + int 0x80 |
| 28 | + mov eax, [num] |
| 29 | + inc eax |
| 30 | + pop ecx |
| 31 | + loop l1 |
| 32 | + mov eax, 1 |
| 33 | + int 0x80 |
| 34 | +section .bss |
| 35 | + num resb 1 |
| 36 | +``` |
| 37 | + |
| 38 | +That monstrosity prints out the numbers from 0 to 9. It's written using code in assembly language, a low-level programming language. Low-level programming languages, as you can see, are very far from languages that humans can easily read and write. They’re designed for computers, not humans. |
| 39 | + |
| 40 | +这段 “鬼画符” 能够输出从 0 到 9 的数字。它使用的是汇编语言编写的,这是一种低级编程语言。正如您所见,低级编程语言与人们能够轻松读写的语言大相径庭。它们主要为计算机而设计,并非面向人类。 |
| 41 | + |
| 42 | +No one wants to write programs like that. But, especially in the past, it was sometimes necessary. Programmers could use it to do exactly what they wanted the computer to do, down to individual instructions. This level of control was needed in order to squeeze every bit of performance out of underpowered computers. For example, the most speed-critical pieces of 1990s computer games such as Doom and Quake were written in assembly language like the code above. It simply wouldn't have been possible to make those games otherwise. |
| 43 | + |
| 44 | +没人愿意像这样写代码。但特别是在早期,这有时候是不得已而为之的。程序员能够借此准确地控制计算机的每一条指令,实现他们所期望的确切操作。这种精确的控制对于充分利用计算能力有限的电脑以获得最大性能是必不可少的。举个例子,像《毁灭战士》和《雷神之锤》这样的90年代关键游戏速度敏感部分,就是使用类似上面的汇编语言编写的。否则,制作这些游戏将是不可能的。 |
| 45 | + |
| 46 | + |
| 47 | +#### Making it a little easier |
| 48 | + |
| 49 | +#### 让难度降低一点儿 |
| 50 | + |
| 51 | +Okay, no more of that. Let's move on. Would you be happier with this? |
| 52 | + |
| 53 | +好的,不再是那样了。让我们继续。这会让你更满意吗? |
| 54 | + |
| 55 | + |
| 56 | +```python |
| 57 | +for num in range(0, 9): |
| 58 | + print(num) |
| 59 | +``` |
| 60 | + |
| 61 | +This, by contrast, is code in the Python language and is what many programmers use these days. Unlike assembly language, which is a low-level language, Python is considered a high-level language because it's much closer to natural language. Even though you don't know about Python code yet, you might be able to guess what this program is trying to do. The first line looks like it's doing something with the range of numbers from 0 to 9. The second line is printing something. It may not be too hard for you to believe that this program, just like the assembly language monstrosity, is supposed to print the numbers from 0 to 9. Unfortunately, something is wrong with it. |
| 62 | + |
| 63 | +与此相对,这段代码是用 Python 语言编写的,是当前许多程序员所采用的。不同于低级语言的汇编语言,Python 被看作是一种高级语言,因为它更贴近自然语言。即便你对 Python 代码尚无了解,你或许能猜出这段程序的目的。第一行似乎在处理一个从 0 到 9 的数字范围。第二行在进行打印操作。不难理解,这个程序的目的,正如那段汇编语言代码一样,是要打印出从 0 到 9 的数字。遗憾的是,程序中存在一些错误。 |
| 64 | + |
| 65 | +While this code is closer to English, it isn’t English. It's a programming language that, like assembly language, has specific rules. As in the code above, misunderstanding the details of those rules can result in a broken program. |
| 66 | + |
| 67 | +尽管这种代码更贴近英语,但它并非英语。它是一种拥有特定规则的编程语言,类似于汇编语言。如上所述,对这些规则的细节理解不当可能会导致程序运行错误。 |
| 68 | + |
| 69 | +The holy grail of communicating with a computer is to do so in a natural language such as English. We've been talking to computers using various programming languages over the past 70 years not because we want to, but because we have to. Computers were simply not powerful enough for the vagaries and idiosyncrasies of a language like English. Our programming languages improved—-from symbol soup assembly language to Python, for example—-but they are still computer languages, not natural languages. |
| 70 | + |
| 71 | +与计算机沟通的终极目标是能够使用如英语这样的自然语言进行。在过去的 70 年中,我们之所以使用各种编程语言与计算机对话,并非出于愿望,而是迫于无奈。计算机的计算能力不足以应对英语这样语言的复杂性和特异性。我们的编程语言虽然有所进步——例如,从充满符号的汇编语言发展到了 Python,但它们依旧是计算机语言,并非自然语言。 |
| 72 | + |
| 73 | +This is changing. |
| 74 | + |
| 75 | +然而,这种情况正在发生变化。 |
| 76 | + |
| 77 | + |
| 78 | +#### Making it a lot easier |
| 79 | + |
| 80 | +#### 让难度降低一大截 |
| 81 | + |
| 82 | +Using an AI assistant, we can now ask for what we want in English and have the computer code written for us in response. To get a correct Python program that does actually print the numbers from 0 to 9, we can ask our AI assistant (Copilot) in normal English language like this: |
| 83 | + |
| 84 | +借助 AI 助手,我们现在可以用英文明确提出我们的需求,并让计算机相应地为我们编写代码。为了得到一个实际上能打印出 0 到 9 数字的正确 Python 程序,我们可以这样用日常英语向我们的 AI 助手(GitHub Copilot)提出指令: |
| 85 | + |
| 86 | +```python |
| 87 | +# Output the numbers from 0 to 9 |
| 88 | +``` |
| 89 | + |
| 90 | +```python |
| 91 | +# 打印出 0 到 9 的数字 |
| 92 | +``` |
| 93 | + |
| 94 | +Copilot might respond to this prompt by generating something like this: |
| 95 | + |
| 96 | +对此,GitHub Copilot 可能会根据这一指令生成如下代码: |
| 97 | + |
| 98 | +```python |
| 99 | +for i in range(10): |
| 100 | + print(i) |
| 101 | +``` |
| 102 | + |
| 103 | +Unlike the example we showed you before, this piece of Python code actually works! |
| 104 | + |
| 105 | +不同于我们之前展示的示例,这段 Python 代码确实能够正常运行! |
| 106 | + |
| 107 | +AI coding assistants can be used to help people write code. In this book, we will learn how to use Copilot to write code for us. We will ask for what we want in English, and we will get the code back in Python. |
| 108 | + |
| 109 | +AI 编程助手能够帮助人们编写代码。在本书中,我们会探索如何利用 GitHub Copilot 来编写代码。我们只需用英语描述我们的需求,便能收到用 Python 编写的代码作为回应。 |
| 110 | + |
| 111 | +More than that, we'll be able to use Copilot as a seamless part of our workflow. Without tools like Copilot, programmers routinely have two windows open: the one where they write code and the other where they ask on Google how to write code. This second window would have Google search results, Python documentation, or forums of programmers talking about how to write code to solve that particular problem. They're often pasting code from these results into their code, then tweaking it slightly for their context, trying alternatives, and so on. This has become a way of life for programmers—but you can imagine the inefficiency here. By some estimates, up to 35% of programmer’s time may be spent searching for code \[1\], and much of the code that is found is not readily usable. This will be much improved with Copilot helping us write our code. |
| 112 | + |
| 113 | +更进一步,我们将能够无缝集成 GitHub Copilot 到我们的工作流程中。在没有 GitHub Copilot 这类工具的情况下,程序员通常需要同时打开两个窗口:一个用于编写代码,另一个用于在 Google 上查询编码方法。这第二个窗口可能充斥着 Google 搜索结果、Python 文档或是程序员论坛上讨论如何解决特定编码问题的帖子。他们经常需要从这些结果中复制代码到自己的项目中,随后进行适当的调整以符合自己的应用场景,尝试不同的解决方案等。这已经成为了程序员生活的一部分——但你可以想象这样做的低效率。据估计,程序员多达 35% 的时间可能花在搜索代码上\[1\],而且很多找到的代码并不是可以立即使用的。有了 GitHub Copilot 协助我们编写代码,这种情况将得到显著改善。 |
0 commit comments