24 Replies - 2751 Views - Last Post: 02 August 2008 - 05:38 PM
#1
Recursion
Posted 29 July 2008 - 07:17 AM
Replies To: Recursion
#2
Re: Recursion
Posted 29 July 2008 - 08:30 AM
Recursion is very clever. It can do vary particular things in an elegant way. It can functionally create a limited dynamic array by using the stack in an out side the box kind of way. I've used it for a number of cases where it was the cleanest, shortest path to the answer, but...
Recursion also seems to propagate the kind of mystical non intuitive snippets that C programmers take such joy in. In one sense, it's hard to truly grasp whats initially going on. Then, when the beginner inevitably gets there stack overflow, that answer to why it happened can be even less clear.
Recursion makes for very clean implementation of very complex algorithms, particularly the divide and conquer stuff. However, I suspect it is generally offered too soon in a curriculum to do the beginner any favors. Also, in the end, it is kind of a hack.
#3
Re: Recursion
Posted 29 July 2008 - 10:52 AM
baavgai, on 29 Jul, 2008 - 11:30 AM, said:
That's true, but I think for something like crawling a tree of nodes the alternatives are worse.
I never learned about recursion in any of my computer science classes. I actually picked it up while reading a book on Lisp.
It's really too bad they don't teach Lisp anywhere I've heard of. It's a pretty nice language that really doesn't seem to get a lot of attention anymore.
Edit: Here's a recursive function from the PHP program I'm working on, for anyone curious/bored.
/**
* Flatten a tree of nodes into a one-dimensional array.
*
* @param ctree A TreeNode.
* @return A 1d array of alternating string/boolean.
* @author Tom Arnold
*/
function tree_flatten($ctree)
{
$items = array($ctree->title, $ctree->value);
for ($i = 0; $i < sizeof($ctree->children); ++$i)
{
$more = tree_flatten($ctree->children[$i]);
for ($j = 0; $j < sizeof($more); ++$j)
{
array_push($items, $more[$j]);
}
}
return $items;
}
This post has been edited by Tom9729: 29 July 2008 - 10:54 AM
#4
Re: Recursion
Posted 29 July 2008 - 07:01 PM
#5
Re: Recursion
Posted 29 July 2008 - 07:28 PM
Radix Sorting of 32 Bit Integers
List[] = FN_BinBinSort( List[], 32 )
END
:
DEF FN_BinBinSort( InList[], Depth )
IF Depth<0 THEN =InList[]
LOCAL DIM ListArray[1]{}
LOCAL i,
FOR i=1 TO InList[].Items
b = ( InList[]{i} AND ( &1 << Depth ) >> Depth )
ListArray[b].AddItem InList[]{i}
NEXT i
IF ListArray[0].Items>1 THEN ListArray[]=FN_BinBinSort( ListArray[0], (Depth-1) )
IF ListArray[1].Items>1 THEN ListArray[]=FN_BinBinSort( ListArray[1], (Depth-1) )
RETURN ListArray[0]+ListArray[1]
END FN
Is there any cooler?
This post has been edited by AdamSpeight2008: 29 July 2008 - 07:29 PM
#6
Re: Recursion
Posted 29 July 2008 - 07:41 PM
This post has been edited by Tom9729: 29 July 2008 - 07:41 PM
#9
Re: Recursion
Posted 30 July 2008 - 05:03 AM
#10
Re: Recursion
Posted 30 July 2008 - 05:31 AM
I voted for awesome. (course that may just be because I have been working in concurrent programming recently.)
#11
Re: Recursion
Posted 30 July 2008 - 12:28 PM
AdamSpeight2008, on 30 Jul, 2008 - 03:28 AM, said:
Radix Sorting of 32 Bit Integers
List[] = FN_BinBinSort( List[], 32 )
END
:
DEF FN_BinBinSort( InList[], Depth )
IF Depth<0 THEN =InList[]
LOCAL DIM ListArray[1]{}
LOCAL i,
FOR i=1 TO InList[].Items
b = ( InList[]{i} AND ( &1 << Depth ) >> Depth )
ListArray[b].AddItem InList[]{i}
NEXT i
IF ListArray[0].Items>1 THEN ListArray[]=FN_BinBinSort( ListArray[0], (Depth-1) )
IF ListArray[1].Items>1 THEN ListArray[]=FN_BinBinSort( ListArray[1], (Depth-1) )
RETURN ListArray[0]+ListArray[1]
END FN
See snippet for a VB.Net Implementation.
Recursive Radix Sort (VB.Net Snippet)
This post has been edited by AdamSpeight2008: 30 July 2008 - 12:29 PM
#12
Re: Recursion
Posted 30 July 2008 - 01:25 PM
#13
Re: Recursion
Posted 31 July 2008 - 06:06 AM
NickDMax, on 30 Jul, 2008 - 05:31 AM, said:
I voted for awesome. (course that may just be because I have been working in concurrent programming recently.)
True. This is what I meant when I said it solves some problems cleanly. I'd be interested to hear any data on the increase in speed, on a multicore/SMP machine, from a regular merge sort to one that spins off a new threads on a split.
#14
Re: Recursion
Posted 01 August 2008 - 12:33 AM
Tom9729, on 29 Jul, 2008 - 12:52 PM, said:
/** * Flatten a tree of nodes into a one-dimensional array. * * @param ctree A TreeNode. * @return A 1d array of alternating string/boolean. * @author Tom Arnold
I never learned recursion either, so I didn't vote in your poll. But since I don't want to be left out I will learn about it (sometime).
So you're Tom Arnold, eh? How's Roseanne?
Bet you never heard that before.
Lisp? You sure that's not spelled Lithp?
lol
#15
Re: Recursion
Posted 01 August 2008 - 04:20 AM
|
|

New Topic/Question
Reply


MultiQuote











|