【发布时间】:2016-03-09 00:31:38
【问题描述】:
我想写一个函数(rep-n-times n & args),它应该像这样工作:
user=>(rep-n-times 3 (print "hello!") (print "bye"))
hello! bye hello! bye hello! bye nil
我的代码是
(defmacro ntimes [n & body]
`(take ~n (repeat ~@body)))
测试:
#'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")))
我该如何解决?
【问题讨论】: