0
我想編寫一個函數(REP-N-n次&參數),它應該像一個復讀功能:如何定義與宏
user=>(rep-n-times 3 (print "hello!") (print "bye"))
hello! bye hello! bye hello! bye nil
我的代碼是
(defmacro ntimes [n & body]
`(take ~n (repeat [email protected])))
測試:
#'user/rep-n-times
user=> (rep-n-ntimes 5 (print "hah"))
hah(nil nil nil nil nil)
user=> (macroexpand '(rep-n-ntimes 4 (print "hello")))
(clojure.core/take 4 (clojure.core/repeat (print "hello")))
我該如何解決?
嗨蒂莫西!我完全同意你的看法!但是,我很抱歉,我沒有明確提出問題。我被要求編寫一個必須用宏定義的函數,而且我不會重複相同的字符串。正如我上面的問題,該函數需要一個或兩個或更多的參數。像(代表5次(打印「你好」)(打印「良好」)(打印「夜」)....)。 –
Hi Xiufen,好的,我添加了一個如何實現它作爲宏的答案的例子。 –
嗨蒂莫西!我真的很感激。我查了很多關於auto-gensym和gensym的材料,但我仍然不知道這些材料究竟是做什麼的。這意味着什麼「使用gensym獲得唯一標識符」?謝謝 –