Repeat line / character with (in format)

Is there a repeating directive for (format)in Common lisp, something like (I know this won't work):

(format t "~5C" #\*)

Just wondering if there is a more elegant way to do this than this: (from rosettacode )

(defun repeat-string (n string)
  (with-output-to-string (stream)
    (loop repeat n do (write-string string stream))))

(princ (repeat-string 5 "hi"))
+4
source share
1 answer
(defun write-repeated-string (n string stream)
  (loop repeat n do (write-string string stream)))

(write-repeated-string 5 "hi" *standard-output*))

Typically, you can use format iteration:

(format t "~v@{~A~:*~}" 5 "hi")

~Acan output all kinds of elements, not just characters. For more information, see uselpa Related Answers.

Above enumerates the iteration number from the first argument. So vbehind the tilde.

The remaining arguments will be used by iteration. In this way @.

. , ~:*.

(format t "~v{~A~:*~}" 5 '("hi")), .

+8

Source: https://habr.com/ru/post/1548461/


All Articles