@comment $OpenXM: OpenXM/src/asir-doc/parts/builtin/list.texi,v 1.4 2003/12/18 10:26:20 ohara Exp $ \BJP @node リストの演算,,, 組み込み函数 @section リストの演算 \E \BEG @node Lists,,, Built-in Function @section Lists \E @menu * car cdr cons append reverse length:: @end menu \JP @node car cdr cons append reverse length,,, リストの演算 \EG @node car cdr cons append reverse length,,, Lists @subsection @code{car}, @code{cdr}, @code{cons}, @code{append}, @code{reverse}, @code{length} @findex car @findex cdr @findex cons @findex append @findex reverse @findex length @table @t @item car(@var{list}) \JP :: 空でない @var{list} の先頭要素. \EG :: The first element of the given non-null list @var{list}. @item cdr(@var{list}) \JP :: 空でない @var{list} から先頭要素を取り除いたリスト. \EG :: A list obtained by removing the first element of the given non-null list @var{list}. @item cons(@var{obj},@var{list}) \JP :: @var{list} の先頭に @var{obj} を付け加えたリスト. \EG :: A list obtained by adding an element @var{obj} to the top of the given list @var{list}. @item @t{append}(@var{list1},@var{list2}) \JP :: @var{list1} と @var{list2} をこの順に 1 つにしたリスト. \EG :: A list obtained by adding all elements in the list @var{list2} according to the order as it is to the last element in the list @var{list1}. @item @t{reverse}(@var{list}) \JP :: @var{list} を逆順にしたリスト. \EG :: reversed list of @var{list}. @item length(@var{list}|@var{vect}) \JP :: @var{list} の長さ, または @var{vect} の長さ. \EG :: Number of elements in a list @var{list} and a vector @var{vect}. @end table @table @var @item return \JP @code{car()} : 任意, @code{cdr()}, @code{cons()}, @code{append()}, @code{reverse()} : リスト, @code{length()} : 自然数 \EG @code{car()} : arbitrary, @code{cdr()}, @code{cons()}, @code{append()}, @code{reverse()} : list, @code{length()} : non-negative integer @item list list1 list2 \JP リスト \EG list @item obj \JP 任意 \EG arbitrary @end table @itemize @bullet \BJP @item リストは @t{[}@var{obj1},@var{obj2},...@t{]} と表される. @var{obj1} が 先頭要素である. @item @code{car()} は, 空でない @var{list} の先頭要素を出力する. 空リストが入力された場合は, 空リストが出力される. @item @code{cdr()} は, 空でない @var{list} から先頭要素を取り除いたリストを出力する. 空リストが入力された場合は, 空リストが出力される. @item @code{cons()} は, @var{list} の先頭に @var{obj} を付け加えたリストを出力する. @item @code{append()} は, @var{list1} の要素と @var{list2} のすべての要素を結合させたリスト [@var{list1} の要素の並び,@var{list2} の要素の並び] を出力する. @item @code{reverse()} は, @var{list} を逆順にしたリストを出力する. @item @code{length()} は, @var{list} または @var{vect} の長さを出力する. 行列の要素の個数は, @code{size()} を用いる. @item リストは読み出し専用で, 要素の入れ替えはできない. @item リストの @var{n} 番目の要素の取り出しは, @code{cdr()} を @var{n} 回適用 した後 @code{car()} を適用することにより可能であるが, 便法として, ベクトル, 行列などの配列と同様, インデックス @code{[@var{n}]} を後ろに 付けることにより取り出すことができる. ただし, システム内部では, 実際にポインタを @var{n} 回たどるので, 後ろの要素ほど取り出しに時間がかかる. @item @code{cdr()} は新しいセルを生成しないが, @code{append()} は, 実際には 第 1 引数のリストの長さだけの @code{cons()} の繰り返しとなるため, 第 1 引数のリストが長い場合には多くのメモリを消費することになる. @code{reverse()} に関しても同様である. \E \BEG @item A list is written in @b{Asir} as @t{[}@var{obj1},@var{obj2},@dots{}@t{]}. Here, @var{obj1} is the first element. @item Function @code{car()} outputs the first element of a non-null list. For a null list, the result should be undefined. In the current implementation, however, it outputs a null list. This treatment for a null list may subject to change in future, and users are suggested not to use the tentative treatment for a null list for serious programming. @item Function @code{cdr()} outputs a list obtained by removing the first element from the input non-null list. For a null list, the result should be undefined. In the current implementation, however, it outputs a null list. This treatment for a null list may subject to change in future, and users are suggested not to use the tentative treatment for a null list for serious programming. @item Function @code{cons()} composes a new list from the input list @var{list} and an arbitrary object @var{obj} by adding @var{obj} to the top of @var{list}. @item Function @code{append()} composes a new list, which has all elements of @var{list1} in the same ordering followed by all elements of @var{list2} in the same ordering. @item Function @code{reverse()} returns a reversed list of @var{list}. @item Function @code{length()} returns a non-negative integer which is the number of elements in the input list @var{list} and the input vector @var{vect}. Note that function @code{size} should be used for counting elements of @var{matrix}. @item Lists are read-only objects in @b{Asir}. There elements cannot be modified. @item The @var{n}-th element in a list can be referred to by applying the function @code{cdr()} @var{n} times repeatedly and @code{cdr()} at last. A more convenient way to access to the @var{n}-th element is the use of bracket notation, that is, to attach an index @code{[@var{n}]} like vectors and matrices. The system, however, follow the @var{n} pointers to access the desired element. Subsequently, much time is spent for an element located far from the top of the list. @item Function @code{cdr()} does not create a new cell (a memory quantity). Function @code{append()}, as a matter of fact, repeats @code{cons()} for as many as the length of @var{list1} the first argument. Subsequently, @code{append()} consumes much memory space if its first argument is long. Similar argument applies to function @code{reverse()}. \E @end itemize @example [0] L = [[1,2,3],4,[5,6]]; [[1,2,3],4,[5,6]] [1] car(L); [1,2,3] [2] cdr(L); [4,[5,6]] [3] cons(x*y,L); [y*x,[1,2,3],4,[5,6]] [4] append([a,b,c],[d]); [a,b,c,d] [5] reverse([a,b,c,d]); [d,c,b,a] [6] length(L); 3 [7] length(ltov(L)); 3 [8] L[2][0]; 5 @end example