scala尾递归_Scala尾递归示例

scala尾递归

A tail recursive function in Scala is remedy if your recursive functions causes a stack overflow. Furthermore, tail recursion is a great way if to make your code faster and memory constant. With this in mind, let’s dive into how tail recursion can be implemented in Scala.

如果您的递归函数导致堆栈溢出 ,则Scala中的尾递归函数可以解决。 此外, 尾部递归是使代码更快且内存恒定的好方法。 考虑到这一点,让我们深入研究如何在Scala中实现尾递归。

The functional programming paradigm used in conjunction with Scala, promotes the usage of recursion for iterating over collections. In contrast to widely used loops or map functions, recursive iterations may reveal faster and more memory efficient. In general, recursion can be phrased as:

与Scala结合使用的函数式编程范例促进了递归在迭代集合上的使用。 与广泛使用的循环映射函数相反,递归迭代可能显示更快,更高效的内存。 通常,递归可以表述为:

Recursion solves the recursive problems by using functions that call themselves from within their own code.

递归 通过使用 从自己的代码内调用自身的 函数 来解决 递归问题

Wikipedia

维基百科

The problem that exists is that repeated call of standard recursive functions may overflow the execution stack, causing the program to crash.

存在的问题是对标准递归函数的重复调用可能会使执行堆栈溢出,从而导致程序崩溃。

尾递归 (Tail recursion)

Scala tail recursion solves the problem of stack overflow. Moreover, it handles the memory heavy numerical operations on large numbers, which can overflow the stack as well. A tail recursive functions’s last expression has to be a call to another recursive function. Because of that, records of the previous state doesn’t have to be kept.

Scala尾递归解决了堆栈溢出的问题。 此外,它还处理大量的大量内存操作,这些操作也可能使堆栈溢出。 尾递归函数的最后一个表达式必须是对另一个递归函数的调用。 因此,不必保留先前状态的记录。

Let’s learn how this works by looking at the example. Firstly, we define a factorial function as a standard recursive function. As the name suggests, the factorial functions counts the factorial of an arbitrary number.

让我们通过查看示例来了解其工作原理。 首先,我们将factorial函数定义为标准递归函数。 顾名思义, 阶乘函数计算任意数字的阶乘。

def factorial(n: Int): Int = {
if (n <= 1) 1
else n * factorial(n - 1)
}

The code above is quite simple — return the multiplication of a number and a factorial of number before, until the number is not equal or less than 1. The last expression in this example, is a mathematical operation. To make this a tail recursive function, the last expression should be exclusively call to a recursive functions. Let’s refactor this example and write the tail recursive equivalent:

上面的代码非常简单-返回一个数字和一个阶乘的乘积,直到该数字等于或小于1。在此示例中,最后一个表达式是数学运算。 为了使它成为尾部递归函数,最后一个表达式应专门调用递归函数。 让我们重构该示例并编写尾递归等效项:

import scala.annotation.tailrecdef tailRecFactorial(n: Int): BigInt = {
@tailrec
def factorialHelper(x: Int, accumulator: BigInt): BigInt = {
if (x <= 1) accumulator
else factorialHelper(x - 1, x * accumulator)
}
factorialHelper(n, 1)
}

First thing to note is the inclusion of import statement. This imports the @tailrec annotation, which explicitly marks the function as tail recursive. The tailRecFactorial functions, uses a helper function factorialHelper that does all the heavy lifting. The core this here is the accumulator parameter - this parameter saves all the intermediate results and passes its value to recursive method calls. This way the stack doesn't have to track the subsequent recursive calls. Instead, the function itself knows the actual value of intermediate result, which introduces a great memory optimization.

首先要注意的是包含import语句。 这将导入@tailrec批注,该批注将功能明确标记为尾递归。 tailRecFactorial函数使用辅助函数factorialHelper来完成所有繁重的工作。 这里的核心是累加器 参数-此参数保存所有中间结果,并将其值传递给递归方法调用。 这样,堆栈就不必跟踪后续的递归调用。 取而代之的是,函数本身知道中间结果的实际值,这带来了出色的内存优化。

And that’s really it. The tail recursive functions are first citizen in Scala and you should use them whenever possible.

就是这样。 尾递归函数是Scala中的第一位公民,您应尽可能使用它们。

摘要 (Summary)

I hope you have found this post useful. If so, don’t hesitate to like or share this post. Additionally, you can follow me on my social media if you fancy so 🙂

我希望您发现这篇文章有用。 如果是这样,请随时喜欢或分享此帖子。 此外,如果您愿意,可以在我的社交媒体上关注我me

翻译自: https://medium.com/swlh/scala-tail-recursion-by-example-47f756959911

scala尾递归

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值