Programming paradigms · 编程范式
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| paradigm/ˈpærədaɪm/ | 范式 | fàn shì |
| low-level/ləʊ ˈlevl/ | 低级 | dī jí |
| imperative/ɪmˈperətɪv/ | 命令式 | mìng lìng shì |
| declarative/dɪˈklærətɪv/ | 声明式 | shēng míng shì |
| functional/ˈfʌŋkʃənl/ | 函数式 | hán shù shì |
| pure functions/pjʊə ˈfʌŋkʃnz/ | 纯函数 | chún hán shù |
| logic/ˈlɒdʒɪk/ | 逻辑式 | luó jí shì |
The same program, written four ways
- Ask for all customers in the UK and a C programmer writes a loop over an array. A Haskell programmer writes a filter. A Prolog programmer states a rule and asks a question. An SQL user writes
SELECT * FROM Customer WHERE Country = 'UK'. - Only the first says how to do it. The others say what is wanted and leave the how to the machine, which is free to reorder the work, use an index, or run it on eight cores.
- Neither is better in general. What differs is which decisions the language takes away from you, and that is exactly what a paradigm 范式 is.
- This lesson is the paradigms the syllabus names: low-level 低级, imperative 命令式 and declarative 声明式.
Low-level programming
- Low-level programming works close to the hardware, in machine code or assembly language, with direct access to registers, memory addresses and individual instructions.
- Benefits: maximum control and speed, and the smallest possible code, which matters when there are only kilobytes of memory.
- Drawbacks: it is architecture-specific, so it must be rewritten for a different processor, and it is slow to write and hard to maintain.
- Used where the hardware must be commanded exactly: device drivers, firmware, embedded controllers, and the innermost loop of a real-time system.
- Practise immediate, direct, indirect, indexed and relative addressing in the assembly-language lesson. Its worked trace shows why the same operand can produce different results.
Which are true of low-level programming? Select all · 所有 that apply. · 关于低级编程哪些陈述是正确的?选择所有适用项。
Control is bought with effort: assembly is slow to write and hard to maintain, which is why it is confined to drivers and firmware. · 控制是以努力为代价换来的:汇编语言编写慢且难以维护,因此仅限于驱动程序和固件。
Imperative programming
- Imperative programming, also called procedural, is a sequence of commands that change the program's state: assignments, conditionals, loops and calls to procedures.
- The programmer specifies how the result is to be computed, step by step. The variables holding the state are the point.
- It is the style of Python, C, Java and pseudocode, and it is what almost everyone learns first.

One command after another, each changing what is stored
Imperative (procedural) programming is based on: · 命令式(过程式)编程基于:
Imperative code gives step-by-step commands (assignments, loops, calls) that change state. · 命令式代码给出逐步命令(赋值、循环、调用)来改变状态。
Declarative programming
- Declarative programming states what is to be computed, not how. The runtime works out the steps.
- Functional 函数式 programming composes pure functions 纯函数, which have no side effects: the same input always produces the same output, and nothing outside the function changes. Haskell and Lisp are the examples.
- Logic 逻辑式 programming states facts and rules, and an engine answers queries by inference. Prolog is the example.
- SQL is the declarative language nearly everyone has met: the query says which rows are wanted, and the database decides how to find them.
Programming concept lab · 编程概念实验室
Connect examples to the programming idea they show. · 将示例与其展示的编程概念连接起来。
Declarative programming means you specify: · 声明式编程意味着你指定:
Declarative code (functional, logic, SQL) states the goal; the runtime decides the steps. · 声明式代码(函数式、逻辑、SQL)陈述目标;由运行时决定步骤。
A pure function (functional programming): · 纯函数(函数式编程):
Purity means no side effects and a deterministic result, which makes functional code easy to reason about. · 纯净性意味着无副作用和确定性结果,这使得函数式代码易于推理。
Declarative paradigms (functional, logic, SQL) state WHAT to compute and let the runtime decide how, whereas imperative code spells out every step. · 声明式范式(函数式、逻辑、SQL)陈述要计算什么(WHAT),让运行时决定如何计算(HOW),而命令式代码则列出了每一步。
A SQL query says which rows you want, not how to scan the tables — the opposite of step-by-step imperative code. · SQL查询说明你想要哪些行,而不是如何扫描表——这与逐步的命令式代码相反。
A pure function always gives the same output for the same input and has no . · 对于相同的输入,纯函数始终给出相同的输出且没有。
That is what lets it be tested in isolation, run in parallel safely and have its result cached. · 这正是使其能够独立测试、安全并行运行并能缓存结果的原因。
Worked example: identify the paradigm
FOR i ← 1 TO n : total ← total + A[i] : NEXT i— imperative: a sequence of commands changing the state held intotal.SELECT Name FROM Customer WHERE Country = 'UK'— declarative: it says which rows are wanted and not how to search for them.LDD 200 : ADD 201 : STO 202— low-level: assembly instructions addressing memory locations directly.parent(X, Y) :- father(X, Y).— declarative, specifically logic programming: a rule from which the engine infers answers.- Name the paradigm and the feature of the code that shows it.
Match each fragment to its paradigm. · 将每个片段与其范式匹配。
Commands changing state, a statement of what is wanted, instructions addressing memory, and a rule for inference. · 改变状态的命令、想要什么的陈述、寻址内存的指令以及推理规则。
Comparing them
| Paradigm | Says | Strength | Typical use |
|---|---|---|---|
| low-level | exact instructions | control, speed, size | drivers, firmware |
| imperative | how, step by step | direct and familiar | general programming |
| declarative | what is wanted | concise, the runtime optimises | queries, rules, data transformation |
- Modern languages mix them. Python is imperative but has functional features; SQL sits inside programs written imperatively. A paradigm is a style, not a wall.
Match each paradigm to its core idea. · 将每种范式与其核心概念匹配。
Imperative says how step by step; OO models objects; functional uses pure functions; declarative states the goal. · 命令式说明逐步怎么做;OO建模对象;函数式使用纯函数;声明式陈述目标。
What is the essential difference between imperative and declarative programming? · 命令式编程和声明式编程的本质区别是什么?
Because the declarative version does not fix the how, the runtime may reorder the work, use an index or parallelise it. · 因为声明式版本不固定“如何”,运行时可能会重新排序工作、使用索引或对其进行并行化。
Worked example: why side effects matter
- A pure function has no side effects. Explain one benefit.
- Because the same input always gives the same output and nothing outside changes, a pure function can be tested in isolation: no setup, no hidden state to arrange.
- It can also be run in parallel safely, since two calls cannot interfere with each other, and its result can be cached.
- The contrast: an imperative routine that updates a global variable behaves differently depending on what ran before it, which is what makes such bugs hard to find.
Marks that slip away
- Imperative says how; declarative says what. That contrast is the answer to most questions here.
- Low-level's drawback is that it is architecture-specific and hard to maintain, not that it is "old".
- A pure function has no side effects and gives the same output for the same input. Both halves are needed.
- SQL is declarative. It is the example the exam most often uses, so recognise it.
You've got it
- a paradigm is a style of structuring code, and modern languages mix several
- low-level: machine code or assembly, direct hardware access, maximum control and speed, but architecture-specific and hard to maintain
- imperative: a sequence of commands changing state, specifying how; the style of Python, C and pseudocode
- declarative: states what is wanted and leaves the how to the runtime, as functional programming with pure functions, logic programming with facts and rules, and SQL