tail-recursion

Is Tail Call optimization all that explains this performance difference

╄→尐↘猪︶ㄣ 提交于 2019-12-25 06:32:04
问题 I saw three different ways to write the recursive form of a Fibonacci function: With the math inline, With the math inline with result caching and one Using tail recursion with. I understand using memoization converts an O(N) algorithm to an O(1) after the answers are cached. But I fail to understand how tail call optimization can help this much. I was under the impression it might prevent a few copies or something like that. It is nearly as fast as the O(1). What is Ruby doing that makes

Recursion removing list elements Python

不羁岁月 提交于 2019-12-24 22:45:25
问题 need to write a recurive function to remove elements of list but keep the arrays in the result such as. def remove_words(listx): for element in listx: if isinstance(element, list): remove_words(element) else: listx.remove(element) return listx remove_words(["a", "b", ["c"]]) would return [[]] My code returns ["b",[]] for some reason it's missing an element. 回答1: Do not remove elements from a collection while iterating it. Repeated calls to list.remove are also not ideal performance-wise since

Tail-Recursive Power Function in Scheme

时间秒杀一切 提交于 2019-12-24 00:57:55
问题 I am having trouble writing a tail-recursive power function in scheme. I want to write the function using a helper function. I know that I need to have a parameter to hold an accumulated value, but I am stuck after that. My code is as follows. (define (pow-tr a b) (define (pow-tr-h result) (if (= b 0) result pow-tr a (- b 1))(* result a)) pow-tr-h 1) I edited my code, and now it works. It is as follows: (define (pow-tr2 a b) (define (pow-tr2-h a b result) (if (= 0 b) result (pow-tr2-h a (- b

Recursive call not in tail position when using for comprehensions

China☆狼群 提交于 2019-12-24 00:36:25
问题 I don't know if I am doing something wrong or it is just a property of the Scala compiler - I get the mentioned compilation error when I try to compile this code: @tailrec def shiftDown2(x: Int, bound: Int) { val childOfX = chooseChild(x, bound) for (child <- childOfX) { swap(x, child) shiftDown2(child, bound) } } whilst the following code compiles without problems: @tailrec def shiftDown(x: Int, bound: Int) { val childOfX = chooseChild(x, bound) if (childOfX.isDefined) { swap(x, childOfX.get

Incomplete pattern matching a tuple in F#

孤者浪人 提交于 2019-12-23 20:14:44
问题 I define a point type TimeSeriesPoint<'T> = { Time : DateTimeOffset Value : 'T } and a series type TimeSeries<'T> = TimeSeriesPoint<'T> list where I assume the points in this list are ordered by time. I am trying to zip two time series, where, in general, they will have points with the same time, but there might be some points missing in either of them. Any idea why I get a warning for incomplete pattern matches in the code below? let zip (series1 : TimeSeries<float>) (series2 : TimeSeries

Why does this tail-call recursive fibonacci function break with gcc -O2? [closed]

血红的双手。 提交于 2019-12-23 18:12:08
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I implemented the following tail-call recursive fibonacci function to try out gcc's Tail Call Optimization (as mentioned here): #include <stdint.h> uint64_t fib_tc( uint8_t n) { uint64_t fib_helper(uint8_t n, uint64_t acc, uint64_t prev) { if(n == 0) return acc; else return fib_helper( n-1, acc+prev, acc); } fib

Tail recursive algorithm for generating all topological orderings in a graph

有些话、适合烂在心里 提交于 2019-12-23 15:14:23
问题 Given a graph i need to generate all topological orderings. For instance, given the following graph: i want to generate all topological orderings, which are: 2 4 7 5 2 7 4 5 2 4 5 7 Because many topological orderings may exist, I need to generate them lazily. Currently, I have a working implementation that is recursive and works on top of the scala-graph library: import scalax.collection.Graph import scalax.collection.GraphPredef._ import scalax.collection.GraphEdge._ import scala.collection

Tail recursive functions for BinaryTree

删除回忆录丶 提交于 2019-12-23 14:44:33
问题 I am stuck with implementing tail recursive foreach, reduce, map and toList functions for a very simple implementation of binary tree. sealed trait Tree[+A] case object EmptyTree extends Tree[Nothing] case class Node[A](value: A, left: Tree[A], right: Tree[A]) extends Tree[A] object Tree { def apply[A]: Tree[A] = EmptyTree def apply[A](value: A): Tree[A] = Node(value, EmptyTree, EmptyTree) def apply[A](value: A, left: Tree[A], right: Tree[A]): Tree[A] = Node(value, left, right) def foreach[A]

Scala tailrec annotation error

自闭症网瘾萝莉.ら 提交于 2019-12-23 12:34:11
问题 I have a Java abstract class called ImmutableEntity and several subclasses that contain a class-level annotation called @DBTable . I am trying to search a class hierarchy for the annotation using a tail-recursive Scala method: def getDbTableForClass[A <: ImmutableEntity](cls: Class[A]): String = { @tailrec def getDbTableAnnotation[B >: A](cls: Class[B]): DBTable = { if (cls == null) { null } else { val dbTable = cls.getAnnotation(classOf[DBTable]) if (dbTable != null) { dbTable } else {

Free ~> Trampoline : recursive program crashes with OutOfMemoryError

两盒软妹~` 提交于 2019-12-23 09:36:37
问题 Suppose that I'm trying to implement a very simple domain specific language with only one operation: printLine(line) Then I want to write a program that takes an integer n as input, prints something if n is divisible by 10k, and then calls itself with n + 1 , until n reaches some maximum value N . Omitting all syntactic noise caused by for-comprehensions, what I want is: @annotation.tailrec def p(n: Int): Unit = { if (n % 10000 == 0) printLine("line") if (n > N) () else p(n + 1) } Essentially