Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Like every tool <code>format</code> has its limitations and it's not suited for such problems very well. Probably the best you can get with plain format without resorting to black magic tricks with <code>~?</code> or <code>~/</code>, that you or anyone else probably won't understand in the future, is this code:</p> <pre><code>CL-USER&gt; (format t "~{~{~A ~}~%~}" '((X X X) (X X X X X X) (X X X X X X X X X))) X X X X X X X X X X X X X X X X X X </code></pre> <p>If you want to get your sophisticated output structure, try to do some pre-processing. Like, if the format of the list is hard-coded, you can use this:</p> <pre><code>(format t "~{~{~6A~} ~%~}" (mapcar (lambda (l) (loop :for i :from 0 :to (1- (length l)) :by (/ (length l) 3) :collect (format nil "~{~A ~}" (subseq l i (+ i (/ (length l) 3)))))) '((X X X) (X X X X X X) (X X X X X X X X X)))) </code></pre> <p>Here we first collect the items of a list into same number of groups for each list, print them and this way get 3 lists with the same number of elements, which can then be processed by <code>format</code>.</p> <p>You can find out more about <code>format</code> in the appropriate chapter of Peter Seibel's excelent Lisp book: <a href="http://gigamonkeys.com/book/a-few-format-recipes.html" rel="nofollow">http://gigamonkeys.com/book/a-few-format-recipes.html</a></p> <p><strong>EDIT</strong></p> <p>If you have a variable number of lists, with each one being twice bigger than the previous one, you'll also need to prepare the format string beforehand:</p> <pre><code>CL-USER&gt; (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 l i (+ i len/3)))))) list))) CL-USER&gt; (format-custom-list '((X X X) (X X X X X X) (X X X X X X X X X) (X X X X X X X X X X X X))) X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X NIL </code></pre> <p>(The trailing <code>nil</code> is the output of <code>format</code>, which isn't printed to the output stream <code>t</code>. If you want to get a string out of this function use <code>nil</code> as <code>format</code>'s output stream.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload