我想写一个函数重复(s:String,n:Int)将连接字符串sn时间并返回它,但由于某种原因,我没有得到正确的结果,并得到一个错误,它不是尾递归,并且我在逻辑上难以理解为什么这不是尾递归。为什么这个scala并置函数不是递归的?
递归是否必须在连接完成之前进行处理?我将如何解决这个问题?使递归重复(s + s,n-1)将不起作用,因为它会递归s太多次,但我不确定还有什么其他方式来做到这一点。
/**
* Returns concatenated string s, concatenated n times
*/
@annotation.tailrec
def repeat(s: String, n: Int): String = n match {
case zero if (n == 0) => "" // zero repeats
case emptyString if (s == "") => throw new IllegalArgumentException("You must input a string") //no string exception
case fail if (n < 0) => throw new IllegalArgumentException("Cannot use a negative number") //throw exception
case last if (n == 1) => s
case concatenate => s + repeat(s, n-1) //tail-end recursion & concat.
}
PS:我这样做主要是为了练递归,而不是为了得到优化代码
这并不是尾递归,因为递归调用'repeat'之后,还有更多的工作留在返回前做的,也就是'S + ...' –
是微不足道的看看你是否以函数形式写入:'plus(s,repeat(s,minus(n,1)))'。 –