1

Is it possible to write a define-values macros in Clojure? Racket language provides such a thing like define-values form which acts in this way

(define -values '(a b c) (1 2 3))

Where a, b, c are global variables now. How can I do it in Clojure?


(defmacro defvar [x y]                                                     
     `(let [a# ~x                                                              
            b# ~y]                                                             
        (def b# a#)))                                                          
                                                                               
                                                                               
  (println (defvar 'a 2))


=> #'user/b__2__auto__

;;;It binds the value to  auto generated symbol

2 Answers 2

4

define-values doesn't make any sense in Clojure. It makes sense in Racket because values lets one expression evaluate to multiple values. In Clojure, an expression always evaluates to exactly one value: there's no values to extract into definitions.

Of course, you can write a macro def-several-things such that

(def-several-things [x y] [1 2])

expands to

(do (def x 1)
    (def y 2))

but this is less readable, not more, so nobody does it.

Sign up to request clarification or add additional context in comments.

4 Comments

I just want to know how to do that
@user324885 i.imgur.com/ccTqs7N.png (defmacro def-several-things [symbols values] `(do ~@(map (fn [s v] (list 'def s v)) symbols values))) (macroexpand '(def-several-things [x y] [1 2]))
@user2609980 If you have an answer to the question, please post it as an Answer, not as a comment on my answer.
@amalloy this is an answer to the comment. @user324885 asked how to write out your suggestion of using a macro def-several-things.
1

Note you can also use clojure.template/do-template:

(clojure.template/do-template
  [name val]
  (def name val) a 1 b 2 c 3)

this will give you vars a b c with vals 1 2 3.

Expansion:

(clojure.walk/macroexpand-all '(do-template [name v] (def name v) a 1 b 2 c 3))
;; => (do (def a 1) (def b 2) (def c 3))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.