Clojure打印没有括号的列表?

时间:2011-12-27 22:47:21

标签: clojure

是否有内置函数可以打印没有顶级括号的列表项,或者是否有更好的编写方法

(defn println-list
  "Prints a list without top-level parentheses, with a newline at the end"
  [alist]
  (doseq [item alist]
    (print (str item " ")))
  (print "\n"))

获得

的输出
user=> (println-list '(1 2 3 4))
1 2 3 4 
nil

2 个答案:

答案 0 :(得分:17)

这样的东西?

(apply println '(1 2 3 4 5))
1 2 3 4 5
nil

来自Clojure docs for apply

Usage: (apply f args)
       (apply f x args)
       (apply f x y args)
       (apply f x y z args)
       (apply f a b c d & args)
Applies fn f to the argument list formed by prepending intervening
arguments to args.

答案 1 :(得分:3)

在clojure-contrib.string中你有函数join,

user=> (require '[clojure.contrib.string :as string])
nil
user=> (string/join " " '(1 2 3 4 4))
"1 2 3 4 4"
user=> (println (string/join " " '(1 2 3 4 4)))
1 2 3 4 4
nil