期望:
(make-repeated-string "foo" 3)
=> "foofoofoo"
搜了一下只找到common lisp的:Lisp format a character a number of times - Stack Overflow ,用的cl的format
,参数和elisp的不一样。
大概是dotimes
+concat
?
期望:
(make-repeated-string "foo" 3)
=> "foofoofoo"
搜了一下只找到common lisp的:Lisp format a character a number of times - Stack Overflow ,用的cl的format
,参数和elisp的不一样。
大概是dotimes
+concat
?
(defun make-repeated-string (x n)
(apply #'concat
(loop repeat n
collect x)))
(apply #'concat (make-list 3 "foo"))
;; => "foofoofoo"
(cl-loop repeat 3 concat "foo")
;; => "foofoofoo"
(let (ret)
(dotimes (_ 3)
(setq ret (concat ret "foo")))
ret)
;; => "foofoofoo"
(let ((i 0)
ret)
(while (< i 3)
(setq ret (concat ret "foo")
i (+ i 1)))
ret)
;; => "foofoofoo"
(with-temp-buffer
(dotimes (_ 3) (insert "foo"))
(buffer-string))
;; => "foofoofoo"
效率上哪个比较有优势?
(apply #'concat (make-list 3 “foo”))
都差不多,或者说都足够快。