|
Top Document: comp.cad.autocad AutoLISP FAQ (part 1/2) - general Previous Document: [13] How can I pass a variable number of arguments to a lisp Next Document: [15] (command "ROTATE3D") does not work! Why? See reader questions & answers on this topic! - Help others by sharing your knowledge
In old AutoLISP the stack size was hardcoded. It couldn't be extended,
but its size should be sufficient for most purposes. In the Visual
Lisp IDE the stack overflow is simulated at 984 recursions, on the
A2000 commandline or loaded programs outside the IDE there's no
overflow anymore. This is dangerous on recursion errors of yours,
see [9]. Most stack overflow errors occur on a program error of
yours, preventing the system from falling into an endless loop, or
from using recursive functions on large lists. Therefore you are
limited to quite short lists with recursive functions and old versions.
You cannot decrease your used stack size with using less local
parameters in your recursive function! However do not use APPLY, EVAL
or MAPCAR to call your function recursively, because they eat up the
stack. Using tail recursion doesn't help either.
You have to convert your recursive function to a iterative one.
(There is a mathematical theorem that says, that every recursive
function can be converted to a iterative one, tail-recursive ones
even automatically.) Iterative versions may use stack-like functions
like (push) and (pop), but those versions store the stack on the heap
(autolisp node space), which size is only limited by your amount of
virtual memory available.
You can test the stack overflow with this simple function:
;;; create a list of n numbers (zero based)
(defun intlst (l n)
(cond ((zerop n) l)
(T (intlst (cons (1- n) l) (1- n)))))
and try:
(setq n 100)(while (intlst nil (setq n (+ 10 n)))(print n))
In AutoLISP of R12/DOS you reach the stack limit with
(intlst nil 138), in A13/Win with (intlst nil 240), in ACOMP bi4's
with (intlst nil 1240), in Vital LISP/Visual Lisp IDE with
(intlst nil 984). With R10c10, the first dos extended lisp version,
you could enhance the lisp stack size with the environment variable
LISPSTACK. ACOMP for R10 had COMPSTACK.
With Vital LISP or Visual LISP RTS or A2000 (outside the IDE) the
stack size is unlimited.
Conversion to an iterative version yields the required results:
(defun intlst (n / l)
(repeat n
(setq l (cons (setq n (1- n)) l)))) ;this looks ugly but it works
User Contributions:Top Document: comp.cad.autocad AutoLISP FAQ (part 1/2) - general Previous Document: [13] How can I pass a variable number of arguments to a lisp Next Document: [15] (command "ROTATE3D") does not work! Why? Part1 - Part2 - Single Page [ Usenet FAQs | Web FAQs | Documents | RFC Index ] Send corrections/additions to the FAQ Maintainer: rurban@xarch.tu-graz.ac.at (Reini Urban)
Last Update March 27 2014 @ 02:11 PM
|

Comment about this article, ask questions, or add new information about this topic: