我将布尔表达式建模为像这样的lisp列表:'(NOT(AND 0(OR B C)))
我需要编写一个在表达式中显示变量的函数
变量是除数字,AND,OR之外的所有内容。
任何身体都可以帮助我?
示例:前一个表达式的输出为:(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))))))))
答案 0 :(得分:1)
你可以这样做:
(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)