use*_*839 2 macros symbols clojure
我一直在经历clojure练习的喜悦,并且遇到了这个例子
(defn build-contract [c]
(let [args (first c)]
(list
(into '[f] args) ;; here is where i am confused
(apply merge
(for [con (rest c)]
(cond (= (first con) :require)
(assoc {} :pre (vec (rest con)))
(= (first con) :ensure)
(assoc {} :post (vec (rest con)))
:else (throw (Exception. (str "Unkown tag " (first con)))))))
(list* 'f args)))) ;; and here
(defn collect-bodies [forms]
(for [form (partition 3 forms)]
(build-contract form)))
(defmacro contract [cg & forms]
(list* `fn cg (collect-bodies forms)))
Run Code Online (Sandbox Code Playgroud)
我似乎无法理解你为什么要在那个向量中使用一个名为f的符号.符号是对事物的引用,那么将引用什么?为什么我们需要参考它?
宏构建函数文字表达式(fn表单).
(into '[f] args) ;; here is where i am confused
Run Code Online (Sandbox Code Playgroud)
这是参数向量.这里f出现的是正式论证的角色.
(list* 'f args)))) ;; and here
Run Code Online (Sandbox Code Playgroud)
这是功能应用.f是一个函数参数,这里它适用于args.
构建时,生成的定义将如下所示.
(fn name [f arg1 arg2] ... (f arg1 arg2))
Run Code Online (Sandbox Code Playgroud)