Lisp: APPLY error when trying to generate a a-list using acons and loop

common-lisp, lisp

Solution

I can see two problems here.

- You are missing an argument list, so the `LOOP` form is taken as the argument list instead. And it happens to contain a dotted pair `(X . Y)` which is not allowed.

- Your key is a constant `'(X . Y)` when you probably wanted a pair containing the (integer) values of `X` and `Y`.

Try this:

(defun gen-world ()
    (loop for x to *hight*
        do (loop for y to *width*
            do (acons `(,x . ,y) '(rock) *world*))))

Problem

I try to run this code in clisp: ``` (defun gen-world (loop for x to *hight* do (loop for y to *width* do (acons '(x . y) '(rock) *world*)))) ``` And get the following error: ``` *** - APPLY: argument list given to SYSTEM::ERROR-OF-TYPE is dotted (terminated by "Invalid specialized parameter in method lambda list (LOOP FOR X TO *HIGHT* DO (LOOP FOR Y TO *WIDTH* DO (ACONS '(X . Y) '(ROCK) *WORLD*))): (LOOP FOR Y IN *WIDTH* DO (ACONS '(X . Y) '(ROCK) *WORLD*))" ) ``` When I run ``` (acons '(x . y) '(rock) *world*) ``` I get no errors Why do this happen? EDIT: `*hight*` `*width*` and `*world*` are global and in scope. `*world*` is a list.

Original source