lisp - 在 lisp 列表中显示特定原子的不同列表

标签 lisp common-lisp boolean-expression

我正在将 bool 表达式建模为 lisp 列表,如下所示:'(NOT (AND 0 (OR B C)))
我需要编写一个函数来显示表达式中的变量 变量是除数字、与、或之外的所有内容。 任何人都可以帮助我吗? 示例:上一个表达式的输出是:(B C)

这是我的尝试:

(defun print-vars (L1) 
  "Append L1 by L2." 
  (if (= 0 (list-length (L1)))
      nil 
      (if (= 1 (list-length (L1))) 
          L1 
          (cons (print-vars (remove-duplicates (first (L1)))) 
                (print-vars (remove-duplicates (rest (L1))))))))

最佳答案

你可以这样做:

(defun vars (exp &optional (res nil))
  (if exp
      (if (consp exp)
          (vars (cdr exp) (vars (car exp) res))
          (if (or (numberp exp) (member exp '(and or not))) 
              res
              (adjoin exp res)))
      res))

然后

? (vars '(NOT (AND 0 (OR B C)))) 
(C B)
? (vars '(NOT (AND C (OR B C))))
(B C)

关于lisp - 在 lisp 列表中显示特定原子的不同列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33947038/

相关文章:

recursion - LISP:如何在递归函数上不使用(Print)或(Format t)函数的情况下输出到控制台

optimization - 如何说服 Lisp SBCL 进行内联 fixnum 运算?

python - 找出哪个条件打破了逻辑 and 表达式

javascript - JS : OR operation with more than two operands?

Emacs Lisp 分割标识和删除的唯一窗口名称

macros - 带有 defclass 的 defmacro

common-lisp - SBCL 运行程序 (Stanford Parser) 或 Unix 中的重定向 I/O

macros - 如何在不使用 `eval` 的情况下编写此宏?

common-lisp - 如何在 Common Lisp 中要求关键字参数?

JavaScript 表达式 : double greater than x and greater than y