Lisp format a character a number of times

后端 未结 4 1055
天命终不由人
天命终不由人 2020-12-01 21:08

I am looking for a way to output a character a number of times using format. Is this possible? Can someone fill in the _?_\'s, so that the example works?

<
4条回答
  •  一个人的身影
    2020-12-01 21:41

    (format nil "~a~:*~a~:*~a~:*" #\*)
    "***"
    

    Or elaborating a bit on Joshua Taylor's answer:

    (format nil "~v{~a~:*~}" 3 '(#\*))
    

    Would be one way of doing this. Don't be confused by the asterisks in the format directive, they are control characters, not characters being printed.


    In terms of efficiency, however, this:

    (make-string 3 :initial-element #\*)
    

    would be a preferred way to achieve the same effect.

提交回复
热议问题