Add Comment
Recursion Fun
ColdFusion Tutorial #33
A while back I did a recursion experiment in ColdFusion by doing a Factorial function which in college is one of the classic programming examples used to teach recursion.
I'm not even sure where you would use recursion these days, except in programming classes. I have not run across the need for it in ages, but for some reason I was compelled to try this little experiment.
Is there really a need for recursion these days, or are we overlooking a power tool? Either way Coldfusion does support recursion and it does have its uses although possibly much less common these days.
demo.cfm
Well the file is so simple it explains it self.The definition of Factorial can bee seen here: http://en.wikipedia.org/wiki/Factorial
Demo
See this code running!
Download
Download this code as a zip!
Comments
Yes, there is absolutely still a need/use for recursion.In graphics/game programming, it's often used as a way to split up the screen into areas that "have or haven't been chagned and thus needing updated by the graphics engine".
In web development, I often have to use it as a way to "clone" database records. On a recent project, I had a huge hierarchial database with "parent-child-grandchild" relationships that went down over 50 levels. I was able to use recursion and pass a function "the current node in the tree, it's parent, and it's child" and the code was able to recursively "walk down the tree" and update all the levels in the database.
To do that with a non-recursive algorithm would have taken more code, and been much less flexible (the recursive idea doesn't need updating when the database schema changes, it's smart enough to figure out the updates as it runs).
I will often use recursion to solve any sort of "parent with N-levels of children" problem.
The "catch" with ColdFusion is, make SURE you "var" scope all your local variables! Otherwise the variables won't get pushed/popped onto the stack properly and you'll end up with a debugging nightmare! :)
-Nolan
http://www.southofshasta.com/blog
Nolan Erck @ Tuesday 31 Mar 2009 - 05:20:40 AM
I'd be curious what sort of stuff you work with that you haven't ran into a situation every now and then where you use recursion.
Allen @ Tuesday 31 Mar 2009 - 06:34:54 PM
I still use recursion, but not very often. Usually when traversing a directory tree.
We wrote an app last year that let users manage files in an online system. They could create any hierarchy of folders and files. When the user logged in, we recursively went through their virtual filesystem, loading everything into value objects.
Ryan Stille @ Wednesday 01 Apr 2009 - 12:39:50 AM
Recursion is handy (not necessary if it doesn't suit you) for coding reduction formulas for integrals of powers of functions (calculus), example: my web page http://www.ramapodesign.com/bruce/math/math.cfm?f=cosPower.html
ProgrammerGuy @ Monday 01 Feb 2010 - 01:20:39 AM
Click button to add a comment
Author
Wil Genovese
Published
Sunday 29 Mar 2009Original
This tutorial has been modified and published with permission of the author. The original tutorial can be found herehttp://www.trunkful.com/index.cfm/2009/2/3/Recursion-Fun