gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:00:28 25 4
gpt4 key购买 nike

我正在将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com