tail-recursion

How to convert this non-tail-recursion function to a loop or a tail-recursion version?

跟風遠走 提交于 2019-12-23 05:18:06
问题 I've been curious for this for long. It is really very easy and elegant for human to create a non-tail-recursive function to get something complicated, but the speed is very slow and easily hit the limit of Python recursion: def moves_three(n, ini=0, med=1, des=2): '''give a int -> return a list ''' if n == 1: return ((ini,des),) return moves_three(n-1, ini=ini, med=des, des=med) + \ ((ini, des),) + \ moves_three(n-1, ini=med, med=ini, des=des) if __name__ == '__main__': moves_three(100) #

Node.js: Are there optimizations for tail calls in async functions?

徘徊边缘 提交于 2019-12-23 02:00:14
问题 I'm using Node v8.10.0 Node.js tail-call optimization: possible or not? The above question explains how Node.js doesn't support TCO anymore. I recently experienced an issue with a function like this though: async function processBatch(nthBatch) { // do a bunch of async work and build up variables await processBatch(nthBatch + 1); } The code had a memory leak which was immediately fixed by changing this to: async function processBatch(nthBatch) { // do a bunch of async work and build up

Tail-recursion in FParsec

谁说胖子不能爱 提交于 2019-12-22 10:36:24
问题 I have encounter a problem with parsers having two branches of recursion. To demonstrate the problem easier, I use a simple grammar of a lambda calculus from the article written by Luca Bolognese as the example: <expression> ::= <name> | <function> | <application> <name> ::= non­blank character sequence <function> ::= \ <name> . <body> <body> ::= <expression> <application> ::= ( <function expression> <argument expression> ) <function expression> ::= <expression> <argument expression> ::=

Racket: Identifying tail recursion?

自古美人都是妖i 提交于 2019-12-22 07:18:08
问题 I wrote two different functions in racket to determine whether a list of numbers is ascending: (define (ascending list) (if (<= (length list) 1) #t (and (< (car list) (car (cdr list))) (ascending (cdr list))))) (define (ascending-tail list) (ascending-tail-helper #t list)) (define (ascending-tail-helper prevBool rest) (if (<= (length rest) 1) prevBool (ascending-tail-helper (and prevBool (< (car rest) (car (cdr rest)))) (cdr rest)))) I had the hardest time determining whether or not the first

Does ES6 Tail Call Optimization Cover Generators?

时光怂恿深爱的人放手 提交于 2019-12-22 04:56:36
问题 Does ES6's support for tail call optimization cover tail calls in generators? Suppose I have this generator for integers >= 0: var nums = function* (n) { n = n || 0; yield n; yield* nums(n + 1); }; Currently, in Chrome and Firefox, it adds a stack level with each recursive call and eventually runs into a "maximum call stack size exceeded" error. Will this still occur once ES6 is fully implemented? (I know I can write the above generator iteratively and not run into the error. I'm just curious

why do continuations avoid stackoverflow?

耗尽温柔 提交于 2019-12-22 03:52:54
问题 I've been trying to understand continuations / CPS and from what I can gather it builds up a delayed computation, once we get to the end of the list we invoke the final computation. What I don't understand is why CPS prevents stackoverflow when it seems analogous to building up a nested function as per the naive approach in Example 1. Sorry for the long post but tried to show the idea (and possibly where it goes wrong) from basics: So: let list1 = [1;2;3] Example 1: "Naive approach" let rec

Why does TCO require support from the VM?

孤街浪徒 提交于 2019-12-21 17:05:22
问题 Some VMs, most notably the JVM, are said to not support TCO. As a result, language like Clojure require the user to use loop recur instead. However, I can rewrite self-tail calls to use a loop. For example, here's a tail-call factorial: def factorial(x, accum): if x == 1: return accum else: return factorial(x - 1, accum * x) Here's a loop equivalent: def factorial(x, accum): while True: if x == 1: return accum else: x = x - 1 accum = accum * x This could be done by a compiler (and I've

Why does the OCaml std lib have so many non-tail-recursive functions?

北城以北 提交于 2019-12-21 07:32:38
问题 I have been rewriting many OCaml standard library functions to be tail-recursive lately. Given that this has entailed straight-forward CPS transformation, I am left puzzling over why the default versions are not written this way. As an example, in the standard library, map is defined as: let rec map f = function [] -> [] | a::l -> let r = f a in r :: map f l I have rewritten it to be: let map f l = let rec aux l k = match l with [] -> k [] | a::l -> aux l (fun rest -> k (f a :: rest)) in aux

Explain to me what the big deal with tail call optimization is and why Python needs it

喜你入骨 提交于 2019-12-20 17:31:12
问题 So apparently, there's been a big brouhaha over whether or not Python needs tail call optimization. This came to a head when someone shipped Guido a copy of SICP because he didn't "get it." I'm in the same boat as Guido. I understand the concept of tail call optimization. I just can't think of any reason why Python really needs it. To make this easier for me to understand, could someone give me a snippet of code that would be greatly simplified using TCO? 回答1: Personally, i put great value on

Explain to me what the big deal with tail call optimization is and why Python needs it

女生的网名这么多〃 提交于 2019-12-20 17:29:38
问题 So apparently, there's been a big brouhaha over whether or not Python needs tail call optimization. This came to a head when someone shipped Guido a copy of SICP because he didn't "get it." I'm in the same boat as Guido. I understand the concept of tail call optimization. I just can't think of any reason why Python really needs it. To make this easier for me to understand, could someone give me a snippet of code that would be greatly simplified using TCO? 回答1: Personally, i put great value on