Loo*_*oom 1 algorithm recursion functional-programming scala lazy-evaluation
我正在尝试在 Scala 中跟踪递归处理。以下是代码示例:
def factorial(n: Int): Int =
if (n <= 1) 1
else {
println("Computing factorial of " + n + " - I first need factorial of " + (n-1))
def result = n * factorial(n - 1)
println("Computed factorial of " + n)
result
}
println(factorial(3))
Run Code Online (Sandbox Code Playgroud)
以下是输出:
Computing factorial of 3 - I first need factorial of 2
Computed factorial of 3
Computing factorial of 2 - I first need factorial of 1
Computed factorial of 2
6
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为 parametern不能在 parameter 之前计算n-1。我宁愿期待以下输出:
Computing factorial of 3 - I first need factorial of 2
Computing factorial of 2 - I first need factorial of 1
Computed factorial of 2
Computed factorial of 3
6
Run Code Online (Sandbox Code Playgroud)
这种行为的原因是什么?
您期望的行为适用于以下程序:
def factorial(n: Int): Int =
if (n <= 1) 1
else {
println("Computing factorial of " + n + " - I first need factorial of " + (n-1))
val result = n * factorial(n - 1) // here is the difference def -> val
println("Computed factorial of " + n)
result
}
println(factorial(3))
Run Code Online (Sandbox Code Playgroud)
Beetween 打印您正在定义一个函数:
def result = n * factorial(n - 1)
Run Code Online (Sandbox Code Playgroud)
但这并不意味着您正在调用它。函数 ( def) 被惰性求值,而值 ( val) - 急切求值。这只是一个定义。之后,您继续进行第二个println和 on result,在那里返回值,result调用函数为 生成打印语句n - 1。