2011-12-27 131 views
10

是否有一个内置的功能打印清单的项目,而不顶级括号,或者会有写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 

回答

16

输出更好的办法像这样?

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

Clojure中,contrib.string你具备的功能加入,

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