To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 4986
4985  |  4987
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:
  recursion (was RE: Would-be hacker queries.)
 
Here is an interesting article on implementing stack free recursion. (...) -- Did you check the web site first?: (URL) (26 years ago, 11-May-99, to lugnet.robotics)
  RE: Would-be hacker queries.
 
(...) Don't modern implementations of Lisp/Scheme/Logo automatically convert tail- recursion into iteration? --Todd (26 years ago, 11-May-99, to lugnet.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

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    
Active threads in Robotics

 
Verified and Trusted Team of Hackers
17 hours ago
Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR