Repeat a string: Difference between revisions

→‎{{header|Common Lisp}}: new version added
(→‎{{header|PARI/GP}}: add a concat version)
(→‎{{header|Common Lisp}}: new version added)
Line 337:
<lang lisp>(defun repeat-string (n string)
(with-output-to-string (stream)
(loop repeat n do (write-string string stream))))</lang>
 
A version which allocates the result string in one step:
(princ (repeat-string 5 "hi"))</lang>
 
<lang lisp>(defun repeat-string (n string
&aux
(len (length string))
(result (make-string (* n len)
:element-type (array-element-type string))))
(loop repeat n
for i from 0 by len
do (setf (subseq result i (+ i len)) string))
result)</lang>
 
<lang lisp>(princ (repeat-string 5 "hi"))</lang>
 
A single character may be repeated using just the builtin <code>make-string</code>:
Anonymous user