Search code examples
clojure

binding form predefined outside let expression


"Syntax error macroexpanding" when applying the quote form to the binding form of let macro:

(def x '[y 1])
(let x y)

How to make it work if the binding form is predefined outside let expression?

@EDIT

By chance I tried locally on this problem that asks to fill the same bind form in each of the three test cases. I failed below hence came up with this question.

(def __ '[x 7 y 3 z 1])

(println
 (= 10 (let __ (+ x y)))
 (= 4 (let __ (+ y z)))
 (= 1 (let __ z)))

Solution

  • You can't predefine the let binding outside the let form. The 4clojure problem isn't asking you to do that although I can see why you would try to do what you showed (defining __ to be a literal vector).

    What 4clojure is doing behind the scenes here is taking your literal input and macroexpanding forms so that your input is placed where the __ would be. It's doing something like this:

    user=> (let [__ '[x 7 y 3 z 1]] `(let ~__ (+ ~'x ~'y)))
    (clojure.core/let [x 7 y 3 z 1] (clojure.core/+ x y))
    

    and then evaluating the result:

    user=> (clojure.core/let [x 7 y 3 z 1] (clojure.core/+ x y))
    10
    

    4clojure is expecting that literal vector, without the quote.