我写了两个这样的函数,但正如你所见,它们大部分是相同的,所以我想写一个宏来简化它们。
我了解教科书中的简单宏示例,但我不知道如何编写自己的。
这是我的代码:
(defn load-dict
; "Load database from a txt file previous saved"
[fname]
(with-open [rdr (io/reader fname)]
(doseq [line (line-seq rdr)]
(println line)
(def vvv (clojure.string/split line #"\s"))
;(println (str "count of vvv is " (count vvv)))
(if (< 1 (count vvv))
(add- dict (gen-word (nth vvv 0) (nth vvv 2) (nth vvv 1))))
)))
(defn load-article
; "Load article from a txt file"
[fname]
(with-open [rdr (io/reader fname)]
(doseq [line (line-seq rdr)]
(println line)
(def vvv (clojure.string/split line #"\s"))
;(println (str "count of vvv is " (count vvv)))
(if (< 1 (count vvv))
(add- article vvv ))
)))
我应该写一个像这样的宏:
(defmacro load- [target fname &expr)
`(...
(add- ~target expr)))
我实际上不知道如何编写这样的宏。我只是讨厌重复的代码。
PS,拖曳功能工作正常。我不关心变量这是代码的一部分。