Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Fun with recursion

Blueman Steele
Registered User
Join date: 28 Dec 2004
Posts: 1,038
01-29-2006 17:32
just thought I'd share. Is there a better way?


CODE

string rev(string str) //receives and returns a string
{

if (llStringLength(str)==1) // end recursion when only 1 letter left
{
return str;
}
else
{
string front = llGetSubString(str, 0, 0); // first letter
string rest = llGetSubString(str, 1, - 1); // rest of string to end
return rev(rest) + front; // put the first letter at the back and reverse the rest.
}

}

default
{
state_entry()
{

llSay(0,rev("electroencephalograph"));
}

}
Christopher Omega
Oxymoron
Join date: 28 Mar 2003
Posts: 1,828
01-29-2006 18:43
Sure, 99% of all recursive algorythims can be written iteratively.

CODE

rev(string str) {
string ret;
integer i = llStringLength(str) - 1;
for (; i >= 0; --i) {
ret += llGetSubString(str, i, i);
}
return ret;
}
_____________________
October 3rd is the Day Against DRM (Digital Restrictions Management), learn more at http://www.defectivebydesign.org/what_is_drm
Blueman Steele
Registered User
Join date: 28 Dec 2004
Posts: 1,038
that 1%
01-30-2006 09:42
and is that one percent worth trying to learn recursion?
Davan Camus
Registered User
Join date: 13 Sep 2005
Posts: 67
01-30-2006 09:59
From: Blueman Steele
and is that one percent worth trying to learn recursion?


I think the thread title says it all: FUN with recursion! That's a fine reason.

(I suppose there are some computer science weenie reasons, as well. But: pshaw.)






:D
_____________________
Visit Cubes at Alice 100,18.
--------------------------------------------------
Davan Camus, born: 2005 September 8
Out-world location: Santa Cruz, CA
UI Proposal: http://davancamus.hexaflexagon.com/blog/?p=39
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
01-30-2006 10:07
IMO, it's usually a bad idea in an embedded-ish environment like LSL, because you can run out of memory pretty quickly. As a general programming tool, it is definitely useful, and sometimes the recursive algorithm is more elegant (and therefore easier to understand) than the equivalent iterative solution.
Pham Neutra
Registered User
Join date: 25 Jan 2005
Posts: 478
01-30-2006 10:09
From: Blueman Steele
and is that one percent worth trying to learn recursion?
It is often much easier to describe the solution to a problem as a recursive algorithm. These solutions are often astonishingly easy to understand, too. And in many cases it is not that terrible important that the algorithm is the most "efficient" one around.

So the answer is: "Yes" It is even "worth trying to learn recursion" for those problems which have an iterative solution. The recursive one is simple more "elegant". ;)
Apotheus Silverman
I write code.
Join date: 17 Nov 2003
Posts: 416
01-30-2006 10:09
I've found recursion most useful in situations where you are working with a hierarchical data structure, such as a file system with folders or some other hierarchical categorization mechanism.

I also do not buy into using it when it can be done with loops and use less memory at the same time.
_____________________
Apotheus Silverman
Shop SL on the web - SLExchange.com

Visit Abbotts Aerodrome for gobs of flying fun.
Blueman Steele
Registered User
Join date: 28 Dec 2004
Posts: 1,038
01-30-2006 10:20
I'm specifically thinking of making a script that produces a maze and find that most algorithms use recursion. (plus a tower of hanoi might be fun)

It seems, as was said, that branching areas do better with recursion, while most linear problems have an easy iterative answer.

Mind you I'm just learning to program so my example of reversing text was just the first recursion I was able to do.

.... and I'd never seen the "Heap Collision" error so many times in my entire second life.
Mack Echegaray
Registered Snoozer
Join date: 15 Dec 2005
Posts: 145
01-30-2006 11:13
Recursion should be used carefully in LSL currently as the stack is so small .. if you're sure it'll bottom out after a few levels then it's probably OK.
Jesrad Seraph
Nonsense
Join date: 11 Dec 2004
Posts: 1,463
01-30-2006 11:25
From: Christopher Omega
Sure, 99% of all recursive algorythims can be written iteratively.

I'm confident 100% of recursive algorithms can be written iteratively :D
_____________________
Either Man can enjoy universal freedom, or Man cannot. If it is possible then everyone can act freely if they don't stop anyone else from doing same. If it is not possible, then conflict will arise anyway so punch those that try to stop you. In conclusion the only strategy that wins in all cases is that of doing what you want against all adversity, as long as you respect that right in others.
Rickard Roentgen
Renaissance Punk
Join date: 4 Apr 2004
Posts: 1,869
01-30-2006 12:19
Jesrad is right. All recursion can be converted to iteration. Now recursion is occasionally more elegant but in lsl where memory is an issue and we don't have any tree-esque datatypes with variable branches/lengths, It probably shouldn't be used.
_____________________
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
01-30-2006 12:34
The funnest way to write a recursive maze generator in SL would be to use llRezObject()/llGiveInventory() as the recursive call, and llSay()/llListen() as the return. :)
Rickard Roentgen
Renaissance Punk
Join date: 4 Apr 2004
Posts: 1,869
01-30-2006 15:24
/me whaps argent over the head. bad ferret!
_____________________
Zapoteth Zaius
Is back
Join date: 14 Feb 2004
Posts: 5,634
01-30-2006 15:55
I demand one of you teach me scripting in compensation for making my head hurt so bad trying to read this!
_____________________
I have the right to remain silent. Anything I say will be misquoted and used against me.
---------------
Zapoteth Designs, Temotu (100,50)
---------------
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
01-31-2006 11:39
From: Rickard Roentgen
/me whaps argent over the head. bad ferret!
You can't tell the difference between a ferret and an otter?

Just for that, I think I'll implement it and rez it in your shoes!