Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Demo of recursion and ListReverse

Wyatt Burton
Registered User
Join date: 11 Jan 2007
Posts: 49
12-12-2007 17:54
Hi everyone. I wrote this function to demonstrate recursion where a function calls itself. This function reverses a list. I thought others may benefit from it and the method used. (See attachment). Just add the script to any prim and then click on prim to see it run the test cases. Feel free to copy use it improve it transfer give it to your sibling etc etc.

Some caveats:

It cannot reverse large lists because of SLs stack/heap limitations. My system does abot 40 items.

It is slow but then LSL is not C++ (just because it has curly brackets and ++ operator it doesn't it make it C++)

Why use recursion? Because it's there, it's elegant coding and many times it is less code. Mainly used to manipulate data structures (E.g. sorting, searching, etc) and parsing strings.
Xhawkx Holden
Registered User
Join date: 1 Nov 2006
Posts: 86
12-12-2007 18:02
Very nice...
Thank you Wyatt...
I hadn't even thought of trying a recursive function.....
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
12-12-2007 20:08
generalizations
recursion - less code at the cost of more memory use
loops - more code at the cost of less memory use

speed and readability are dependant on the usage some problems express better one way than the other
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
12-13-2007 09:34
From: Wyatt Burton
It cannot reverse large lists because of SLs stack/heap limitations. My system does abot 40 items.


Anybody remember if using "myList = (myList = []) + myList + [newItem]" decreases memory or the time it takes to add to a list? If it's a size thing, you could use that in a couple places to save a few bytes, Wyatt..
_____________________
Tired of shouting clubs and lucky chairs? Vote for llParcelSay!!!
- Go here: http://jira.secondlife.com/browse/SVC-1224
- If you see "if you were logged in.." on the left, click it and log in
- Click the "Vote for it" link on the left