[Tutor] Re: parsing--is this right?
Derrick 'dman' Hudson
dman@dman.ddts.net
Tue, 11 Jun 2002 15:22:27 -0500
--JYK4vJDZwFMowpUq
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
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@dman.ddts.net
GnuPG key : http://dman.ddts.net/~dman/public_key.gpg
--JYK4vJDZwFMowpUq
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org
iEYEARECAAYFAj0GXAMACgkQO8l8XBKTpRQYRACffh49CdNjFAz/blTkXxNx+Nm8
18EAnAl4gF1qSCBgBHXe6ACXDdmoOQxO
=OmJL
-----END PGP SIGNATURE-----
--JYK4vJDZwFMowpUq--