eq / eql / equal如何使用白名单?

问题描述 投票:0回答:1

[我是一个学生,我需要一点帮助,我正在做一个代码以检测回文(单词,短语或序列,其顺序与向后读相同),我有这段代码,但是eql总是给我错误,我不知道什么是错误的

 (defun palindromo()
 (let ((a) (b '(itati)))
 (print "Ingrese una lista: ")
 (setq a(read))
 (reverse b )
 (if (eql '(a) '(b) )
       (print "Verdadero")
       (print "Falso"))
 );let
 );defun

这是唯一的测试版本,原始版本可以使用任何单词或数字

common-lisp equality
1个回答
1
投票
(defun palindromo()
 (let ((a) (b '(itati)))
 (print "Ingrese una lista: ")
 (setq a(read))
 (reverse b )
 (if (eql '(a) '(b) )
       (print "Verdadero")
       (print "Falso"))
 );let
 );defun

您的代码大部分不可读。您应该花一些时间来格式化和缩进代码。

问题:

(defun palindromo ()
  (let ((a)
        (b '(itati)))             ; literal data, don't modify that later
    (print "Ingrese una lista: ")

                                  ; needs a call to (finish-output) in portable code
                                  ;  to make sure that the output actually gets printed


    (setq a (read))
    (reverse b)                   ; the value of the expression is not used
                                  ; also: don't change literal data!
    (if (eql '(a) '(b) )          ; '(a) and '(b) are constant literal expressions
                                  ; a and b will never be evaluated
                                  ; thus the expression is always false
        (print "Verdadero")
        (print "Falso"))))

要做:

  • 确保ab得到评估以进行比较
  • 使用EQUAL作为测试
  • 使用(reverse b)的结果
  • 不要将b设置为文字常量
  • 确保输出出现
© www.soinside.com 2019 - 2024. All rights reserved.