Let Over Lambda for common lisp 中描述了这样的宏。您必须包装您的代码(with-cxrs ...)
以将它们全部纳入范围,但它会遍历您的代码以查看您需要哪些组合器。几年前我为了好玩而写了一个 Clojure 移植版,当然没有人(包括我)想要真正使用它。如果您愿意,可以将其移植到 Scheme。
(defn cxr-impl [name]
(when-let [op (second (re-matches #"c([ad]+)r" name))]
`(comp ~@(map {\a `first \d `rest} op))))
(defmacro with-cxrs [& body]
(let [symbols (remove coll? (tree-seq coll? seq body))]
`(let [~@(for [sym symbols
:let [impl (cxr-impl (name sym))]
:when impl
thing [sym impl]]
thing)]
~@body)))
user> (macroexpand-1 '(with-cxrs (inc (caadaaddadr x))))
(let [caadaaddadr (comp first first rest first first rest rest first rest)]
(inc (caadaaddadr x)))
https://groups.google.com/g/clojure/c/CanBrJPJ4aI/m/S7wMNqmj_Q0J
如邮件列表线程中所述,如果您想真正使用它,则必须解决一些错误。