Like every format tool, it has its limitations, and it is not suitable for such problems very well. Probably the best thing you can get in the usual format without resorting to black magic tricks with ~? or ~/ , which you or someone else probably will not understand in the future is the code:
CL-USER> (format t "~{~{~A ~}~%~}" '((XXX) (XXXXXX) (XXXXXXXXX))) XXXXXXXXXXXXXXXXXX
If you want a complex output structure, try preprocessing. For example, if the list format is hard-coded, you can use this:
(format t "~{~{~6A~} ~%~}" (mapcar (lambda (l) (loop :for i :from 0 :to (1- (length l)) :by (/ (length l) 3) :collect (format nil "~{~A ~}" (subseq li (+ i (/ (length l) 3)))))) '((XXX) (XXXXXX) (XXXXXXXXX))))
Here we first collect the list items in the same number of groups for each list, print them and thus get 3 lists with the same number of items, which can then be processed using format .
You can learn more about format in the relevant chapter of Peter Seibel excelent Lisp book: http://gigamonkeys.com/book/a-few-format-recipes.html
EDIT
If you have a variable number of lists, and each of them is twice as large as the previous one, you also need to prepare a format string in advance:
CL-USER> (defun format-custom-list (list) (format t (format nil "~~{~~{~~~DA~~} ~~%~~}" (* 2 (length list))) (mapcar (lambda (l) (let* ((len (length l)) (len/3 (/ len 3))) (loop :for i :from 0 :to (1- len) :by len/3 :collect (format nil "~{~A ~}" (subseq li (+ i len/3)))))) list))) CL-USER> (format-custom-list '((XXX) (XXXXXX) (XXXXXXXXX) (XXXXXXXXXXXX))) XXXXXXXXXXXXXXXXXXXXX XXXXXXXXX NIL
(The final nil is the format output, which is not output to the output stream t . If you want to get a string from this function, use nil as the format output stream.)
source share