Search code examples
common-lispparallel-processingsequential

How do I create variables in sequence in common Lisp?


I have the following code inside a function that is reading in a file which is a map. I get an error that *numrows* is an illegal dimension. I think this is because lisp is processing these variables in parallel. How can I fix this?

(setq *numrows* (read map))
(setq *numcols* (read map))
(setq *map* (make-array '(*numrows* *numcols*) :initial-element nil))

Solution

  • You're misdiagnosing the problem. The first argument you're passing to MAKE-ARRAY is a list of two symbols, *NUMROWS* and *NUMCOLS*. However, the first argument to MAKE-ARRAY should be a list of non-negative integers. The easiest way to fix your example is to make a list with the values instead: (list *numrows* *numcols*). So the code would look like this instead:

    (setq *numrows* (read map))
    (setq *numcols* (read map))
    (setq *map* (make-array (list *numrows* *numcols*) :initial-element nil))
    

    You normally wouldn't use setq like this, though. It'd probably be better, depending on the context, to bind those variables with LET*:

    (let* ((numrows (read map))
           (numcols (read map))
           (map-array (make-array (list numrows numcols) :initial-element nil))
      ; do something with map-array
      )