Description
1.1 How we talk to computers
1.1 我们如何与计算机对话
Would you be happy if we started by asking you to read and understand the code below? 1
如果我们一上来就要求你读懂下面这段代码,你能接受吗?[脚注1]
1 Based on code from https://draftsbook.com/part-7-conditions-and-loop-uses-in-assembly-language/.
[脚注1] 这段代码摘自 https://draftsbook.com/part-7-conditions-and-loop-uses-in-assembly-language/ 。
section .text
global _start
_start:
mov ecx, 10
mov eax, '0'
l1:
mov [num], eax
mov eax, 4
mov ebx, 1
push ecx
mov ecx, num
mov edx, 1
int 0x80
mov eax, [num]
inc eax
pop ecx
loop l1
mov eax, 1
int 0x80
section .bss
num resb 1
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 not languages that humans can easily read and write. They’re designed for computers, not humans.
这段 “天书” 会打印出 0~9 的数字。它是用汇编语言编写的,汇编是一种低级编程语言。如你所见,低级编程语言与我们日常读写的语言相去甚远。它们主要是为计算机而设计的,并非面向人类。
No one wants to write programs like that, but especially in the past, it was sometimes necessary. Programmers could use it to define exactly what they wanted the computer to do, down to individual instructions. This level of control was needed 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 previous code example. It simply wouldn’t have been possible to make those games otherwise.
没人愿意写这种程序,但在早期,这也是不得已而为之。程序员利用这种手段来细致地定义他们希望计算机执行的具体操作,细致到每一条指令。为了从性能不足的计算机中尽可能地榨取性能,这种精细的控制是必需的。1990 年代的那些对速度要求极高的电脑游戏,比如《毁灭战士(Doom)》和《雷神之锤(Quake)》,都是采用上面那种汇编语言编写的。如果不这样搞,这些游戏根本做不出来。
1.1.1 Making it a little easier
1.1.1 让难度降低一点儿
Okay, no more of that. Let’s move on. Would you be happier reading this code below?
好的,汇编到此为止。我们继续。以下这段代码感觉如何?
for num in range(0, 9):
print(num)
This code is in the Python language, which 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’s not too hard 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, and it doesn’t actually print the numbers from 0 to 9 (instead, it prints 0 to 8).
这段代码是用 Python 语言编写的,目前许多程序员都在使用它。不同于汇编语言这样的低级语言,Python 被视为高级语言,因为它与自然语言的距离更近。即便你对 Python 代码尚无了解,你或许也能猜出这段程序的目的。第一行似乎在处理 0~9 的数字范围。第二行在进行打印操作。不难理解,这段程序的目的与上面那段汇编语言 “天书” 是一样的,是想打印出 0~9 的数字(不过很可惜,它实际打印的是 0~8)。
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 previous code, misunderstanding the details of those rules can result in a broken program.
尽管这种代码更接近英语,但它并非英语。它还是一种编程语言,就像汇编语言一样,有着特定的规则。正如上面的代码所示,误解这些规则的细节可能会导致程序错误。
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. This is changing.
与计算机沟通的终极目标是能够使用诸如英语这样的自然语言。在过去 70 年里,我们之所以使用各种编程语言与计算机对话,并非出于喜好,而是迫于无奈。计算机的计算能力不足以应对英语等自然语言的复杂性和特异性。我们的编程语言虽然一直在演进——比如从天书一般的汇编语言发展到了 Python——但它们依旧是计算机语言,并非自然语言。不过,这种情况正在发生变化。
1.1.2 Making it a lot easier
1.1.2 让难度降低一大截
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:
有了 AI 助手,我们现在可以用英语提出我们的需求,并让计算机生成相应的代码。如果我们希望得到一段正确 Python 程序,打印出 0~9 的数字,我们可以用日常英语向我们的 AI 助手(Copilot)发出指令:
# Output the numbers from 0 to 9
Copilot might respond to this prompt by generating something like this:
对此,Copilot 可能会根据这一指令生成如下代码:
for i in range(10):
print(i)
Unlike the example we showed you before, this piece of Python code actually works!
不同于上面那个例子,这段 Python 代码确实能够正常运行!
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.
AI 编程助手能够帮助人们编写代码。在本书中,我们会探索如何利用 Copilot 来编写代码。我们只需用英语描述我们的需求,便能收到用 Python 编写的代码作为回应。
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 to write code and the other to ask Google how to write code. This second window has 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 programmers’ time is spent searching for code [1], and much of the code that is found is not readily usable. Copilot greatly improves this experience by helping us write our code.
更重要的是,我们将能够把 Copilot 无缝集成到我们的工作流程中。在没有 Copilot 这类工具的情况下,程序员通常需要同时打开两个窗口:一个用于编写代码,另一个用于在 Google 上查询编码方法。这第二个窗口可能充斥着 Google 搜索结果、Python 文档或是程序员论坛上讨论如何解决特定编码问题的帖子。他们经常需要从这些结果中复制代码到自己的项目中,随后进行适当的调整以符合自己的应用场景,尝试不同的解决方案等等。这已经成为了程序员生活的一部分——但你可以想象这种工作方式的效率有多低。据估计,程序员可能有多达 35% 的时间花在搜索代码上 [1],而且找到的代码并不是立即可用的。有了 Copilot 协助我们编写代码,这种情况将得到显著改善。