Example9.1

「Example9.1」の編集履歴(バックアップ)一覧はこちら

Example9.1 - (2008/07/22 (火) 23:45:06) の最新版との変更点

追加された行は緑色になります。

削除された行は赤色になります。

** 9.1 Using Lists The List type. Like arrays, lists are homogeneous. That is, the elements of a list all have the same type. The type of a list with elements of type T is written List[T] (compare to T[] in Java). ''リスト型'' 配列と同じくリストは同種的です。つまり、リストの要素全ては同じ型です。要素型 T のリストの型は (Java では List<T> ですが) List[T] と書きます。(訳注:原文の T[] は配列の間違いでは?) val fruit: List[String] = List("apples", "oranges", "pears") val nums : List[Int] = List(1, 2, 3, 4) val diag3: List[List[Int]] = List(List(1, 0, 0), List(0, 1, 0), List(0, 0, 1)) val empty: List[Int] = List() ''List constructors.'' All lists are built from two more fundamental constructors, Nil and :: (pronounced “cons”). Nil represents an empty list. The infix operator :: expresses list extension. That is, x :: xs represents a list whose first element is x, which is followed by (the elements of ) list . Hence, the list values above could also have been defined as follows (in fact their previous definition is simply syntactic sugar for the definitions below). ''リストのコンストラクタ'' 全てのリストは二つの重要なコンストラクタである Nil と :: ("cons" と発音する) とから作られます。Nil は空リストを表します。中置演算子 :: はリストの延長を表します。すなわち、x :: xs は最初の要素が x で、その後にリスト xs (の要素) が続くリストを表します。従って、上記のリストの値は下記の様に定義出来ます (実際、前述の定義は単に下記の糖衣構文です)。 val fruit = "apples" :: ("oranges" :: ("pears" :: Nil)) val nums = 1 :: (2 :: (3 :: (4 :: Nil))) val diag3 = (1 :: (0 :: (0 :: Nil))) :: (0 :: (1 :: (0 :: Nil))) :: (0 :: (0 :: (1 :: Nil))) :: Nil val empty = Nil The ‘::’ operation associates to the right: A :: B :: C is interpreted as A :: (B :: C). Therefore, we can drop the parentheses in the definitions above. For instance, we can write shorter '::' 操作は右結合します。A :: B :: C は A :: (B :: C) と解釈されます。従って上記の定義の括弧を除去出来ます。例えば次の様に短く書けます。 val nums = 1 :: 2 :: 3 :: 4 :: Nil ''Basic operations on lists.'' All operations on lists can be expressed in terms of the following three: ''リストの基本操作'' リストの全ての操作は下記の三つを用いて表現出来ます。 head returns the first element of a list, tail returns the list consisting of all elements except the first element, isEmpty returns true iff the list is empty head はリストの最初の要素を返す tail は最初の要素以外のリストの構成要素全てを返す isEmpty はリストが空である時かつその時に限り true を返す。 These operations are defined as methods of list objects. So we invoke them by selecting from the list that’s operated on. Examples: これらの操作はリストオブジェクトのメソッドとして定義されます。それらの操作例を幾つか示す事で説明してみましょう。例えば empty.isEmpty = true fruit.isEmpty = false fruit.head = "apples" fruit.tail.head = "oranges" diag3.head = List(1, 0, 0) The head and tail methods are defined only for non-empty lists. When selected from an empty list, they throw an exception. head と tail のメソッドは非空リストに対してだけ定義されます。空リストに対して選択された場合は例外を投げます。 As an example of how lists can be processed, consider sorting the elements of a list of numbers into ascending order. One simple way to do so is insertion sort, which works as follows: To sort a non-empty list with first element x and rest xs, sort the remainder xs and insert the element x at the right position in the result. Sorting an empty list will yield the empty list. Expressed as Scala code: リストがどのように処理されるかの例として、数のリストを昇順にソートすることを考えましょう。ソートする簡単な方法の一つは挿入ソートであり、次の様に行われます。最初の要素が x で残りが xs であるような非空リストをソートする為には、残りの xs をソートして、要素 x をその結果の正しい場所に挿入します。空リストのソートは空リストを与えます。Scala のコードで表現すると def isort(xs: List[Int]): List[Int] = if (xs.isEmpty) Nil else insert(xs.head, isort(xs.tail)) ''Exercise 9.1.1'' Provide an implementation of the missing function insert. ''例題 9.1.1'' 書かれていない関数 insert を実装しなさい。 ''List patterns.'' In fact, :: is defined as a case class in Scala’s standard library. Hence, it is possible to decompose lists by pattern matching, using patterns composed from the Nil and :: constructors. For instance, isort can be written alternatively as follows. ''リストパターン'' 実際、:: は Scala の標準ライブラリでケースクラスとして定義されています。それ故、Nil と :: コンストラクタからなるパターンを用いたパターンマッチングでリストを分解する事が出来ます。例えば isort は下記の様にも書く事が出来ます。 def isort(xs: List[Int]): List[Int] = xs match { case List() => List() case x :: xs1 => insert(x, isort(xs1)) } where 但し、 def insert(x: Int, xs: List[Int]): List[Int] = xs match { case List() => List(x) case y :: ys => if (x <= y) x :: xs else y :: insert(x, ys) } ---- #comment
#co(){ 9.1 Using Lists The List type. Like arrays, lists are homogeneous. That is, the elements of a list all have the same type. The type of a list with elements of type T is written List[T] (compare to T[] in Java). } #setmenu2(ex-r-menu) ** 9.1 リストの使用 (Using Lists) &b(){リスト型 } 配列と同じくリストは&bold(){等質}です。つまり、リストの要素はすべて同じ型です。要素型 T のリストの型は List[T] と書きます(Java の T[] は配列です)。 val fruit: List[String] = List("apples", "oranges", "pears") val nums : List[Int] = List(1, 2, 3, 4) val diag3: List[List[Int]] = List(List(1, 0, 0), List(0, 1, 0), List(0, 0, 1)) val empty: List[Int] = List() #co(){ List constructors. All lists are built from two more fundamental constructors, Nil and :: (pronounced "cons"). Nil represents an empty list. The infix operator :: expresses list extension. That is, x :: xs represents a list whose first element is x, which is followed by (the elements of ) list . Hence, the list values above could also have been defined as follows (in fact their previous definition is simply syntactic sugar for the definitions below). } &b(){リストのコンストラクタ } すべてのリストは2つの基本的なコンストラクタである Nil と :: ("cons" と発音する) から作られます。Nil は空リストを表します。中置演算子 :: は、リストの拡張を表します。つまり x :: xs は、最初の要素が x で、その後にリスト xs (の要素) が続くリストを表します。したがって、先ほどのリストの値は、次のようにも定義できます (実際、前述の定義は次の単なる糖衣構文です)。 val fruit = "apples" :: ("oranges" :: ("pears" :: Nil)) val nums = 1 :: (2 :: (3 :: (4 :: Nil))) val diag3 = (1 :: (0 :: (0 :: Nil))) :: (0 :: (1 :: (0 :: Nil))) :: (0 :: (0 :: (1 :: Nil))) :: Nil val empty = Nil #co(){ The '::' operation associates to the right: A :: B :: C is interpreted as A :: (B :: C). Therefore, we can drop the parentheses in the definitions above. For instance, we can write shorter } '::' 操作は右結合です。A :: B :: C は A :: (B :: C) と解釈されます。したがって先ほどの定義で、括弧は省略できます。たとえば次のように短く書けます。 val nums = 1 :: 2 :: 3 :: 4 :: Nil #co(){ Basic operations on lists. All operations on lists can be expressed in terms of the following three: } &b(){リストの基本操作 } リストのすべての操作は、次の三つを使って表現できます。 #co(){ head returns the first element of a list, tail returns the list consisting of all elements except the first element, isEmpty returns true iff the list is empty } head : リストの最初の要素を返す。 tail : 最初の要素以外の、すべての要素からなるリストを返す。 isEmpty : リストが空である時、かつその時に限り true を返す。 #co(){ These operations are defined as methods of list objects. So we invoke them by selecting from the list that's operated on. Examples: } これらの操作は、リストオブジェクトのメソッドとして定義されています。操作対象のリストから、それらの操作を選択して呼び出してみましょう。たとえば empty.isEmpty = true fruit.isEmpty = false fruit.head = "apples" fruit.tail.head = "oranges" diag3.head = List(1, 0, 0) #co(){ The head and tail methods are defined only for non-empty lists. When selected from an empty list, they throw an exception. } head と tail メソッドは非空リストに対してだけ定義されています。空リストから選択された場合は、例外を投げます。 #co(){ As an example of how lists can be processed, consider sorting the elements of a list of numbers into ascending order. One simple way to do so is insertion sort, which works as follows: To sort a non-empty list with first element x and rest xs, sort the remainder xs and insert the element x at the right position in the result. Sorting an empty list will yield the empty list. Expressed as Scala code: } リストを扱う例として、数のリストを昇順にソートすることを考えましょう。ソートする一つの簡単な方法は&bold(){挿入ソート}であり、次のようにします。最初の要素が x で残りが xs であるような非空リストをソートするには、残りの xs をソートして、要素 x をその結果の正しい場所に挿入します。空リストのソートは空リストになります。Scala のコードで表現すると def isort(xs: List[Int]): List[Int] = if (xs.isEmpty) Nil else insert(xs.head, isort(xs.tail)) #co(){ Exercise 9.1.1 Provide an implementation of the missing function insert. } &b(){演習 9.1.1 } 書かれていない関数 insert を実装しなさい。 #co(){ List patterns. In fact, :: is defined as a case class in Scala's standard library. Hence, it is possible to decompose lists by pattern matching, using patterns composed from the Nil and :: constructors. For instance, isort can be written alternatively as follows. } &b(){リストパターン } 実際のところ、:: は Scala の標準ライブラリでケースクラスとして定義されています。ですから、Nil と :: コンストラクタからなるパターンを用いた、パターンマッチングでリストを分解できます。たとえば isort は次のようにも書けます。 def isort(xs: List[Int]): List[Int] = xs match { case List() => List() case x :: xs1 => insert(x, isort(xs1)) } #co(){ where } ただし、 def insert(x: Int, xs: List[Int]): List[Int] = xs match { case List() => List(x) case y :: ys => if (x <= y) x :: xs else y :: insert(x, ys) } #center(){[[前ページ>Chapter 9 Lists]] [[ 9 章>Chapter 9 Lists]] [[目次>ScalaByExample和訳]] [[次ページ>Example9.2]]} ---- #comment

表示オプション

横に並べて表示:
変化行の前後のみ表示:
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。