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

showing a distinct list of specific atoms in a list in lisp

我正在将布尔表达式建模为这样的 lisp 列表:'(NOT (AND 0 (OR B C)))
我需要编写一个函数来显示表达式中的 variables 变量是除数字、与、或之外的所有内容。 任何人都可以帮助我吗? 示例:上一个表达式的输出是:(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)