Example9.1

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

Example9.1 - (2011/02/24 (木) 08:45:49) の1つ前との変更点

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

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

#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). } ** 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
#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 もご覧ください。