سؤال

I have the following piece of code

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

println normalFactorial(1)
println normalFactorial(2)

normalFactorial(1) method works fine and prints 1 as expected. The second call fails with the below exception. Any clues.. ????

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)
هل كانت مفيدة؟

المحلول

The closure isn't defined when you define the closure (if that makes sense)

Try:

def normalFactorial
normalFactorial = { BigInteger n ->
  n <= 1 ? 1 : normalFactorial(n - 1) * n
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top