《Composing Programs》学习笔记(1.2)编程的元素(关键词:软件工程)

1.2 Elements of Programming

1.2 编程的元素

A programming language is more than just a means for instructing(教导;命令) a computer to perform tasks. The language also serves as a framework within which we organize our ideas about computational processes. Programs serve to communicate those ideas among the members of a programming community. Thus(因此;这样), programs must be written for people to read, and only incidentally(顺便;附带) for machines to execute.

一种编程语言,不仅仅是命令一台计算机完成任务的一种方法。编程语言也充当一种框架,这种框架里面,我们组织我们关于计算过程的想法。程序在一个编程社区的成员中服务,以交流那些想法。因此,程序必须必须写给人来读,并且只是顺便给机器执行。

When we describe a language, we should pay particular attention to the means that the language provides for combining simple ideas to form more complex ideas. Every powerful language has three such mechanisms(机制):

当我们描述一种语言,我们应该特别注意这种方法,这种方法是语言提供的、用以将简单的想法组合为更复杂的想法。每种强大的语言有3种这样的机制:

  • primitive(原始的) expressions and statements, which represent the simplest building blocks that the language provides,
  • means of combination, by which compound(复合;混合) elements are built from simpler ones, and
  • means of abstraction, by which compound elements can be named and manipulated(操作,处理;操纵) as units.

In programming, we deal with two kinds of elements: functions and data. (Soon we will discover that they are really not so distinct(有区别的).) Informally(非正式地;通俗地), data is stuff(材料;原料) that we want to manipulate(操作,处理;操纵), and functions describe the rules for manipulating the data. Thus(因此;于是), any powerful programming language should be able to describe primitive(原始的) data and primitive(原始的) functions, as well as(此外;除…之外(也)) have some methods for combining and abstracting both functions and data.

原始的表达式和声明,代表了这种语言提供的最简单的构造块;组合的方法,复合元素是由更简单的元素构成;抽象的方法,复合的元素可以被命名、被处理为单元。在编程中,我们处理两种元素:函数和数据。(很快我们会发现,它们真的没有太大的区别。)通俗地说,数据是我们想要操纵的原料,函数描述了操纵原料的规则。因此,任何强大的编程语言应该能够描述原始的数据和原始的函数,此外(编程语言)有一些方法用于组合和抽象【函数和数据】。

1.2.1 Expressions
1.2.1 表达式

Video: Show Hide

录像:展示隐藏物

Having experimented with the full Python interpreter in the previous section, we now start a new, methodically(有条理地) developing the Python language element by element. Be patient if the examples seem simplistic(过分简单化的) — more exciting material is soon to come.

在上一小节中,已经利用整个Python解释器做了实验,我们现在开始一个新的、有条理地开发Python语言,一个又一个元素。要有点耐心,如果这些例子看起来过分简单——更令人兴奋的原料马上就来。

We begin with primitive(原始的) expressions. One kind of primitive expression is a number. More precisely(精确地;严格地), the expression that you type consists of the numerals(数字) that represent the number in base 10.

我们以原始的表达式开始。原始的表达式的一种是一个数。更严格地说,你输入的这个表达式,是10进制数。

>>> 42
42

Expressions representing numbers may be combined with mathematical operators(运算符) to form(形成;构成) a compound(复合物) expression, which the interpreter will evaluate(求…的值):

表达式代表的数,可能会被数学运算符联结,以构成一个符合表达式,解释器会求值:

>>> -1 - -1
0
>>> 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 + 1/128
0.9921875

These mathematical expressions use infix(中缀) notation(标记法), where the operator (e.g., +, -, *, or /) appears in between the operands(操作数;运算数) (numbers). Python includes many ways to form compound expressions. Rather than(与其…倒不如…) attempt to enumerate(列举) them all immediately, we will introduce new expression forms as we go, along with the language features that they support.

这些数学表达式使用中缀标记法,运算符(例如, +, -, *, 或 /)出现在运算数之间。Python包括很多方法,构成复杂的表达式。与其尝试立即列举它们全部,倒不如我们介绍新的表达式形式,随着它们支持的语言特性。

1.2.2 Call Expressions
1.2.2 调用表达式

Video: Show Hide
The most important kind of compound expression is a call expression, which applies a function to some arguments. Recall(召回;取消) from algebra(代数) that the mathematical(数学的;精确的;绝对的) notion(标记法) of a function is a mapping from some input arguments to an output value. For instance(比如), the max function maps its inputs to a single output, which is the largest of the inputs. The way in which Python expresses function application is the same as in conventional(平常的;传统的;依照惯例的) mathematics.

最重要的复合表达式是一个调用表达式,它将一个函数应用在一些参数上。从代数召回,是一个从某些输入参数到一个输出值的映射,这个代数有着精确的标记法的函数。比如,max函数将它的输入映射到一个单一的输出,这个输出是最大的。Python表达应用程序的方式,与在平常的数学中一样。

>>> max(7.5, 9.5)
9.5

This call expression has subexpressions: the operator(运算符) is an expression that precedes(先于;优于) parentheses(圆括号), which enclose(把…围起来) a comma-delimited(逗号分界) list of operand(运算数) expressions.

这个调用表达式有子表达式:运算符是一个优先于圆括号的表达式,圆括号把一个逗号分界的运算数表达式的列表围起来。

这里写图片描述

The operator(运算符) specifies(指定;详述) a function. When this call expression is evaluated, we say that the function max is called with arguments 7.5 and 9.5, and returns a value of 9.5.

运算符指定一个函数。当这个调用表达式被求值,我们说,max函数被参数7.5和9.5调用,并且返回一个9.5的值。

The order of the arguments in a call expres

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值