Author Topic: (help) explain recursive methods?  (Read 1006 times)

0 Members and 1 Guest are viewing this topic.

ssdd

  • Newt
  • Posts: 35
(help) explain recursive methods?
« on: January 17, 2022, 11:20:30 PM »
Code - Auto/Visual Lisp: [Select]
  1. (defun repli (l n / f)
  2.   (defun f (l i)
  3.     (cond
  4.       ((null l) l)
  5.       ((<= i 0) (f (cdr l) n))
  6.       (T (cons (car l) (f l (1- i))))
  7.     )  
  8.   )
  9.   (f l n)
  10. )


defun f ?



EDIT (John): Added code tags.

« Last Edit: January 18, 2022, 09:27:54 AM by JohnK »


LinhPham

  • Mosquito
  • Posts: 10
Re: (help) explain recursive methods?
« Reply #2 on: January 18, 2022, 01:15:07 AM »
Recursive is used when the function call itself from within.
Example, very common, a function to list all files in a folder, including subfolders.
Let name the function GetAllFiles, it can easily get all files in the top level of the folder.
But then, it finds there are many subfolders. So for each subfolder, GetAllFiles calls itself to list all file in that subfolder.
The nested calls can continue digging out more sub sub sub sub folder, but it won't be recursive forever: It will stop once the subfolder has no subfolder.
If the nested call continue forever, you got an overflow error. You have to control the parameter so that there are cases, and there must be a case, that happen and the recursive function stop calling itself.
AutoCAD Plugin Developer Freelancer

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8662
  • AKA Daniel
Re: (help) explain recursive methods?
« Reply #3 on: January 18, 2022, 01:58:12 AM »
in order to understand recursion you must first understand recursion  :laugh:

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: (help) explain recursive methods?
« Reply #4 on: January 18, 2022, 07:38:02 AM »
Hi,

LISP is basically recursive.
A LISP program is a list of expressions, an expression is either an atom or a list of expressions.
A LISP list (single linked list) is a recursive data structure, a list is either nil (empty) or a cons cell whose tail points either to nil or to a cons cell.

A recursive algorithm to count the number of items of a list should be:
the number of items equals 0 if the list is empty or 1 plus the number of items of the list but first item (cdr).
'the list is empty' is the stop condition
'the number of items of the list but first item' is a recursive call
This can be written in AutoLISP :
Code - Auto/Visual Lisp: [Select]
  1. (defun count (l)
  2.   if l
  3.     (+ 1 (count (cdr l)))
  4.     0
  5.   )
  6. )

Here's the way it works:
Code: [Select]
(count '(a b c d))
> (+ 1 (count '(b c d)))
>> (+ 1 (+ 1 (count '(c d))))
>>> (+ 1 (+ 1 (+ 1 (count '(d)))))
>>>> (+ 1 (+ 1 (+ 1 (+ 1 (count '())))))
>>>>> (+ 1 (+ 1 (+ 1 (+ 1 0))))
>>>> (+ 1 (+ 1 (+ 1 1)))
>>> (+ 1 (+ 1 2))
>> (+ 1 3)
> 4

Recursion is a natural and elegant way to process lists despite the fact it is most of the time slower and might reach a "stack overflow" if case of a (very) big number of recursive calls.
Recursion is the only way to process recursive data structures as tress (CF LinhPham's folder tree example).
Speaking English as a French Frog

ssdd

  • Newt
  • Posts: 35
Re: (help) explain recursive methods?
« Reply #5 on: January 18, 2022, 08:22:41 AM »
Thank you!Gile,It's Alive!,LinhPham,ElpanovEvgeniy。

Recursive and cyclic comparison

The recursive speed is slow, the memory consumption is high, the loop speed is fast, and the memory consumption is low

Infinite recursion leads to system crash, and infinite loops consume CPU cycles

Recursion makes the code smaller, while iteration makes the code longer.
« Last Edit: January 18, 2022, 09:41:27 AM by ssdd »

d2010

  • Bull Frog
  • Posts: 323
Re: (help) explain recursive methods?
« Reply #6 on: January 18, 2022, 02:35:45 PM »
http://www.translatetheweb.com/?from=ru&to=en&refd=mrtranslate.ru&dl=en&rr=DC&a=http%3a%2f%2felpanov.com%2findex.php%3fid%3d10
How to display the current name of function?
Can you replace ?100? with "calculate name of function?
e.g==  (princ (get-defun-name))

Code: [Select]
(Defun str_princ(lst / $rr item)
 (foreach item lst (princ item))
 (grread)
 (princ "\nThe Current defun Name=")
 (princ "?100?")
)