您的位置:首页 > 编程语言 > Lua

Scala-Evaluation Strategies and Termination (4_22)

2015-03-11 16:00 267 查看
This scheme of expression evaluation is called substitution model.将一个表达式计算为一个数值。对没有side effects的表达式都可以使用。
Side effect:

Call-by-name

has the advantage that a function argument is not evaluated if the corresponding parameter is unused in the evaluation of the function body.

Call-by-value

has the advantage that it evaluates every function argument only once.
例:
def test(x:Int, y:Int) = x*x
test(3+4, 8)
Call-by-value
test(7,8)->7*7->49
Call-by-name
Test(7,8)->(3+4)*(3+4)->7*(3+4)->7*7->49
CBV evaluation of an expression e terminates, then CBN evaluation of e terminates, too. The other direction is not true.
Scala choose CBV. If you want to use the CBN, you can use the => to tell it like def constOne(x:Int, y:=>Int)=1

如果CBV能终止 那么CBN一定能终止 反过来不成立

例子:

def first(x:Int , y:Int) =x

def loop()=loop

first(1,loop)

那么CBN一定可以终止的 CBV不会,CBV会在loop造成循环。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: