Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Collections by

I don't understand why apply vector on a list doesn't return the same collection than vector or [], when at least one of the elements of the list is a function

(= [1 2 3] (apply vector '(1 2 3)))
=> true   
(= [int 2 3] (apply vector '(int 2 3)))
=> false
(= [int 2 3] (vector int 2 3))
=> true
(apply vector '(int 2 3))
=> [int 2 3]

Clojure 1.8.0 Java HotSpot(TM) 64-Bit Server VM 1.8.0_91-b14

1 Answer

0 votes
selected by
Best answer

In the second example int is a symbol not a function. Use list instead of tick (')

(map type (apply vector '(int 2 3)))
;; => (clojure.lang.Symbol java.lang.Long java.lang.Long)

(apply vector (list int 2 3))
;; => [#function[clojure.core/int] 2 3]
Thank you, I understand now, arguments are not evaluated using tick (but somehow 2 and 3 are recognized as Longs).
cf. https://clojuredocs.org/clojure.core/list#example-54d17097e4b0e2ac61831cfe

When I type int in some expression, do I have to know what is calling it (either tick or list in this example) to assume the correct type of int ?
(map type (vector int 2 3))
=> (clojure.core$int java.lang.Long java.lang.Long)

Same behavior if i previously stored int in some function f
(def f int)
=> #'user/f
=> #object[clojure.core$int 0x44ffdf93 "clojure.core$int@44ffdf93"]
(map type [f 2 3])
=> (clojure.core$int java.lang.Long java.lang.Long)  
(map type '(f 2 3))
=> (clojure.lang.Symbol java.lang.Long java.lang.Long)
`int` or `f` are symbols which are evaluated when requested. But sometimes you want to manipulate Symbols not Vars, for that case you use tick or backtick. `'int` evaluates to Symbol but `int` is Var (function). Numbers, keywords (and other literals) have no Symbolic representation so they are the same. Please refer this document: https://clojure.org/reference/reader