「Example4.1」の編集履歴(バックアップ)一覧はこちら
Example4.1 - (2010/10/18 (月) 09:57:59) の最新版との変更点
追加された行は緑色になります。
削除された行は赤色になります。
** 4.1 Expressions And Simple Functions
A Scala system comes with an interpreter which can be seen as a fancy calculator. A user interacts with the calculator by typing in expressions. The calculator returns the evaluation results and their types. For example:
Scala システムには素敵な計算機のように見えるインタプリタが同梱されています。ユーザは式をタイプすることで計算機と交信出来ます。計算機は評価した結果とその型を返します。例えば
ーーー
修正案murase_syuka
s/素敵な計算機のように見えるインタプリタ/高級計算機と見ることができるインタプリタ/
ーーー
scala> 87 + 145
unnamed0: Int = 232
scala> 5 + 2 * 3
unnamed1: Int = 11
scala> "hello" + " world!"
unnamed2: java.lang.String = hello world!
It is also possible to name a sub-expression and use the name instead of the expression afterwards:
部分式に名前を付け、以後は式の代わりに名前を使う事が出来ます。
scala> def scale = 5
scale: Int
scala> 7 * scale
unnamed3: Int = 35
scala> def pi = 3.141592653589793
pi: Double
scala> def radius = 10
radius: Int
scala> 2 * pi * radius
unnamed4: Double = 62.83185307179586
Definitions start with the reserved word def; they introduce a name which stands for the expression following the = sign. The interpreter will answer with the introduced name and its type.
定義は予約語 def で始まります。= 記号に続く式を表す名前を導入します。インタプリタは導入された名前と型を返します。
Executing a definition such as def x = e will not evaluate the expression e. Instead e is evaluated whenever x is used. Alternatively, Scala offers a value definition val x = e, which does evaluate the right-hand-side e as part of the evaluation of the definition. If x is then used subsequently, it is immediately replaced by the pre-computed value of e, so that the expression need not be evaluated again.
def x = e のような定義の実行は式 e を評価しません。その代わり、x が使用される時はいつでも e は評価されます。一方で、 Scala には値の定義 val x = e もあり、定義の評価の一部として右辺 e を評価します。その後 x が使用されたならば、前に計算された e の値で直ちに置き換えられ、式は再評価する必要はありません。
How are expressions evaluated? An expression consisting of operators and operands is evaluated by repeatedly applying the following simplification steps.
式はどのように評価されるのでしょう?。演算子とオペランドから構成される式は下記の簡単化のステップを繰り返し適用して評価されます。
- pick the left-most operation
- evaluate its operands
- apply the operator to the operand values.
- 最も左の操作を選ぶ
- そのオペランドを評価する
- オペランド値に演算子を適用する
A name defined by def is evaluated by replacing the name by the (unevaluated) definition’s right hand side. A name defined by val is evaluated by replacing the name by the value of the definitions’s right-hand side. The evaluation process stops once we have reached a value. A value is some data item such as a string, a number, an array, or a list.
def で定義された名前は、名前を (未評価の) 定義の右辺で置き換えることによって評価されます。val で定義された名前は、定義の右辺の値で名前を置き換える事で評価されます。評価プロセスは値を得たら終了します。値は文字列や数や配列やリストといったデータアイテムです。
Example 4.1.1 Here is an evaluation of an arithmetic expression.
数式の評価です。
(2 * pi) * radius
→ (2 * 3.141592653589793) * radius
→ 6.283185307179586 * radius
→ 6.283185307179586 * 10
→ 62.83185307179586
The process of stepwise simplification of expressions to values is called reduction.
式を値へと段階的に簡単化するプロセスを「簡約」と呼びます。
#center(){[[前ページ>ExampleChap4]] [[4章>ExampleChap4.1]] [[全体目次>ScalaByExample和訳]] [[次ページ>ExampleChap4.2]]}
----
#comment
#co(){
4.1 Expressions And Simple Functions
A Scala system comes with an interpreter which can be seen as a fancy calculator. A user interacts with the calculator by typing in expressions. The calculator returns the evaluation results and their types. For example:
}
#setmenu2(ex-r-menu)
** 4.1 式と簡単な関数
Scala システムには、素敵な計算機と見ることのできるインタプリタが同梱されています。ユーザーは式をタイプすることで計算機と交信できます。計算機は評価した結果とその型を返します。たとえば
scala> 87 + 145
unnamed0: Int = 232
scala> 5 + 2 * 3
unnamed1: Int = 11
scala> "hello" + " world!"
unnamed2: java.lang.String = hello world!
#co(){
It is also possible to name a sub-expression and use the name instead of the expression afterwards:
}
部分式に名前を付け、以後は式の代わりにその名前を使うこともできます。
scala> def scale = 5
scale: Int
scala> 7 * scale
unnamed3: Int = 35
scala> def pi = 3.141592653589793
pi: Double
scala> def radius = 10
radius: Int
scala> 2 * pi * radius
unnamed4: Double = 62.83185307179586
#co(){
Definitions start with the reserved word def; they introduce a name which stands for the expression following the = sign. The interpreter will answer with the introduced name and its type.
Executing a definition such as def x = e will not evaluate the expression e. Instead e is evaluated whenever x is used. Alternatively, Scala offers a value definition val x = e, which does evaluate the right-hand-side e as part of the evaluation of the definition. If x is then used subsequently, it is immediately replaced by the pre-computed value of e, so that the expression need not be evaluated again.
}
定義は予約語 def で始まります。= 記号に続く式を表す名前を導入します。インタプリタは導入された名前と型を返します。
def x = e のような定義の実行は、式 e を評価しません。その代わり、x が使われる時はいつでも e は評価されます。一方で、Scala には値の定義 val x = e もあり、定義評価の一環として右辺 e を評価します。その後 x が使われるときは、前に計算された e の値で直ちに置き換えられので、式を再評価する必要はありません。
#co(){
How are expressions evaluated? An expression consisting of operators and operands is evaluated by repeatedly applying the following simplification steps.
- pick the left-most operation
- evaluate its operands
- apply the operator to the operand values.
}
式はどのように評価されるのでしょう? 演算子とオペランドから構成される式は次の簡単化のステップを繰り返し適用して評価されます。
- 最も左の操作を選ぶ
- そのオペランドを評価する
- オペランド値に演算子を適用する
#co(){
A name defined by def is evaluated by replacing the name by the (unevaluated) definition's right hand side. A name defined by val is evaluated by replacing the name by the value of the definitions's right-hand side. The evaluation process stops once we have reached a value. A value is some data item such as a string, a number, an array, or a list.
Example 4.1.1 Here is an evaluation of an arithmetic expression.
}
def で定義された名前は、名前を (未評価の) 定義の右辺で置き換えることで評価されます。val で定義された名前は、名前を定義の右辺の値で置き換えることで評価されます。評価プロセスは値を得たら終了します。値は文字列や数や配列やリストといったデータアイテムです。
&b(){Example 4.1.1 } 数式の評価です。
(2 * pi) * radius
→ (2 * 3.141592653589793) * radius
→ 6.283185307179586 * radius
→ 6.283185307179586 * 10
→ 62.83185307179586
#co(){
The process of stepwise simplification of expressions to values is called reduction.
}
式を値へと段階的に簡単化するプロセスを&bold(){簡約}と呼びます。
#center(){[[前ページ>ExampleChap4]] [[ 4 章>ExampleChap4]] [[目次>ScalaByExample和訳]] [[次ページ>Example4.2]]}
----
ーーー 修正案murase_syuka
s/素敵な計算機のように見えるインタプリタ/高級計算機と見ることができるインタプリタ/
ーーー
#comment