digitalmars.D.bugs - [Issue 1076] New: by using scope(exit) tail recursion ain't working
- d-bugmail puremagic.com (26/26) Mar 21 2007 http://d.puremagic.com/issues/show_bug.cgi?id=1076
- d-bugmail puremagic.com (17/17) Mar 21 2007 http://d.puremagic.com/issues/show_bug.cgi?id=1076
- d-bugmail puremagic.com (10/10) Mar 21 2007 http://d.puremagic.com/issues/show_bug.cgi?id=1076
- d-bugmail puremagic.com (12/12) Mar 21 2007 http://d.puremagic.com/issues/show_bug.cgi?id=1076
- d-bugmail puremagic.com (18/18) Mar 21 2007 http://d.puremagic.com/issues/show_bug.cgi?id=1076
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
--
Mar 21 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1076 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 --
Mar 21 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1076
bugzilla digitalmars.com changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |INVALID
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.
--
Mar 21 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1076
smjg iname.com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |smjg iname.com
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.
--
Mar 21 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1076
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);
}
}
--
Mar 21 2007









d-bugmail puremagic.com 