将 "not" 添加到 Clojure 序列中的每个项目

Adding "not" to each item in a sequence in Clojure

我正在尝试将 (not(X)) 添加到我的所有项目 X 中。

例如:

(a b)转换为 ( (not(a)) (not(b)) )

当我使用 (map (fn [x] (not(x))) mylist) 时,它会尝试计算 nots 和 return 布尔值。

当我使用 (map (fn [x] '(not(x))) mylist) 时,它只是 return 一个 (not(x)) 的列表,而没有实际放入我的列表的变量。

(a b) --> ( (not(a)) (not(b)) )?谢谢!

user=> (map (fn [x] (list 'not (list x))) '(a b))
((not (a)) (not (b)))

' 单引号运算符可以方便地制作列表,因为它会阻止评估,但它在您的情况下不可用,因为结果列表中有您想要评估的内容。

另一个选项是 ` AKA quasiquote,它允许选择性取消引号,但也允许命名空间符号(再一次,在您的情况下没有用,您希望按字面意思使用符号)。

您可以使其更具可读性并摆脱大量 list 调用 通过使用语法引用 reader 宏:

user> (map (fn [x] `(~'not (~x))) '(a b))
((not (a)) (not (b)))

(请参阅 clojure reader 文档中关于 [语法引用] 的部分( http://clojure.org/reader)) unquote-quote not (~'not) 在这里用来插入文字 not 符号而不是命名空间前缀 clojure.core/not