私は次のマクロをlispの土地からclojureに変換しようとしています:
(defmacro tag (name atts &body body)
`(progn (print-tag ',name
(list ,@(mapcar (lambda (x)
`(cons ',(car x) ,(cdr x)))
(pairs atts)))
nil)
,@body
(print-tag ',name nil t)))
しかし、私はもう1つのレベルの評価を必要とするattsで立ち往生し続けています。たとえば、次のようにt#を評価する必要があります。
(defmacro tag [tname atts & body]
`(do (print-tag '~tname '[~@(map (fn [[h# t#]] [h# t#]) (pair atts))] nil)
~@body
(print-tag '~tname nil true)))
それは次のようなものを生成するので:
(tag mytag [color 'blue size 'big])
<mytag color="(quote blue)" size="(quote big)"><\mytag>
属性を評価したい場所。上記で「(evalt#)」を使用すると、次のような問題が発生します。
(defn mytag [col] (tag mytag [colour col]))
java.lang.UnsupportedOperationException: Can't eval locals (NO_SOURCE_FILE:1)
助言がありますか?
Clojureで評価のレベルが1つ下がったように見えるのはなぜですか?
サポート機能の定義:
;note doesn't handle nils because I'm dumb
(defn pair [init-lst]
(loop [lst init-lst item nil pairs []]
(if (empty? lst)
pairs
(if item
(recur (rest lst) nil (conj pairs [item (first lst)]))
(recur (rest lst) (first lst) pairs)))))
(defn print-tag [name alst closing]
(print "<")
(when closing
(print "\\"))
(print name)
(doall
(map (fn [[h t]]
(printf " %s=\"%s\"" h t))
alst))
(print ">"))
(何らかの理由で、本と同じようにペア関数を実行しなかったため、nilsが正しく処理されません)