Subject:
|
RE: Would-be hacker queries.
|
Newsgroups:
|
lugnet.robotics
|
Date:
|
Tue, 11 May 1999 16:53:27 GMT
|
Original-From:
|
Joel Shafer <joel@SPAMLESSconnect.net>
|
Viewed:
|
958 times
|
| |
| |
It's too bad that recursion pays the price of stack usage. Recursion is
almost always a more elegant solution and is more easily maintained. But
as it is, recursion should only be used when you know that the worst case
scenario won't overflow the stack. This usually implies knowing something
about the data that you are processing, and of course this isn't good
practice because data can and does change.
At 04:40 PM 5/11/99 +0000, you wrote:
> > Searching and replacing is a good example of recursion. Suppose that you
> > are editing a document and want to replace every occurrence of the word
> > "the" with the word "then". Here is one way that the program could handle it.
> >
> > Function Replace(OriginalText,SearchFor,ReplaceWith) returns modified string
> >
> > Find the first occurance of SearchFor within OriginalText
> > If SearchFor wasn't found then just return the OriginalText
> >
> > Return the text before the first occurrence + ReplaceWith + Replace(Text
> > after first occurrence) /* This is where the recursion occurs */
> >
> > End Function
> >
> > This function solves the first little piece of the problem by replacing the
> > first occurrence, and then uses recursion to solve the rest of the problem.
>
> You are right, but recursion carries a terrible price in terms of stack usage.
> Most recursion is useful for implementing search and traversal algorithms, but
> unless the hardware supports DEEP stacks for the worst case, the problem
> is almost
> always better solved the iterative way...
>
> Just my 2 cents...
>
> Cheers,
>
> Ralph Hempel - P.Eng
>
> --------------------------------------------------------
> Check out pbFORTH for LEGO Mindstorms at:
> <http://www.bmts.com/~rhempel/lego/pbFORTH/default.html>
> --------------------------------------------------------
> Reply to: rhempel at bmts dot com
> ------------------------------------------------------
> --
> Did you check the web site first?: http://www.crynwr.com/lego-robotics
Joel Shafer joel@connect.net
--
Did you check the web site first?: http://www.crynwr.com/lego-robotics
|
|
Message is in Reply To:
| | RE: Would-be hacker queries.
|
| Searching and replacing is a good example of recursion. Suppose that you are editing a document and want to replace every occurrence of the word "the" with the word "then". Here is one way that the program could handle it. Function (...) (26 years ago, 11-May-99, to lugnet.robotics)
|
21 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
Active threads in Robotics
|
|
|
|