関数型 プログラミングの基本
1導入
この
2基本用語
3純粋関数 と不変性
たとえば、
increment(n) = n + 1
increment(3) は
xs のxs をxs から
4参照透過性 と等式推論
ただし、
5高階関数
map(f, []) = []
map(f, x :: xs) = f(x) :: map(f, xs)
map はmap に
6命令型 との関係
たとえば、
7要点
純粋関数 は返値 を引数 だけから決定 し、観察可能 な副作用 を生 じさせない。不変性 と参照透過性 は、式 を局所的 に推論 するための基盤 となる。高階関数 は、計算 の共通構造 と個別処理 を分離 する。実用的 なプログラムでは、純粋 な計算 と必要 な効果 を明確 な境界 で統合 する。
8次 に進 む講義
data/lecture/information/programming-languages/foundation/variable-binding-and-free-variables-basics.lecture.n.md
Functional Programming Fundamentals
data/lecture/information/programming/introduction-to-programming.lecture.n.md1Introduction
This lecture explains functional programming, which organizes computation primarily through function application and expression evaluation. It defines pure functions, immutability, higher-order functions, and referential transparency and explains why these concepts support local reasoning.
Functional and imperative programming are not mutually exclusive categories. Practical languages often combine features from both, and effective designs place state updates and pure computations according to the needs of the problem.
2Basic Terms
A
A
A
3Pure Functions and Immutability
For example, a pure function that increments an integer by one can be written as follows.
increment(n) = n + 1
Every evaluation of increment(3) produces , and the evaluation does not modify shared state. By contrast, a function that updates and returns a shared counter is not pure because its result depends on the counter's current state.
An operation that prepends to an immutable list xs constructs a new list from and xs without modifying xs. Consequently, the meaning of other expressions that refer to the old list does not change.
4Referential Transparency and Equational Reasoning
Suppose expression evaluates to value and is referentially transparent. Replacing with in the program then leaves its observable result unchanged. This property enables
This does not make I/O unnecessary. Functional languages handle required I/O through types or syntax that make effects explicit, or by separating pure computations from boundaries that perform effects.
5Higher-Order Functions
map(f, []) = []
map(f, x :: xs) = f(x) :: map(f, xs)
map is a higher-order function because it receives function as an argument. It centralizes the traversal of a list while separating the operation on each element as . This separation facilitates reuse and composition.
6Relationship to Imperative Programming
Imperative programming describes computation primarily through state transitions caused by instructions such as assignments. Functional programming describes computation primarily through expression evaluation and function application. This distinction concerns emphasis; it does not imply that either approach is always preferable.
For example, an implementation may expose a pure function while using a mutable array local to each call, provided that no reference to the array escapes and no call history affects the observable result.
7Summary
- A pure function determines its return value only from its arguments and produces no observable side effects.
- Immutability and referential transparency provide a basis for local reasoning about expressions.
- Higher-order functions separate common computational structure from specific operations.
- Practical programs integrate pure computations and necessary effects at explicit boundaries.