1

我有以下一段代码

def normalFactorial = { BigInteger n ->
n <= 1 ? 1 : normalFactorial(n - 1) * n
}

println normalFactorial(1)
println normalFactorial(2)

normalFactorial(1) 方法工作正常并按预期打印 1。第二次调用失败,出现以下异常。任何线索.. ????

May 09, 2013 10:39:23 PM org.codehaus.groovy.runtime.StackTraceUtils sanitize

WARNING: Sanitizing stacktrace:

groovy.lang.MissingMethodException: No signature of method: tailRecursion.normalFactorial() is applicable for argument types: (java.math.BigInteger) values: [1]

    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
4

1 回答 1

3

定义闭包时未定义闭包(如果有意义的话)

尝试:

def normalFactorial
normalFactorial = { BigInteger n ->
  n <= 1 ? 1 : normalFactorial(n - 1) * n
}
于 2013-05-09T17:30:33.097 回答