Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure printing a list without parentheses?

Tags:

clojure

Is there a built-in function to print the items of a list without top-level parentheses, or would there be a better way to write

(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"))

to get an output of

user=> (println-list '(1 2 3 4))
1 2 3 4 
nil
like image 216
wrongusername Avatar asked Dec 27 '11 22:12

wrongusername


2 Answers

Something like this?

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

From the 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.
like image 120
Gert Avatar answered Oct 27 '22 13:10

Gert


in clojure-contrib.string you have the function 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
like image 45
patz Avatar answered Oct 27 '22 14:10

patz