markdown
関数型プログラミングの基本md d126b95
lecture/information/programming-languages/foundation/introduction-to-functional-programming.lecture.n.md
Download PDF

関数型かんすうがたプログラミングの基本きほん

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

1導入どうにゅう

この講義こうぎでは、関数かんすう適用てきようしき評価ひょうか中心ちゅうしん計算けいさん構成こうせいする関数型かんすうがたプログラミングを説明せつめいする。純粋関数じゅんすいかんすう不変性ふへんせい高階関数こうかいかんすう参照透過性さんしょうとうかせい定義ていぎし、これらが局所的きょくしょてき推論すいろん寄与きよする理由りゆう解説かいせつする。

関数型かんすうがた命令型めいれいがたは、相互排他的そうごはいたてき分類ぶんるいではない。現実げんじつ言語げんご両者りょうしゃ機能きのう併用へいようすることがおおく、問題もんだいおうじて状態更新じょうたいこうしん純粋じゅんすい計算けいさん適切てきせつ配置はいちすることが重要じゅうようである。

2基本用語きほんようご

純粋関数じゅんすいかんすうPure functionとは、返値かえりち引数ひきすうだけによって決定けっていされ、評価ひょうかによって外部がいぶから観察可能かんさつかのう状態変化じょうたいへんかしょうじさせない関数かんすうである。

副作用ふくさようSide effectとは、返値かえりち計算けいさんとはべつ発生はっせいする、外部がいぶから観察可能かんさつかのう変化へんかである。共有変数きょうゆうへんすう更新こうしん入出力にゅうしゅつりょく例外れいがい送出そうしゅつなどが該当がいとうする。

不変性ふへんせいImmutabilityとは、あたい生成せいせいしたあとにその内容ないよう変更へんこうしない性質せいしつである。変更後へんこうご情報じょうほう必要ひつよう場合ばあいは、既存きそんあたい改変かいへんせずにあたらしいあたい生成せいせいする。

第一級関数だいいちきゅうかんすうFirst-class functionとは、ほかあたい同様どうように、引数ひきすう返値かえりち、データ構造こうぞう要素ようそなどとしてあつかえる関数値かんすうちである。高階関数こうかいかんすうHigher-order functionとは、関数かんすう引数ひきすうとしてるか、関数かんすう返値かえりちとしてかえ関数かんすうである。

参照透過性さんしょうとうかせいReferential transparencyとは、あるしきを、そのしきあたい置換ちかんしてもプログラムの観察可能かんさつかのう振舞ふるまいが変化へんかしない性質せいしつである。

3純粋関数じゅんすいかんすう不変性ふへんせい

たとえば、整数せいすう1 増加ぞうかさせる純粋関数じゅんすいかんすうつぎのように記述きじゅつできる。

text
increment(n) = n + 1

increment(3)評価ひょうかするたびに 4 となり、その評価ひょうか共有状態きょうゆうじょうたい変更へんこうしない。これにたいして、共有きょうゆうカウンタを更新こうしんしてからかえ関数かんすうは、そのカウンタの現在値げんざいち結果けっか依存いぞんするため、純粋関数じゅんすいかんすうではない。

不変ふへんなリスト xs先頭せんとうx追加ついかする操作そうさは、xs変更へんこうせず、xxs からあたらしいリストを構成こうせいする。きゅうリストを参照さんしょうするほかしき意味いみは、この操作そうさによって変化へんかしない。

4参照透過性さんしょうとうかせい等式推論とうしきすいろん

しき eあたい v評価ひょうかされ、e参照透過的さんしょうとうかてきであるとする。このとき、プログラムないev置換ちかんしても観察結果かんさつけっか変化へんかしない。この性質せいしつにより、しき変形へんけい等式とうしきとして検証けんしょうする等式推論とうしきすいろんEquational reasoning可能かのうになる。

ただし、入出力にゅうしゅつりょくそのものが不要ふようになるわけではない。関数型言語かんすうがたげんごは、効果こうか明示めいじするかた構文こうぶん、または純粋じゅんすい計算けいさん効果こうかともな境界きょうかい分離ぶんりによって、必要ひつよう入出力にゅうしゅつりょくあつかう。

5高階関数こうかいかんすう

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

map関数かんすう f引数ひきすうとしてるため、高階関数こうかいかんすうである。リストの走査方法そうさほうほうmap集約しゅうやくし、各要素かくようそへの処理しょりf として分離ぶんりできる。この分離ぶんり処理しょり再利用さいりよう合成ごうせい促進そくしんする。

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.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
タブを全て閉じる