削除された内容 追加された内容
<source> -> <syntaxhighlight>
m 空白
1行目:
{{lowercase}}
'''cons'''は、ほとんどの[[Lisp]]方言における基本的な関数である。cons は、2つの、値もしくは値へのポインタを保持するオブジェクトを構成(construct)する。これによって作られたオブジェクトは、(cons)セル、cons、non-atomic S式(NATS式)、(cons)対などと呼ばれる。<!-- In Lisp jargon, the expression "to cons x onto y" means to construct a new object with (cons x y). Lisp 関連の用語(俗語?) として,「x と y の cons を取る」は新しいオブジェクト、(cons x y) を作ることを意味する。? -->cons の結果のペアの左側(第一要素)は car{{efn|由来は、contents of the address part of the register<ref name=グレアム2002>{{Cite book |和書 |author=ポール・グレアム |authorlink=ポール・グレアム |year=2002 |title=ANSI Common Lisp |publisher=ピアソン・エデュケーション |page=280 |isbn=4-89471-433-7}}</ref>}}と呼ばれ、右側(その残り)は cdr{{efn|由来は、contents of the decrement part of the register<ref name=グレアム2002 />}}と呼ばれる。<!-- It is loosely related to the object-oriented notion of a constructor, which creates a new object given arguments, and more closely related to the constructor function of an algebraic data type system.
cons の結果のペアの左側(第一要素)は car{{efn|由来は、contents of the address part of the register<ref name=グレアム2002>{{Cite book |和書 |author=ポール・グレアム |authorlink=ポール・グレアム |year=2002 |title=ANSI Common Lisp |publisher=ピアソン・エデュケーション |page=280 |isbn=4-89471-433-7}}</ref>}} と呼ばれ、右側(その残り)は cdr{{efn|由来は、contents of the decrement part of the register<ref name=グレアム2002 />}} と呼ばれる。
<!-- It is loosely related to the object-oriented notion of a constructor, which creates a new object given arguments, and more closely related to the constructor function of an algebraic data type system.
The word "cons" and expressions like "to cons onto" are also part of a more general functional programming jargon. Sometimes operators that have a similar purpose, especially in the context of list processing, are pronounced "cons". (A good example is the :: operator in ML and Scala, which adds an element to the beginning of a list.) -->
 
== 使い方 ==
cons 自体は単に、データの[[順序対]]を保持することができるだけだが、しばしば[[リスト_ (抽象データ型)|リスト]]や[[二分木]]などの複雑なデータ構造を保持するためにも使われる。
 
例えば、Lisp の式、<code>(cons 1 2)</code> は、左側(car部)に1 、右側(cdr部)に2 を持ったcons セルを作る。Lisp の表記では、<code>(cons 1 2)</code> の値は次のようになる:
Lisp の表記では、<code>(cons 1 2)</code> の値は次のようになる:
 
(1 . 2)
24 ⟶ 21行目:
#一般に <code>nil</code> と呼ばれる特別なオブジェクトである空のリスト <code>()</code>
#<code>car</code> としてリストの第一要素を持ち、 <code>cdr</code> としてリストの残りの要素を持つ cons セル
この構造は、単純な片方向[[連結リスト]]構造を作る。この連結リストは、 <code>cons</code>, <code>car</code>, <code>cdr</code> のみで操作することのできる構造をしている。<code>nil</code> は cons 対でない唯一のリストであることに気をつけなければならない。例として、1, 2, 3 の要素から成るリストを考えよう。そのようなリストは、以下の3ステップで作られる。
<code>nil</code> は cons 対でない唯一のリストであることに気をつけなければならない。
例として、1, 2, 3 の要素から成るリストを考えよう。そのようなリストは、以下の3ステップで作られる。
 
#3 と<code>nil</code>(空リスト)のcons を作る。
32 ⟶ 27行目:
#1 とその結果とのcons を作る。
 
つまり:
 
<syntaxhighlight lang="lisp">(cons 1 (cons 2 (cons 3 nil)))</syntaxhighlight>
 
もしくはそれの短縮形として:
 
<syntaxhighlight lang="lisp">(list 1 2 3)</syntaxhighlight>
 
結果として、以下のリストが得られる:
 
(1 . (2 . (3 . nil)))
54 ⟶ 49行目:
(1 2 3)
 
要するに、<code>cons</code> は、すでに存在するリストの先頭に要素をひとつ付け加えるように働く。例えば、上で定義したリストを''x''とすると、<code>(cons 5 ''x'')</code> は、以下のようなリストを生成する
例えば、上で定義したリストを''x''とすると、<code>(cons 5 ''x'')</code> は、以下のようなリストを生成する。
 
(5 1 2 3)
62 ⟶ 56行目:
 
=== 木構造 ===
[[木構造_ (データ構造)|葉]]の部分にのみ値を持つデータ構造である[[二分木]]もまた、<code>cons</code> によって容易に作ることができる。例えば、以下のコードは
例えば、以下のコードは
 
<syntaxhighlight lang="lisp">(cons (cons 1 2) (cons 3 4))</syntaxhighlight>
 
以下のような結果になる。
 
((1 . 2) . (3 . 4))
137 ⟶ 130行目:
 
{{LISP系言語}}
 
[[カテゴリ:LISP]]
{{DEFAULTSORT:LISP}}
[[カテゴリCategory:LISP]]