Programming paradigms · Paradigmas de programación
| English | Español |
|---|---|
| paradigm/ˈpærədaɪm/ | paradigma |
| low-level/ləʊ ˈlevl/ | bajo nivel |
| imperative/ɪmˈperətɪv/ | imperativo |
| declarative/dɪˈklærətɪv/ | declarativo |
| functional/ˈfʌŋkʃənl/ | funcional |
| pure functions/pjʊə ˈfʌŋkʃnz/ | funciones puras |
| logic/ˈlɒdʒɪk/ | lógica |
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 · cómo to do it. The others say what · qué 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 · y 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 · y speed · velocidad, 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 · todos that apply. · ¿Cuáles son verdaderas sobre la programación de bajo nivel? Selecciona todas las que correspondan.
Control is bought with effort: assembly is slow to write and hard to maintain, which is why it is confined to drivers and firmware. · El control se obtiene a costa del esfuerzo: el ensamblaje es lento de escribir y difícil de mantener, por lo que se limita a controladores y 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 · cómo 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: · La programación imperativa (procedural) se basa en:
Imperative code gives step-by-step commands (assignments, loops, calls) that change state. · El código imperativo da comandos paso a paso (asignaciones, bucles, llamadas) que cambian el estado.
Declarative programming
- Declarative programming states what · qué is to be computed, not how · cómo. 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 · Laboratorio de conceptos de programación
Connect examples to the programming idea they show. · Conectar ejemplos con la idea de programación que muestran.
Declarative programming means you specify: · La programación declarativa significa que especificas:
Declarative code (functional, logic, SQL) states the goal; the runtime decides the steps. · El código declarativo (funcional, lógico, SQL) establece el objetivo; el tiempo de ejecución decide los pasos.
A pure function (functional programming): · Una función pura (programación funcional):
Purity means no side effects and a deterministic result, which makes functional code easy to reason about. · La pureza significa no tener efectos secundarios y un resultado determinista, lo que facilita razonar sobre el código funcional.
Declarative paradigms (functional, logic, SQL) state WHAT to compute and let the runtime decide how, whereas imperative code spells out every step. · Los paradigmas declarativos (funcional, lógico, SQL) establecen QUÉ calcular y dejan que el tiempo de ejecución decida cómo, mientras que el código imperativo detalla cada paso.
A SQL query says which rows you want, not how to scan the tables — the opposite of step-by-step imperative code. · Una consulta SQL dice qué filas quieres, no cómo escanear las tablas — lo opuesto al código imperativo paso a paso.
A pure function always gives the same output for the same input and has no ____. · Una función pura siempre da la misma salida para la misma entrada y no tiene ____.
That is what lets it be tested in isolation, run in parallel safely and have its result cached. · Es lo que permite probarla de forma aislada, ejecutarla en paralelo de forma segura y almacenar su resultado en caché.
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 · y the feature of the code that shows it.
Match each fragment to its paradigm. · Empareja cada fragmento con su paradigma.
Commands changing state, a statement of what is wanted, instructions addressing memory, and a rule for inference. · Comandos que cambian el estado, una afirmación de lo que se desea, instrucciones que acceden a la memoria y una regla para la inferencia.
Comparing them
| Paradigm | Says | Strength · Fuerza | 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. · Empareja cada paradigma con su idea central.
Imperative says how step by step; OO models objects; functional uses pure functions; declarative states the goal. · El imperativo dice cómo paso a paso; OO modela objetos; funcional usa funciones puras; declarativo establece el objetivo.
What is the essential difference between imperative and declarative programming? · ¿Cuál es la diferencia esencial entre la programación imperativa y declarativa?
Because the declarative version does not fix the how, the runtime may reorder the work, use an index or parallelise it. · Como la versión declarativa no fija el cómo, el tiempo de ejecución puede reordenar el trabajo, usar un índice o paralelizarlo.
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 · paralelo 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 · cómo; declarative says what · qué. 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 · cómo; the style of Python, C and pseudocode
- declarative: states what · qué is wanted and leaves the how to the runtime, as functional programming with pure functions, logic programming with facts and rules, and SQL