[Tutor] Re: parsing--is this right?
by Derrick 'dman' Hudson other posts by this author
Jun 11 2002 8:22PM messages near this date
Re: [Tutor] parsing--is this right?
|
RE: [Tutor] loop problem
On Tue, Jun 11, 2002 at 02:55:47PM -0400, Paul Tremblay wrote:
=20
| One of the confusing things for me is that in your code, a
| function calls itself.
This is a programming style/technique known as recursion. Recursion
is really effective when the problem can be defined in terms of
itself. Parsing nested structures (the {} in your RTF) is well suited
for recursion (and hence the name 'recursive descent' parser).
Another example of recursion is the factorial operation.
n! =3D n * n-1 * n-2 * ... * 2 * 1
The "..." can be shown with recursion :
def fact( n ) :
if n =3D=3D 1 :
return 1
else:
return n * fact( n-1 )
Of course, for factorial, a faster implementation can be devised, but
the most natural definition of it is recursive.
(don't try this fact() on anything other than an integer > =3D 1
otherwise you'll get some bad results :-))
-D
--=20
"...the word HACK is used as a verb to indicate a massive amount
of nerd-like effort." -Harley Hahn, A Student's Guide to Unix
=20
Jabber ID : dman@[...].net
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg
Attachments:
unknown1
|