I need to repeat every element of a list N times, i.e. perform this kind of transformation:
(1 2 3) => (1 1 1 2 2 2 3 3 3) ; N = 3
It is important to preserve the order of elements, i.e. first element should be repeated N times, then second, etc.
This is my best attempt so far:
(defun my-fnc (lst &optional (n 2))
(mapcan (lambda (x) (make-list n :initial-element x))
lst))
Looks like it works:
CL-USER> (defparameter *foo* '("foo" "bar"))
*FOO*
CL-USER> (setf *foo* (my-fnc *foo* 3))
("foo" "foo" "foo" "bar" "bar" "bar")
...but not quite. The problem is that former three elements are references to the same object.
("foo" "foo" "foo" "bar" "bar" "bar")
;{---------------} {---------------}
; the same string the same string
This is not what I want.
So my question is: how to solve the problem in most idiomatic way, so that every element of result list would be reference to copied separate object.