tail-recursion

How to find out if Prolog performs Tail Call Optimization

泄露秘密 提交于 2020-12-08 07:33:02
问题 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,

How to find out if Prolog performs Tail Call Optimization

Deadly 提交于 2020-12-08 07:32:15
问题 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,

Common Lisp: Why does my tail-recursive function cause a stack overflow?

♀尐吖头ヾ 提交于 2020-08-04 04:06:34
问题 I have a problem in understanding the performance of a Common Lisp function (I am still a novice). I have two versions of this function, which simply computes the sum of all integers up to a given n . Non-tail-recursive version: (defun addup3 (n) (if (= n 0) 0 (+ n (addup (- n 1))))) Tail-recursive version: (defun addup2 (n) (labels ((f (acc k) (if (= k 0) acc (f (+ acc k) (- k 1))))) (f 0 n))) I am trying to run these functions in CLISP with input n = 1000000 . Here is the result [2]>

Recursion overhead — how serious is it? [duplicate]

扶醉桌前 提交于 2020-06-24 22:26:50
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Is recursion ever faster than looping? I was first trained to program seriously in C, about 15 years ago. My employer wanted highly optimized code for computationally difficult tasks. I remember being advised more than once to rewrite recursions as loops, even at the expensive of readability, in order to avoid "recursion overhead." As I understood it then, recursion overhead was the extra effort required to push

When is tail recursion guaranteed in Rust?

陌路散爱 提交于 2020-05-14 16:44:26
问题 C language In the C programming language, it's easy to have tail recursion : int foo(...) { return foo(...); } Just return as is the return value of the recursive call. It is especially important when this recursion may repeat a thousand or even a million times. It would use a lot of memory on the stack . Rust Now, I have a Rust function that might recursively call itself a million times: fn read_all(input: &mut dyn std::io::Read) -> std::io::Result<()> { match input.read(&mut [0u8]) { Ok ( 0

Given a recursive function, how do I change it to tail recursive and streams?

天大地大妈咪最大 提交于 2020-04-11 04:20:11
问题 Given a recursive function in scheme how do I change that function to tail recursive, and then how would I implement it using streams? Are there patterns and rules that you follow when changing any function in this way? Take this function as an example which creates a list of numbers from 2-m (this is not tail recursive?) Code: (define listupto (lambda (m) (if (= m 2) '(2) (append (listupto (- m 1)) (list m))))) 回答1: I'll start off by explaining your example. It is definitely not tail

How to make this Scheme function not tail recursive?

南楼画角 提交于 2020-03-04 21:27:21
问题 I can't figure out how can I make this tail recursive Scheme function not tail recursive anymore. Anyone can help me? (define (foldrecl f x u) (if (null? x) u (foldrecl f (cdr x) (f (car x) u)))) 回答1: left folds are inheritly iterative, but you can easily make them recursive by adding a continuation. eg. (let ((value expresion-that-calculates)) value) So in your case: (define (foldrecl f x u) (if (null? x) u (let ((result (foldrecl f (cdr x) (f (car x) u)))) result))) While this looks