Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are these Clojure lists different?

Tags:

clojure

I'm running through some of the 4Clojure problems and hit some weird behavior with some of my code. Upon further investigation it seems the culprit was using the quote macro vs list function. Why does this matter in the code below, and why does it produce the incorrect result?

user=> (= (class '(/ 1 2)) (class (list / 1 2)))
true
user=> (def a '(/ 1 2))
#'user/a
user=> (def b (list / 1 2))
#'user/b
user=> (class a)
clojure.lang.PersistentList
user=> (class b)
clojure.lang.PersistentList
user=> (apply (first a) (rest a))
2
user=> (apply (first b) (rest b))
1/2
user=> (class (first a))
clojure.lang.Symbol
user=> (class (first b))
clojure.core$_SLASH_
like image 650
JamesSwift Avatar asked Jul 22 '26 02:07

JamesSwift


1 Answers

'(/ 1 2)

is analogous to:

(list '/ 1 2)

When you don't quote /, you get its value, which is the built-in division function, rather than the symbol.

like image 114
Barmar Avatar answered Jul 23 '26 19:07

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!