Author Topic: How many layers can the if instruction in autolisp be nested at most?  (Read 403 times)

0 Members and 1 Guest are viewing this topic.

baitang36

  • Bull Frog
  • Posts: 213
After testing, there were no issues with the 1024 layer. I want to try layer 2048, but the vlide editor crashes.

CodeDing

  • Newt
  • Posts: 55
Re: How many layers can the if instruction in autolisp be nested at most?
« Reply #1 on: January 16, 2024, 07:56:17 PM »
baitang,

...Why tho?

...Does recursion count?

I asked my custom GPT (https://chat.openai.com/g/g-Zt0xFNpOH-autolisp-ace) to create an infinitely recursive function to accomplish this...

Code - Auto/Visual Lisp: [Select]
  1. (defun C:infRecursive ( / count)
  2.     (defun innerFunc (count)
  3.         (princ (strcat "\nCurrent count: " (itoa count)))
  4.         (if t (innerFunc (1+ count)))
  5.     )
  6.     (setq count 0)
  7.     (innerFunc count)
  8.     (princ)
  9. )
  10.  

GPT interaction:
Quote

User
create an infinitely recursive function that uses only a single (if t ...) to continue nesting. each instance should also prompt that current count to the command line

GPT
Creating an infinitely recursive function in AutoLISP that uses only a single (if t ...) to continue nesting and prompts the current count to the command line can be achieved as follows. The function will increment a counter and display it at each recursive call. To ensure that the recursion is controlled and does not crash AutoCAD, I'll include a mechanism to exit the recursion based on user input or a predefined condition.

(defun C:infRecursive ( / count)
    (defun innerFunc (count)
        (princ (strcat "\nCurrent count: " (itoa count)))
        (if t (innerFunc (1+ count)))
    )
    (setq count 0)
    (innerFunc count)
    (princ)
)

To use this function:

Load the function into AutoCAD's AutoLISP interpreter.
Run the command infRecursive in the AutoCAD command line.
Explanation:

C:infRecursive is the main function that initializes the counter and calls the inner recursive function innerFunc.
innerFunc is a recursive function that prints the current count and calls itself again if the condition (if t ...) is true, which is always the case here.
(1+ count) increments the count each time the function calls itself.
Important Note: This script will result in an infinite loop without a built-in mechanism to stop. In actual use, it's critical to have a condition to break out of the recursion to prevent software crashes or freezes. This example is purely for academic purposes and should be used with caution.
Senior CAD Tech & AI Specialist
Need AutoLisp help?
Try my custom GPT 'AutoLISP Ace'

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: How many layers can the if instruction in autolisp be nested at most?
« Reply #2 on: January 17, 2024, 04:28:11 AM »
After testing, there were no issues with the 1024 layer. I want to try layer 2048, but the vlide editor crashes.
everything has its limit :)