Mark
Mark

Reputation: 63

print list of symbols in clojure

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

Upvotes: 3

Views: 1040

Answers (2)

Nevena
Nevena

Reputation: 707

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

Upvotes: 2

Matthias Benkard
Matthias Benkard

Reputation: 15759

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

Upvotes: 7

Related Questions