Monday, January 27, 2014

AutoLISP progn error: too many arguments SOLVED

I was working on an existing AutoLISP program for AutoCAD while I found that during “Check Text in Editor” (or “Check Selection”) I got an error. It worked fine to just load and run the program but check did not. Trying to make an application (FAS or VLX) will also give this error.

; error: too many arguments: (PROGN 1 2 3 ... )

It was really confusing until I realized that progn only accepts 255 arguments. Often progn is used to together with if to allow more expressions to be evaluated and in a case like that it can easily happen that there are more than 255 arguments.

This is en example of how to reproduce this:

(progn
  1    2    3     4    5       6    7    8      9    10   11     12   13   14
  15   16   17     18   19   20    21   22      23   24   25     26   27   28
  29   30   31     32   33   34    35   36      37   38   39     40   41   42
  43   44   45     46   47   48    49   50      51   52   53     54   55   56
  57   58   59     60   61   62    63   64      65   66   67     68   69   70
  71   72   73     74   75   76    77   78      79   80   81     82   83   84
  85   86   87     88   89   90    91   92      93   94   95     96   97   98
  99   100  101     102  103  104    105  106  107  108  109     110  111  112
  113  114  115     116  117  118    119  120  121  122  123     124  125  126
  127  128  129     130  131  132    133  134  135  136  137     138  139  140
  141  142  143     144  145  146    147  148  149  150  151     152  153  154
  155  156  157     158  159  160    161  162  163  164  165     166  167  168
  169  170  171     172  173  174    175  176  177  178  179     180  181  182
  183  184  185     186  187  188    189  190  191  192  193     194  195  196
  197  198  199     200  201  202    203  204  205  206  207     208  209  210
  211  212  213     214  215  216    217  218  219  220  221     222  223  224
  225  226  227     228  229  230    231  232  233  234  235     236  237  238
  239  240  241     242  243  244    245  246  247  248  249     250  251  252
  253  254  255     256
)

The workaround I used was to add a few extra nested progn and problem solved.

Another way is to use cond like this:
(cond (T
1 2 3 all the way to 256 or higher will work fine
))

And of course create a new function with defun.

But what about using (repeat 1 …) as a workaround saving an extra pair of parentheses compared to cond? Unfortunately repeat has the same limitation of 255 expressions as progn.
; error: too many arguments: (REPEAT 1 1 2 ... )

What then is progn other than a way to bundle together a lot of expressions?
  
Progn evaluates each expression sequentially and returns the value of the last expression. In the progn example above the returned value would be 256.


Some of the latest blog posts

Subscribe to RSS headline updates from:
Powered by FeedBurner