「ExampleChap5」の編集履歴(バックアップ)一覧はこちら
ExampleChap5 - (2011/02/24 (木) 08:33:10) の1つ前との変更点
追加された行は緑色になります。
削除された行は赤色になります。
#co(){
Chapter 5 First-Class Functions
A function in Scala is a "first-class value". Like any other value, it may be passed as a parameter or returned as a result. Functions which take other functions as parameters or return them as results are called higher-order functions. This chapter introduces higher-order functions and shows how they provide a flexible mechanism for program composition.
As a motivating example, consider the following three related tasks:
}
* 第 5 章 第一級の関数 (First-Class Functions)
Scala の関数は「&bold(){第一級の値}」です。他の値と同じように、関数の引数として渡したり結果として返したりできます。他の関数を引数としてとったり、関数を結果として返す関数は、&bold(){高階関数}と呼ばれます。この章では高階関数を紹介し、それがプログラム作成にどれほど柔軟なメカニズムを与えるか示します。
興味をそそる例として、次の3つの関連した課題を考えます。
#co(){
1. Write a function to sum all integers between two given numbers a and b:
}
1. 2つのあたえられた数 a と b の間にある、すべての整数の和を求める関数を書きなさい。
def sumInts(a: Int, b: Int): Int =
if (a > b) 0 else a + sumInts(a + 1, b)
#co(){
2. Write a function to sum the squares of all integers between two given numbers a and b:
}
2. 2つのあたえられた数 a と b の間にある、すべての整数の2乗の和を求める関数を書きなさい。
def square(x: Int): Int = x * x
def sumSquares(a: Int, b: Int): Int =
if (a > b) 0 else square(a) + sumSquares(a + 1, b)
#co(){
3. Write a function to sum the powers 2n of all integers n between two given numbers a and b:
}
3. 2つのあたえられた数 a と b の間にある、すべての整数 n について 2^n の和を求める関数を書きなさい。
def powerOfTwo(x: Int): Int = if (x == 0) 1 else 2 * powerOfTwo(x - 1)
def sumPowersOfTwo(a: Int, b: Int): Int =
if (a > b) 0 else powerOfTwo(a) + sumPowersOfTwo(a + 1, b)
#co(){
These functions are all instances of Σ^b_a f(n) for different values of f . We can factor out the common pattern by defining a function sum:
}
これらの関数はすべて、異なる値 f に対する Σ^b_a f(n) のインスタンスです。
関数 sum を定義して、共通パターンを括り出せます。
def sum(f: Int => Int, a: Int, b: Int): Int =
if (a > b) 0 else f(a) + sum(f, a + 1, b)
#co(){
The type Int => Int is the type of functions that take arguments of type Int and return results of type Int. So sum is a function which takes another function as a parameter. In other words, sum is a higher-order function.
Using sum, we can formulate the three summing functions as follows.
}
型 Int => Int は、型 Int の引数をとって、型 Int の結果を返す関数の型です。したがって sum は、他の関数を引数としてとる関数です。別の言葉で言えば、sum は&bold(){高階関数}です。
sum を使って、和を求める3つの関数を次のように書けます。
def sumInts(a: Int, b: Int): Int = sum(id, a, b)
def sumSquares(a: Int, b: Int): Int = sum(square, a, b)
def sumPowersOfTwo(a: Int, b: Int): Int = sum(powerOfTwo, a, b)
#co(){
where
}
ただし、
def id(x: Int): Int = x
def square(x: Int): Int = x * x
def powerOfTwo(x: Int): Int = if (x == 0) 1 else 2 * powerOfTwo(x - 1)
- [[5.1 無名関数 (Anonymous Functions) >Example5.1]]
- [[5.2 カリー化 (Currying) >Example5.2]]
- [[5.3 例 : 関数の不動点探索 >Example5.3]]
- [[5.4 まとめ >Example5.4]]
- [[5.5 ここまでの構文 >Example5.5]]
#center(){[[前ページ>Example4.6]] [[ 5 章>ExampleChap5]] [[目次>ScalaByExample和訳]] [[次ページ>Example5.1]]}
----
- 冒頭の「他の他の値」は「他の値」のtypoでしょうか。 -- ryugate (2008-05-19 22:19:55)
- 「first-class」は、「ファーストクラス」でいいんでは?補足説明は必要でしょうが、無理に「第一級」と訳す必要はないと思われ。 -- murase_syuka (2008-09-25 01:12:53)
#comment
#co(){
Chapter 5 First-Class Functions
A function in Scala is a "first-class value". Like any other value, it may be passed as a parameter or returned as a result. Functions which take other functions as parameters or return them as results are called higher-order functions. This chapter introduces higher-order functions and shows how they provide a flexible mechanism for program composition.
As a motivating example, consider the following three related tasks:
}
#setmenu2(ex-r-menu)
* 第 5 章 第一級の関数 (First-Class Functions)
Scala の関数は「第一級の値」です。他の値と同じように、関数のパラメータとして渡したり結果として返したりできます。他の関数をパラメータとしてとったり、関数を結果として返す関数は、&bold(){高階関数}と呼ばれます。この章では高階関数を紹介し、それがプログラム作成にどれほど柔軟なメカニズムを与えるか示します。
興味をそそる例として、次の3つの関連した課題を考えます。
#co(){
1. Write a function to sum all integers between two given numbers a and b:
}
1. 2つのあたえられた数 a と b の間にある、すべての整数の和を求める関数を書きなさい。
def sumInts(a: Int, b: Int): Int =
if (a > b) 0 else a + sumInts(a + 1, b)
#co(){
2. Write a function to sum the squares of all integers between two given numbers a and b:
}
2. 2つのあたえられた数 a と b の間にある、すべての整数の2乗の和を求める関数を書きなさい。
def square(x: Int): Int = x * x
def sumSquares(a: Int, b: Int): Int =
if (a > b) 0 else square(a) + sumSquares(a + 1, b)
#co(){
3. Write a function to sum the powers 2n of all integers n between two given numbers a and b:
}
3. 2つのあたえられた数 a と b の間にある、すべての整数 n について 2^n の和を求める関数を書きなさい。
def powerOfTwo(x: Int): Int = if (x == 0) 1 else 2 * powerOfTwo(x - 1)
def sumPowersOfTwo(a: Int, b: Int): Int =
if (a > b) 0 else powerOfTwo(a) + sumPowersOfTwo(a + 1, b)
#co(){
These functions are all instances of Σ^b_a f(n) for different values of f . We can factor out the common pattern by defining a function sum:
}
これらの関数はすべて、異なる値 f に対する Σ^b_a f(n) のインスタンスです。
関数 sum を定義して、共通パターンを括り出せます。
def sum(f: Int => Int, a: Int, b: Int): Int =
if (a > b) 0 else f(a) + sum(f, a + 1, b)
#co(){
The type Int => Int is the type of functions that take arguments of type Int and return results of type Int. So sum is a function which takes another function as a parameter. In other words, sum is a higher-order function.
Using sum, we can formulate the three summing functions as follows.
}
型 Int => Int は、型 Int の引数をとって、型 Int の結果を返す関数の型です。したがって sum は、他の関数をパラメータとしてとる関数です。別の言葉で言えば、sum は&bold(){高階関数}です。
sum を使って、和を求める3つの関数を次のように書けます。
def sumInts(a: Int, b: Int): Int = sum(id, a, b)
def sumSquares(a: Int, b: Int): Int = sum(square, a, b)
def sumPowersOfTwo(a: Int, b: Int): Int = sum(powerOfTwo, a, b)
#co(){
where
}
ただし、
def id(x: Int): Int = x
def square(x: Int): Int = x * x
def powerOfTwo(x: Int): Int = if (x == 0) 1 else 2 * powerOfTwo(x - 1)
- [[5.1 無名関数 (Anonymous Functions) >Example5.1]]
- [[5.2 カリー化 (Currying) >Example5.2]]
- [[5.3 例 : 関数の不動点探索 >Example5.3]]
- [[5.4 まとめ >Example5.4]]
- [[5.5 ここまでの構文 >Example5.5]]
#center(){[[前ページ>Example4.6]] [[ 5 章>ExampleChap5]] [[目次>ScalaByExample和訳]] [[次ページ>Example5.1]]}
----
- 冒頭の「他の他の値」は「他の値」のtypoでしょうか。 -- ryugate (2008-05-19 22:19:55)
- 「first-class」は、「ファーストクラス」でいいんでは?補足説明は必要でしょうが、無理に「第一級」と訳す必要はないと思われ。 -- murase_syuka (2008-09-25 01:12:53)
#comment