markdown
Functional Programming Fundamentalsmd d126b95
lecture/information/programming-languages/foundation/introduction-to-functional-programming.lecture.n.md
Download PDF

Functional Programming Fundamentals

date2026-07-14document_iddoc_d7e77bd0e2008362b28f50668741e33bdescription純粋関数、不変性、高階関数、参照透過性を定義し、関数型プログラミングの基本と命令型プログラミングとの関係を説明する。prerequisitesdata/lecture/information/programming/introduction-to-programming.lecture.n.mdtype講義statusactiverelateddata/lecture/information/programming-languages/foundation/functional-programming-and-type-theory-portal.lecture.n.md / data/lecture/information/programming-languages/foundation/variable-binding-and-free-variables-basics.lecture.n.md / data/lecture/information/programming/introduction-to-programming.lecture.n.md
informationprogramming-languageslecture
data/lecture/information/programming/introduction-to-programming.lecture.n.md

1Introduction

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 pure functionPure function is a function whose return value is determined only by its arguments and whose evaluation produces no externally observable state change.

A side effectSide effect is an externally observable change that occurs in addition to computing a return value. Updating a shared variable, performing I/O, and raising an exception are examples.

ImmutabilityImmutability is the property that a value's contents do not change after the value is created. When updated information is required, a new value is created without modifying the existing value.

A first-class functionFirst-class function is a function value that can be used like other values, including as an argument, return value, or element of a data structure. A higher-order functionHigher-order function is a function that receives a function as an argument or returns a function as its result.

Referential transparencyReferential transparency is the property that replacing an expression with its value does not change the program's observable behavior.

3Pure Functions and Immutability

For example, a pure function that increments an integer by one can be written as follows.

text
increment(n) = n + 1

Every evaluation of increment(3) produces 4, 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 x to an immutable list xs constructs a new list from x 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 e evaluates to value v and e is referentially transparent. Replacing e with v in the program then leaves its observable result unchanged. This property enables equational reasoningEquational reasoning, in which transformations of expressions can be verified as equations.

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

text
map(f, [])       = []
map(f, x :: xs)  = f(x) :: map(f, xs)

map is a higher-order function because it receives function f as an argument. It centralizes the traversal of a list while separating the operation on each element as f. 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.

8Next Lecture

data/lecture/information/programming-languages/foundation/variable-binding-and-free-variables-basics.lecture.n.md
raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる