Author Topic: Recursion HowTo  (Read 7902 times)

0 Members and 1 Guest are viewing this topic.

SMadsen

  • Guest
Recursion HowTo
« Reply #15 on: October 26, 2004, 10:10:32 AM »
Daron, I agree it's pretty confusing how a recursive function can deliver a result other than the input when it reverses the process upon exit.
Consider the MAKE10 function above. If you set a breakpoint and add n to the watch window, it's obvious that n is decrementing to the original input as the recursion backtraces. But it has already returned a value of 10. The important thing - as Keith mentions - is not to place any call after the recursive call because that would then be the result.

Try rewrite the MAKE10 function to this:
Code: [Select]
(defun make10 (n)
  (if (>= n 10) n
    (make10 (1+ n))
  )
  n
)


This is perhaps the most common error to make in a recursive function. It will drag its own backtrace along and return the original input - to itself.

(make10 6) -> 6

In trace window:
Code: [Select]
Entering (MAKE10 6)
  Entering (MAKE10 7)
    Entering (MAKE10 8)
      Entering (MAKE10 9)
        Entering (MAKE10 10)
        Result:  10
      Result:  9
    Result:  8
  Result:  7
Result:  6


The explicit return value will cause the function to receive whatever it pops off the stack. In this example it will return the value of n to itself - thus decrementing the value while popping it off.
When it's not superceded by another call - like in the original MAKE10 - there is nothing to return but n=10.

As for APPEND, it is by nature a recursive function due to how a list is represented with CONS pairs. I doubt it is written as a recursion in the ARX module rather than an iterative one but it is pretty easy to write your own APPEND as a recursive function:

Code: [Select]
(defun myAppend (a b)
  (cond ((null a) b)
        ((cons (car a) (myAppend (cdr a) b)))
  )
)

SMadsen

  • Guest
Recursion HowTo
« Reply #16 on: October 26, 2004, 10:15:37 AM »
Quote from: Se7en
Here is a great explination of the diff types of Recurstion.
LINK

There ya go. Excellent find, John!

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Recursion HowTo
« Reply #17 on: October 26, 2004, 11:08:32 AM »
That's a very nice start on a tutorial Stig. I look forward to your forthcoming posts in this thread.

Alas, if I only had the time, this is one of favorite topics. If there's anything left to discuss I'll try this weekend coming up, otherwise it's just short posts for me.

PS - that's a very fine book John. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Recursion HowTo
« Reply #18 on: October 26, 2004, 11:15:03 AM »
I love that book! It explains sooo muh.

Good post Stig.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

daron

  • Guest
Recursion HowTo
« Reply #19 on: October 26, 2004, 01:10:21 PM »
Yes, thank you Stig. You have a very good command of the English language. I hope I'm not the only recursion newbie learning anything on this subject. Thanks for bringing it up Mark.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Recursion HowTo
« Reply #20 on: October 26, 2004, 01:45:49 PM »
Quote from: Daron
I hope I'm not the only recursion newbie learning anything on this subject. Thanks for bringing it up Mark.

*cough* why do you think I brought it up.  :D

Thanks to all for helping Daron and myself understand recursion.  I think the trace command is one of the best ways to understand how these gems work.
TheSwamp.org  (serving the CAD community since 2003)

daron

  • Guest
Recursion HowTo
« Reply #21 on: October 26, 2004, 01:54:28 PM »
I thought you had a pretty good understanding of it, Mark.