有宏。这个宏很方便,因为它允许您查看执行Clojure脚本的结果。但它经常给出一个不适合我的结果。我用Babashka来运行脚本。
(defn symbol-several
"returns a symbol with the concatenation of the str values of the args"
[& x]
(symbol (apply str x)))
(defmacro ! [& forms]
(cons
`symbol-several
(for [form forms]
`(str '~form " ;-> " ~form "\n")))),产出如下:
c:\clj>bb "~@.clj"
(do (def v [3 4]) (def l (quote (1 2))) (clojure.core/sequence (clojure.core/seq (clojure.core/concat (clojure.core/list 0) l v)))) ;-> (0 1 2 3 4)~@.clj文件包含宏"!“守则如下:
"(! (do (def v [3 4]) (def l '(1 2)) `(0 ~@l ~@v)))" 如何重写宏以使其输出原始代码,即如下所示:
(do (def v [3 4]) (def l '(1 2)) `(0 ~@l ~@v))) ;-> (0 1 2 3 4)此外,宏输出的不是结果(列表),而是LazySeq:
c:\clj>bb map.clj
(apply map vector [[1 2] [3 4]]) ;-> clojure.lang.LazySeq@8041我使用Babashka (bb.exe)运行脚本。map.clj文件包含宏"!“守则如下:
(apply map vector [[1 2] [3 4]])发布于 2022-12-02 08:23:11
使用pr-str有助于正确地打印Clojure数据结构。
(defn symbol-several
"returns a symbol with the concatenation of the str values of the args"
[& x]
(symbol (apply str x)))
(defmacro ! [& forms]
`(symbol-several
~@(for [form forms]
`(str '~form " ;-> " (pr-str ~form) "\n"))))
(! (apply map vector [[1 2] [3 4]]))
;; => (apply map vector [[1 2] [3 4]]) ;-> ([1 3] [2 4])https://stackoverflow.com/questions/74651694
复制相似问题