tail-recursion

Implement javascript function using tail recursion

六眼飞鱼酱① 提交于 2021-02-18 16:56:32
问题 I have got a flat array representing a tree, and I want to build a nested object using tail recursion. I've got the following code to run and generate the desired output, but I am not sure if it is a proper implementation of tail recursion. Please advice :) const myArray = [ { id: 'root' }, { id: 0, parent: 'root' }, { id: 1, parent: 'root' }, { id: 2, parent: 0 }, { id: 3, parent: 1 }, { id: 4, parent: 2 }, { id: 5, parent: 1 }, { id: 6, parent: 4 }, { id: 7, parent: 0 }, { id: 8, parent: 0

Conversion of Looping to Recursive Solution

时光怂恿深爱的人放手 提交于 2021-02-11 05:12:37
问题 I have written a method pythagoreanTriplets in scala using nested loops. As a newbie in scala, I am struggling with how can we do the same thing using recursion and use Lazy Evaluation for the returning list(List of tuples). Any help will be highly appreciated. P.S: The following method is working perfectly fine. // This method returns the list of all pythagorean triples whose components are // at most a given limit. Formula a^2 + b^2 = c^2 def pythagoreanTriplets(limit: Int): List[(Int, Int,

Conversion of Looping to Recursive Solution

做~自己de王妃 提交于 2021-02-11 05:07:41
问题 I have written a method pythagoreanTriplets in scala using nested loops. As a newbie in scala, I am struggling with how can we do the same thing using recursion and use Lazy Evaluation for the returning list(List of tuples). Any help will be highly appreciated. P.S: The following method is working perfectly fine. // This method returns the list of all pythagorean triples whose components are // at most a given limit. Formula a^2 + b^2 = c^2 def pythagoreanTriplets(limit: Int): List[(Int, Int,

Does @tailrec affect compiler optimizations?

时光总嘲笑我的痴心妄想 提交于 2021-02-08 11:13:02
问题 I looked at this question trying to better understand @tailrec annotation in scala. What I'm not sure is whether the annotation also hints the compiler to do some optimizations or it's only used for warnings when you mark a method that is not a tail-recursion? More specifically - is this annotation might affect performance in any way? For example, if I don't put this annotation, the compiler will compile a tail-recursive function as non-tail recursive? 回答1: As per the scaladoc: A method

Iterative logarithmic exponentiation

。_饼干妹妹 提交于 2021-02-08 02:01:55
问题 I bombed an interview (phone screen with collabedit) recently. Here is the question: Write an interative O(lg n) algorithm for finding the power of x^y (x is a double, y>0 is an int). I first did the recursive divide and conquer one and tried to convert it to iterative... and I couldn't :S Is there a method to convert recursion to iterative (it is easy for tail recursion, but how about recursive functions with two possible recursive calls which depend on conditions to decide which call will

Iterative logarithmic exponentiation

六眼飞鱼酱① 提交于 2021-02-08 02:01:36
问题 I bombed an interview (phone screen with collabedit) recently. Here is the question: Write an interative O(lg n) algorithm for finding the power of x^y (x is a double, y>0 is an int). I first did the recursive divide and conquer one and tried to convert it to iterative... and I couldn't :S Is there a method to convert recursion to iterative (it is easy for tail recursion, but how about recursive functions with two possible recursive calls which depend on conditions to decide which call will

Converting a function to use tail recursion — a formal study

帅比萌擦擦* 提交于 2021-02-04 13:23:30
问题 Has anyone written a formal paper describing a method to (automatically) convert functions to be tail recursive? I am looking for a university-level formal treatment including the limitations (types of functions that can be converted), procedures for conversion, and, if possible, proofs of correctness? Examples in Haskell would be a bonus. 回答1: a method to (automatically) convert functions to be tail recursive? So there are two parts to this: recognizing that a given recursive function can be

Converting a function to use tail recursion — a formal study

浪尽此生 提交于 2021-02-04 13:23:05
问题 Has anyone written a formal paper describing a method to (automatically) convert functions to be tail recursive? I am looking for a university-level formal treatment including the limitations (types of functions that can be converted), procedures for conversion, and, if possible, proofs of correctness? Examples in Haskell would be a bonus. 回答1: a method to (automatically) convert functions to be tail recursive? So there are two parts to this: recognizing that a given recursive function can be

Can this implementation of Ackermann function be called tail recursive?

不打扰是莪最后的温柔 提交于 2021-01-27 07:20:14
问题 I have written following code in C. Can we call it a tail recursive implementation? #include <stdio.h> int ackermann(unsigned int *m, unsigned int *n, unsigned int* a, int* len) { if(!*m && *len == -1) { return ++*n; } else if(!*m && *len >= 0) { ++*n; *m = a[(*len)--]; } else if(*n == 0) { --*m; *n = 1; } else { ++*len; a[*len] = *m - 1; --*n; } return ackermann(m, n, a, len); } int main() { unsigned int m=4, n=1; unsigned int a[66000]; int len = -1; for (m = 0; m <= 4; m++) for (n = 0; n <

How to find out if Prolog performs Tail Call Optimization

假如想象 提交于 2020-12-08 07:33:14
问题 Using the development version of SWI Prolog (Win x64), I wrote a DCG predicate for a deterministic lexer (hosted on github) (thus all external predicates leave no choice points): read_token(parser(Grammar, Tables), lexer(dfa-DFAIndex, last_accept-LastAccept, chars-Chars0), Token) --> ( [Input], { dfa:current(Tables, DFAIndex, DFA), char_and_code(Input, Char, Code), dfa:find_edge(Tables, DFA, Code, TargetIndex) } -> { table:item(dfa_table, Tables, TargetIndex, TargetDFA), dfa:accept(TargetDFA,