|
From: Per W. <pw...@ia...> - 2003-02-14 12:37:40
|
I think you got a bit confused about terminology.
Looping and iteration is actually the same thing.
However, recursion is not anything that should be specifically avoided.
It's a question of what problem to solve.
The trivial example of the faculty function seems to be the most quoted
example for the use of a loop instead of recursion, but the example is
quite bad, since it's faculty is easier to implement and runds faster as a
loop.
There are matematical rules that makes it possible to rewrite any
iterative function into a recursive function. And in the same way, any
recursive function may be converted to use state queues to store the
recursive state.
Use recursion when recursion results in faster run-time, or an easier
implementation. In other cases, select an iterative approach.
Examples where recursion faster and/or easier:
- light-tree evaluation in a ray tracer.
- parsing of mathematical expressions
- some recursively defined functions, such as Ackerman
- regular expression pattern matching
/Per W
On Fri, 14 Feb 2003, Jaap de Wolff wrote:
>
>
> > -----Original Message-----
> > From: Ioannis Vranos [mailto:no...@ho...]
> > Sent: Friday, February 14, 2003 07:25 AM
> > To: dev...@li...
> > Subject: Re: [Dev-C++] recursion
> >
> >
> > -------Original Message-------
> >
> > From: Santiago
> > Date: =D0=E1=F1=E1=F3=EA=E5=F5=DE, 14 =D6=E5=E2=F1=EF=F5=DC=F1=E9=EF=F2=
2003 5:53:20 =F0=EC
> > To: dev-cpp-users-list
> > Subject: [Dev-C++] recursion
> >
> > Hello all,
> > Simple one: how often, critical, and necessary is the usage
> > of C++ recursion
> > in the "real world" programming field?
> >
> >
> > =3D=3D> It has its uses, but try to avoid using it. Use loops
> > instead (usually
> > iteration).
> >
> >
> >
> > Ioannis Vranos
>
> Iteration is usefull when you have to make more of the same (iterative)
> operations on the result of itself, before you have your result.
> A traditional example of anything which can't be solved without iteration=
is
> the resolving of a regular expression.
> But of course there are more simple things which are far less complex usi=
ng
> iteration as by loops.
> As an example creating a hex string on stream.
>
> void ToHex(ostream &rstr, unsigned int n)
> {
> =09const char *HexChars =3D "0123456789ABCDEF";
> =09if (n >=3D 16)
> =09{
> =09=09ToHex(rstr, n/16);
> =09=09ToHex(rstr, n%16);
> =09}
> =09else
> =09{
> =09=09rstr << HexChars[n];
> =09}
> }
>
> Jaap
>
>
> -------------------------------------------------------
> This SF.NET email is sponsored by: FREE SSL Guide from Thawte
> are you planning your Web Server Security? Click here to get a FREE
> Thawte SSL guide and find the answers to all your SSL security issues.
> http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0026en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|