Thread overview |
---|
March 21, 2007 [Issue 1076] New: by using scope(exit) tail recursion ain't working | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=1076 Summary: by using scope(exit) tail recursion ain't working Product: D Version: 1.009 Platform: PC OS/Version: Windows Status: NEW Severity: normal Priority: P2 Component: DMD AssignedTo: bugzilla@digitalmars.com ReportedBy: davidl@126.com int i; int scp( int n ){ if( n==0 ) return 0; // scope(exit) printf("%d",n); scope(exit) i++; return scp(n-1); } void main() { scp(40000); printf(`%d`,i); } //stack overflow change scope(exit) i++; by i++; then tail recursion works fine -- |
March 21, 2007 [Issue 1076] by using scope(exit) tail recursion ain't working | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1076 ------- Comment #1 from davidl@126.com 2007-03-21 13:03 ------- actually i have an idea of how to implement tail recursion with scope . err the idea here is simple, when tail recursion , just loop to the point after SEH structure has been pushed on the stack. coz we are tail recursion the same func, the SEH structure except the next SEH structure part are the same as the SEH structure we have now pushed on the stack. 1. if an exception the current func doesn't handle, then throwing it directly to the caller wouldn't be a problem, coz it would be thrown by the os recursively 2. if an exception in the current func is actually handled, then in the handler, which get called from our OS, would simply do its job, after execution of its handler func , then back to continue tail recursion not return here. it's quite a rough thought. but i think it's possible to have scope() stuff support tail recursion -- |
March 21, 2007 [Issue 1076] by using scope(exit) tail recursion ain't working | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1076 bugzilla@digitalmars.com changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |INVALID ------- Comment #2 from bugzilla@digitalmars.com 2007-03-21 13:18 ------- Tail recursion is when the *last* thing a function does is call itself. With scope (exit), this isn't the case, and so the function recurses normally. -- |
March 21, 2007 [Issue 1076] by using scope(exit) tail recursion ain't working | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1076 smjg@iname.com changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |smjg@iname.com ------- Comment #3 from smjg@iname.com 2007-03-21 13:37 ------- (In reply to comment #1) I don't see how that would help. The scope(exit) code would have to be called as many times as there are levels of recursion. In this case, where it doesn't touch any local variables, it could probably be rewritten as a for loop. But in the general case, you'd still need all the stack frames for it to work. -- |
March 22, 2007 [Issue 1076] by using scope(exit) tail recursion ain't working | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1076 ------- Comment #4 from davidl@126.com 2007-03-22 00:39 ------- err, yep , i haven't considered the following: void hello( int n) { if (n==0) return; if (n%2==1) { scope(exit) printf(`%d odd`\n,n); hello(n-1); } if (n%2==0) { scope(exit) printf(`%d even`\n,n); hello(n-1); } } -- |
Copyright © 1999-2021 by the D Language Foundation