Comprobando cada elemento de un array en lisp.
Estoy intentando verificar cada elemento de un array de caracteres en Lisp si es igual a algunos valores significativos. Estoy usando dotimes para la iteración y declaraciones if, pero soy muy nuevo en Lisp, así que obtengo muchos errores y no sé por qué. ¿Alguien puede explicarlo?
(defun gppinterpreter (filename)
(let ((count 0)
(my-array (make-array '(1000))))
(with-open-file (stream filename)
(do ((char (read-char stream nil)
(read-char stream nil)))
((null char))
(setf (aref my-array count) char)
(incf count)))
(print my-array))
(let (c "a"))
(dotimes (n count)
(setf (aref (my-array n)) c)
(if(= c "+")
(format t "OP_PLUS ~%"))
(if(= c "-")
(format t "OP_MINUS ~%"))
(if(= c "/")
(format t "OP_DIV ~%"))
(if(= c "*")
(if(= (aref my-array (+ n 1)) "*")
(format t "OP_DBLMULT ~%"))
(format t "OP_MULT ~%"))
(if(= c "(")
(format t "OP_OP ~%"))
(if(= c ")")
(format t "OP_CP ~%"))
(if(= c " ")
(format t "OP_OC ~%"))
(if(= c " ")
(format t "OP_CC ~%"))
(if(= c ",")
(format t "OP_COMMA ~%"))
)
davy.ai
Hay algunos problemas en el código:
(let (c "a"))
. En su lugar, se debe usar(let (c "a"))
.La variable
c
no está inicializada con ningún valor. Debería ser inicializada antes del bucle.El bucle
dotimes
está faltando paréntesis alrededor del cuerpo del bucle. Todos losif
deberían estar agrupados dentro de paréntesis.Aquí está el código corregido:
En este código corregido, el bucle itera sobre los elementos de
my-array
, y la funciónchar=
se utiliza para comparar cada elemento con los caracteres especiales. La construccióncond
se utiliza para la ramificación, y todos losif
se agrupan juntos.