在 Clojure 中打印符号列表
我试图打印出符号列表,我想知道我是否 可以删除引号。
(def process-print-list
(fn [a-list]
(cond (empty? a-list) 'false
(list? a-list) (let [a a-list] (println (first a)) (process-print-
list (rest a) ))
:else (process-print-list (rest a-list) ))))
该列表是 ('x 'y 'z)) ,
输出如下:
(quote x)
(quote y)
(quote z)
我只是想打印出来:
x
y
z
I was trying to print out a list of symbols and I was wondering if I
could remove the quotes.
(def process-print-list
(fn [a-list]
(cond (empty? a-list) 'false
(list? a-list) (let [a a-list] (println (first a)) (process-print-
list (rest a) ))
:else (process-print-list (rest a-list) ))))
the list is ('x 'y 'z))
with the following output:
(quote x)
(quote y)
(quote z)
I am Just trying to get it to print out:
x
y
z
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
('x 'y 'z)
是((quote x) (quote y) (quote z))
的语法缩写。如果您确实想要一个符号列表(即(xyz)
),那么您可能在某处引用了太多。一般来说,除非您知道自己在做什么,否则不要使用引用构建列表。请改用
list
构造函数。另一方面,我在这里选择迭代而不是递归。这工作正常:
('x 'y 'z)
is a syntactic abbreviation for((quote x) (quote y) (quote z))
. If you actually want a list of symbols (i.e.(x y z)
), you're probably quoting too much somewhere.Generally, do not construct lists using quotation unless you know what you're doing. Use the
list
constructor instead.On another note, I'd choose iteration over recursion here. This works fine:
您应该使用
name
fn 来获取符号名称。或者通过打印
或者将它们组合起来以获得您想要的返回类型...
You should use
name
fn to get symbol name.Or with printing
Or combine those to get return type you want...