开发者

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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜