es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

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 ~%"))
 )
Tags:  ,

Answer

  1. Avatar for davy.ai

    Hay algunos problemas en el código:

    1. Hay un error de sintaxis en la línea (let (c "a")). En su lugar, se debe usar (let (c "a")).
    2. La variable c no está inicializada con ningún valor. Debería ser inicializada antes del bucle.

    3. El bucle dotimes está faltando paréntesis alrededor del cuerpo del bucle. Todos los if deberían estar agrupados dentro de paréntesis.

    Aquí está el código corregido:

    (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 c (aref my-array n))
    
            (cond
              ((char= c #\+)
               (format t "OP_PLUS ~%"))
              ((char= c #\-)
               (format t "OP_MINUS ~%"))
              ((char= c #\/)
               (format t "OP_DIV ~%"))
              ((char= c #\*)
               (if (char= (aref my-array (1+ n)) #\*)
                   (format t "OP_DBLMULT ~%")
                   (format t "OP_MULT ~%")))
              ((char= c #\()
               (format t "OP_OP ~%"))
              ((char= c #\))
               (format t "OP_CP ~%"))
              ((char= c #\{)
               (format t "OP_OC ~%"))
              ((char= c #\})
               (format t "OP_CC ~%"))
              ((char= c #\,)
               (format t "OP_COMMA ~%"))))))
    
    

    En este código corregido, el bucle itera sobre los elementos de my-array, y la función char= se utiliza para comparar cada elemento con los caracteres especiales. La construcción cond se utiliza para la ramificación, y todos los if se agrupan juntos.

Comments are closed.