tail-call-optimization

Scheme: Iterative process to reconstruct a list in original order?

别等时光非礼了梦想. 提交于 2021-02-11 04:55:20
问题 My question is: how to write a procedure that utilises tailcall, and that constructs a list not in the reverse order. To show what I mean, here is an example of a very simple procedure that is iterative, and that creates a copy of a list: (define (copy-list ls) (define (iter cp-ls rest-ls) (if (null? rest-ls) cp-ls (iter (cons (car rest-ls) cp-ls) (cdr rest-ls)))) (iter '() ls)) The problem is that, due to the iterative order in which the elements are cons ed together, the returned list ends

Scheme: Iterative process to reconstruct a list in original order?

不问归期 提交于 2021-02-11 04:54:20
问题 My question is: how to write a procedure that utilises tailcall, and that constructs a list not in the reverse order. To show what I mean, here is an example of a very simple procedure that is iterative, and that creates a copy of a list: (define (copy-list ls) (define (iter cp-ls rest-ls) (if (null? rest-ls) cp-ls (iter (cons (car rest-ls) cp-ls) (cdr rest-ls)))) (iter '() ls)) The problem is that, due to the iterative order in which the elements are cons ed together, the returned list ends

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,

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,

vs2010 c++ tail call optimization

旧时模样 提交于 2020-01-12 14:11:14
问题 Consider the following code: int fac_aux( int x, int res ) { if( x == 1 ) return res; else return fac_aux( x - 1, res * x ); } int fac( int x ) { return fac_aux( x, 1 ); } int main() { int x = fac( 50 ); std::cout << x; return 0; } According to generated asm file everything is ok, tail call is optimized. Try to replace int x = fac( 50 ); with int x = fac_aux( 50, 1 ); Strange enough, but tail call optimization is disappeared. As far as I remember there was no such a strange compiler behaviour

Tail recursion with Groovy

╄→尐↘猪︶ㄣ 提交于 2020-01-01 05:20:08
问题 I coded 3 factorial algorithms: First, I expect to fail by Stack Overflow. No problem. Second, I try tail recusive call , convert previous algorithm from recursive to iterative. It doesn't work but I don't understand why . Third, I use trampoline() method and works fine as I expect. def factorial factorial = { BigInteger n -> if (n == 1) return 1 n * factorial(n - 1) } factorial(1000) // Stack Overflow factorial = { Integer n, BigInteger acc = 1 -> if (n == 1) return acc factorial(n - 1, n *

Compiler Friendly Tail Recursion + Tail Recursion Checking in ATS

爱⌒轻易说出口 提交于 2019-12-23 18:11:01
问题 What is the best way to check that a function has been tail call optimized in ATS? (So far I have been running "top" to see if memory usage is constant) As a follow up: Say you have a complex tail-recursive function that the compiler has failed to TCO, is there a way to re-write it in a more compiler friendly way? Or in such a way to force the compiler to attempt TCO? 回答1: There is a peculiar way to do it in ATS2. Say you have fnx foo(...) = bar(...) and bar(...) = ...bar... If the body of