Subject:
|
RE: Would-be hacker queries.
|
Newsgroups:
|
lugnet.robotics
|
Date:
|
Tue, 11 May 1999 16:40:27 GMT
|
Viewed:
|
998 times
|
| |
| |
> 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
------------------------------------------------------
|
|
Message has 2 Replies:
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
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
Active threads in Robotics
|
|
|
|