在scala中如果一个函数在最后一步调用自己(必须完全调用自己,不能加其他额外运算子),那么在scala中会复用函数栈,这样递归调用就转化成了线性的调用,效率大大的提高。
If a function calls itself as its last action, the function's stack frame can be reused. This is called tail recursion.
=> Tail recursive functions are iterative process 这里实现了两个版本的阶乘函数,一个是普通的写法,另外一个使用了tail recursive的优化
/*
In Scala, only directly recursive call to the current function are optimized.
One can require that a function is tail-recursive using a @tailrec annotation:
@tailrec
def gcd(a: Int, b: Int): Int = ...
If the annotation is given, and the implementation of gcd were not
tail recursive, an error would be issued.
*/ import scala.annotation.tailrec object exercise { def factorial(n: Int): Int =
if (n == ) else n * factorial(n-) //a tail recursive version of factorial
def factorialTailRecursion(n: Int): Int = {
@tailrec
def loop(acc: Int, n: Int): Int =
if (n == ) acc
else loop(acc * n, n-)
loop(, n)
} factorial()
factorialTailRecursion() //optimized! the function's stack frame can be reused!
}

scala tail recursive优化,复用函数栈的更多相关文章

  1. learning scala Function Recursive Tail Call

    可以使用scala库,可以从字面上看出是在调用 递归函数: code import scala.util.control.TailCalls._ val arrayDonuts: Array[Stri ...

  2. 【Scala】尾递归优化

    以递归方式思考 递归通过灵巧的函数定义,告诉计算机做什么.在函数式编程中,随处可见递归思想的运用.下面给出几个递归函数的例子: object RecursiveExample extends App{ ...

  3. 什么是sibling and tail recursive calls

    1 tail call 在函数f中调用函数b,如果这个调用是函数f中执行的最后一条指令,那么这个调用就称为tail call. 例子: int foo(float a, float b) { ... ...

  4. broadcom6838开发环境实现函数栈追踪

    在嵌入式设备开发中.内核为内核模块的函数栈追踪已经提供了非常好的支持,但用户层的函数栈追踪确没有非常好的提供支持. 在网上收集学习函数栈跟踪大部分都是描写叙述INTER体系架构支持栈帧的实现机制.或者 ...

  5. 使用模拟退火算法优化 Hash 函数

    背景 现有个处理股票行情消息的系统,其架构如下: 由于数据量巨大,系统中启动了 15 个线程来消费行情消息.消息分配的策略较为简单:对 symbol 的 hashCode 取模,将消息分配给其中一个线 ...

  6. (转)JavaScript-性能优化之函数节流(throttle)与函数去抖(debounce)

     JavaScript-性能优化之函数节流(throttle)与函数去抖(debounce)         函数节流,简单地讲,就是让一个函数无法在很短的时间间隔内连续调用,只有当上一次函数执行后过 ...

  7. 谈谈arm下的函数栈

    引言 这篇文章简要说说函数是怎么传入参数的,我们都知道,当一个函数调用使用少量参数(ARM上是少于等于4个)时,参数是通过寄存器进行传值(ARM上是通过r0,r1,r2,r3),而当参数多于4个时,会 ...

  8. c函数调用过程原理及函数栈帧分析

    转载自地址:http://blog.csdn.net/zsy2020314/article/details/9429707       今天突然想分析一下函数在相互调用过程中栈帧的变化,还是想尽量以比 ...

  9. online ddl 使用、测试及关键函数栈

    [MySQL 5.6] MySQL 5.6 online ddl 使用.测试及关键函数栈  http://mysqllover.com/?p=547 本文主要分为三个部分,第一部分是看文档时的笔记:第 ...

随机推荐

  1. centos上如何安装redis?|centos傻瓜式安装redis教程

    本文介绍centos安装redis,请不要安装2.4.3,是有问题的. 首先安装gcc yum -y install gcc yum -y install gcc-c++ yum install ma ...

  2. c#.Net:Excel导入/导出之NPOI 2.0简介

      NPOI 2.0+主要由SS, HPSF, DDF, HSSF, XWPF, XSSF, OpenXml4Net, OpenXmlFormats组成,具体列表如下: 资料来自:百度百科   Ass ...

  3. 10月24日上午PHP面向对象

    面向对象 程序分为两种,一种是面向过程的,另一种是面向对象的.之前的学的都是面向过程的,按部就班的一步一步的按照顺序往下走. 面向对象: 1.什么叫做对象 一切皆为对象(一个对象由一组属性和有权对这些 ...

  4. css 图片垂直居中总结

    1.利用vertical-align:middle: 父级元素设置成display:table-cell; 同级元素设置一个span标签 设置display:inline-block:图片样式设置ve ...

  5. jQuery知识点一 each()和toggleClass()

    jQuery的一些东东比较容易忘,所以在这里整理一下... ... 1. each (1) $(selector).each(function(index,element))         inde ...

  6. HTC学习笔记

    添加一个属性的setter, getter 建立一个页面 <html> <head> <title>TODO supply a title</title> ...

  7. App-Pass the password

    V1.0 初始版本 注册一个帐号却不想使用简单密码? Pass the Password! 输入任意字符串,如反写或截取网站域名,我们帮你生成高安全性密码. 记住规则,忘记密码 . 下一次依照你的规则 ...

  8. PAT Mooc datastructure 6-1

    Saving James Bond - Hard Version This time let us consider the situation in the movie "Live and ...

  9. class Solution(object): def fizzBuzz(self, n): a = [] i = 1 while(i <= n): if(i%15 == 0): a.append("FizzBuzz") elifleetcode day_01

    412. Fizz Buzz Write a program that outputs the string representation of numbers from 1 to n. But fo ...

  10. C和指针 第九章 字符串 字符 字节

    C语言中没有字符串类型,字符串是以NUL结尾的字符数组组成的. 高级字符串查找: //计算字符串起始部分,有多少字符是在group中 size_t strspn(char const * str, c ...