可以使用scala库,可以从字面上看出是在调用 递归函数:

code

import scala.util.control.TailCalls._

  val arrayDonuts: Array[String] = Array("Vanilla Donut", "Strawberry Donut", "Plain Donut", "Glazed Donut")

  println("\nStep : How to define a tail recursive function using scala.util.control.TailCalls._")
def tailSearch(donutName: String, donuts: Array[String], index: Int): TailRec[Option[Boolean]] = {
if(donuts.length == index) {
done(None) // NOTE: done is imported from scala.util.control.TailCalls._
} else if(donuts(index) == donutName) {
done(Some(true))
} else {
val nextIndex = index +
tailcall(tailSearch(donutName, donuts, nextIndex)) // NOTE: tailcall is imported from scala.util.control.TailCalls._
}
} println("\nStep : How to call tail recursive function using scala.util.control.TailCalls._")
val tailFound = tailcall(tailSearch("Glazed Donut", arrayDonuts, ))
println(s"Find Glazed Donut using TailCall = ${tailFound.result}") // NOTE: our returned value is wrapped so we need to get it by calling result val tailNotFound = tailcall(tailSearch("Chocolate Donut", arrayDonuts, ))
println(s"Find Chocolate Donut using TailCall = ${tailNotFound.result}")

resule:

Step : How to define a tail recursive function using scala.util.control.TailCalls._

Step : How to call tail recursive function using scala.util.control.TailCalls._
Find Glazed Donut using TailCall = Some(true)
Find Chocolate Donut using TailCall = None

  

learning scala Function Recursive Tail Call的更多相关文章

  1. learning scala Function Composition andThen

    Ordering using andThen: f(x) andThen g(x) = g(f(x)) Ordering using compose: f(x) compose g(x) = f(g( ...

  2. [machine learning] Loss Function view

    [machine learning] Loss Function view 有关Loss Function(LF),只想说,终于写了 一.Loss Function 什么是Loss Function? ...

  3. learning scala How To Create Variable Argument Function - varargs :_ *

    Scala collection such as List or Sequence or even an Array to variable argument function using the s ...

  4. learning scala How To Create Implicit Function

    println("Step 1: How to create a wrapper String class which will extend the String type") ...

  5. learning scala PartialFunction

    Partial函数的定义 scala> val isVeryTasty: PartialFunction[String, String] = { case "Glazed Donut& ...

  6. learning scala generic classes

    package com.aura.scala.day01 object genericClasses { def main(args: Array[String]): Unit = { val sta ...

  7. learning scala extractor object

    package com.aura.scala.day01 import scala.util.Random object extractorObject { def main(args: Array[ ...

  8. Deep Learning: Activation Function

    Sigmoid Function ReLU Function Tanh Function

  9. [Reinforcement Learning] Value Function Approximation

    为什么需要值函数近似? 之前我们提到过各种计算值函数的方法,比如对于 MDP 已知的问题可以使用 Bellman 期望方程求得值函数:对于 MDP 未知的情况,可以通过 MC 以及 TD 方法来获得值 ...

随机推荐

  1. 文件类型分类:头文件dirent.h中定义的文件类型与linux内文件符号对应关系

    头文件 dirent.h 定义了文件类型: enum{    DT_UNKNOWN = 0,         //未知类型    DT_FIFO = 1,            //first in, ...

  2. FFMPEG - ffplay源代码分析

    FFmpeg是一个开源,免费,跨平台的视频和音频流方案,它提供了一套完整的录制.转换以及流化音视频的解决方案.而ffplay是有ffmpeg官方提供的一个基于ffmpeg的简单播放器.学习ffplay ...

  3. 织梦安全防护:禁止uploads、data、templets执行脚本

    下面介绍下如何针对uploads.data.templets做PHP脚本限制:对uploads.data.templets 三个目录做执行php脚本限制,就算被上传了木马文件到这些文件夹,也是无法运行 ...

  4. The 2018 ACM-ICPC Asia Nanjing Regional Programming Contest

    A. Adrien and Austin 大意: $n$个石子, 编号$1$到$n$, 两人轮流操作, 每次删除$1$到$k$个编号连续的石子, 不能操作则输, 求最后胜负情况. 删除一段后变成两堆, ...

  5. iOS10推送必看UNNotificationAttachment以及UNTimeIntervalNotificationTrigger

    虽然这篇文章比较长,也不好理解,但是还是建议大家收藏,以后用到的时候,可以看看,有耐心的还是读一读. 这篇文章开始,我会跟大家好好讲讲,苹果新发布的iOS10的所有通知类. 一.创建本地通知事例详解: ...

  6. 修改mysql/MariaDB数据库的端口号+远程

    1.修改端口 2.远程+开放端口 (1)设置远程账号:xxx和密码yyyyyyygrant all privileges on *.* to 'xxx'@'%' identified by 'yyyy ...

  7. SQLSEVER 同台服务器下不同表 触发器实现数据实时同步

    触发器的使用: 1.首先建立两个相同结构的表,两个表明的列的名称不同. student_01   字段  name  ;  字段 age  ; 字段  class ; student_02   字段  ...

  8. C# - 配置动态更新

    生产中经常会遇到修改配置的情况,但是又需要重启应用程序,是不是有点小烦躁.... 下面了解下在不重启情况下,实现配置更新实时生效 public static void SetConfig(string ...

  9. Access denied for user 'test'@'%' to database 'mysql'

    1.问题描述 今天使用MySQL新建了一个用户,此处假设为test用户,用来作为某安装软件的配置用户(会新建大量的表及视图) mysql> create user 'test'@'%' iden ...

  10. javascript之ECMAScript:语法的操作标准

    一.如何书写一个javascript代码 javascript代码需要写在javascript标签中才会生效,而javascript标签可以写在任何地方,但考虑到规范化及页面的加载问题,最好是写在bo ...