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获得唯一标识符”?谢谢 –