www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - 2.0: Call self by recursion with 'self' or something like that

reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
I just had a case where I had a function that didn't need to be named, 
except that I wanted it to be recursive.  It would have been really nice 
if I could have called it from itself with a keyword like 'self':

uint function(uint) func =
   function uint(uint i) {
     if(i == 0)
       return 0;
     else
       return i+self(i-1);
   };

Of course, I could have written it as a for loop, but sometimes 
recursion is really the thing that is most readable.

I have this weird feeling that I might have mentioned this before...but 
I didn't find it with a search.
Jan 18 2005
next sibling parent parabolis <parabolis softhome.net> writes:
Russ Lewis wrote:
 Of course, I could have written it as a for loop, but sometimes 
 recursion is really the thing that is most readable.
 
 I have this weird feeling that I might have mentioned this before...but 
 I didn't find it with a search.

rofl Recursive people tend to have the feeling they have done something before.
Jan 18 2005
prev sibling parent reply Andy Friesen <andy ikagames.com> writes:
Russ Lewis wrote:
 I just had a case where I had a function that didn't need to be named, 
 except that I wanted it to be recursive.  It would have been really nice 
 if I could have called it from itself with a keyword like 'self':
 
 uint function(uint) func =
   function uint(uint i) {
     if(i == 0)
       return 0;
     else
       return i+self(i-1);
   };
 
 Of course, I could have written it as a for loop, but sometimes 
 recursion is really the thing that is most readable.
 
 I have this weird feeling that I might have mentioned this before...but 
 I didn't find it with a search.

Why not just give it a name? -- andy
Jan 18 2005
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Andy Friesen wrote:
 Why not just give it a name?

This is basically your question to both of my messages here. I think it deserves a good answer. :) I generally agree with the idea that declaring a function, and giving it a name, is good practice. But the thing that that does is it tends to clutter up the "mindspace" of the reader. The reader has to ask himself: is this function used in more than one place? What sorts of arguments might it have? Does the guy store function pointers to it anyhow? Unnamed functions have the advantage of being quite obvious - they are only used once (unless they are stored in a function pointer, but then it's clear which function pointer they are). Their purpose is (or, at least, can be in certain contexts) clear from the context, and you don't have to give it a long name to describe it. (Yes, you can use short names, or even "Foo", but that doesn't help readability, IMHO.) I know that unnamed functions also make the code harder to read, in certain situations. I think it's a balance. Either way has its downsides, and in this particular situation I thought that an unnamed function would be more readable. That's just MHO, of course. There's nothing that says that anybody else has to agree with me :)
Jan 18 2005