在 Clojure 中打印符号列表

发布于 2024-10-08 02:20:42 字数 410 浏览 0 评论 0原文

我试图打印出符号列表,我想知道我是否 可以删除引号。

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习ぎ惯性依靠 2024-10-15 02:20:42

('x 'y 'z)((quote x) (quote y) (quote z)) 的语法缩写。如果您确实想要一个符号列表(即 (xyz)),那么您可能在某处引用了太多。

'(x y z)          ;=> (x y z)
'('x 'y 'z)       ;=> ((quote x) (quote y) (quote z))
(list 'x 'y 'z)   ;=> (x y z)

一般来说,除非您知道自己在做什么,否则不要使用引用构建列表。请改用 list 构造函数。

另一方面,我在这里选择迭代而不是递归。这工作正常:

(doseq [sym some-list]
  (println sym))

('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.

'(x y z)          ;=> (x y z)
'('x 'y 'z)       ;=> ((quote x) (quote y) (quote z))
(list 'x 'y 'z)   ;=> (x y z)

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:

(doseq [sym some-list]
  (println sym))
人间不值得 2024-10-15 02:20:42

您应该使用 name fn 来获取符号名称。

(def my-list (list 'x 'y 'z))

(defn process-list
  [a-list]
  (map #(name %) a-list))

(process-list my-list)
;=> ("x" "y" "z")

或者通过打印

 (defn process-print-list
    [a-list]
    (doall (map #(println (name %)) a-list)) 
     nil)

  (process-print-list my-list)
  ;x
  ;y
  ;z
  ;=>nil

或者将它们组合起来以获得您想要的返回类型...

You should use name fn to get symbol name.

(def my-list (list 'x 'y 'z))

(defn process-list
  [a-list]
  (map #(name %) a-list))

(process-list my-list)
;=> ("x" "y" "z")

Or with printing

 (defn process-print-list
    [a-list]
    (doall (map #(println (name %)) a-list)) 
     nil)

  (process-print-list my-list)
  ;x
  ;y
  ;z
  ;=>nil

Or combine those to get return type you want...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文