tags:

views:

55

answers:

1

I am trying to create a map from an interleaved list and running into errors. Here is what I am doing:

(interleave ['a 'b] [1 2])

gives the list (a 1 b 2).

If I want to create a hash-map from a bunch of elements, I can do

(hash-map 'a 1 'b 2)

Combining the two together,

(hash-map ~@(interleave ['a 'b] [1 2]))

I get this error:

java.lang.IllegalStateException: Var clojure.core/unquote-splicing is unbound. (repl-1:2)

How do I pass the elements of the list to hash-map?

+4  A: 

You need to use apply:

(apply hash-map (interleave ['a 'b] [1 2]))

apply applies a function to a seq of arguments; type (doc apply) at the REPL for details.

~@ is one of the two companions to the syntax-quote, written as backtick, the other being ~:

`(~(+ 1 2) ~@[2 1])
; => (3 2 1)

The three are useful mostly for writing macros; you cannot use ~ and ~@ outside of syntax-quote.

Michał Marczyk