Common Lisp supports an plethora of formatting directives. However, I couldn't find a handy directive for my problem. Basically, I'd like to print a grid of numbers.
Using a list the following works nicely:
(format t "~{~A|~A|~A~%~^-----~%~}" '(1 2 3 4 5 6 7 8 9))
1|2|3
-----
4|5|6
-----
7|8|9
NIL
I was unable find a similar construct to iterate over vectors. CLtL2 states clearly that ~{...~}
expects a list as argument. I tried using a vector anyway, but my Clisp rightly exclaimed about the wrong argument type. As a workaround I convert the my vector into a throw-away list using the almighty loop
.
(let ((lst (loop for e across '#(1 2 3 4 5 6 7 8 9) collecting e)))
(format t "~{~A|~A|~A~%~^-----~%~}" lst))
1|2|3
-----
4|5|6
-----
7|8|9
NIL
This works, but it strikes me as a clumsy makeshift solution. I'd rather not create tons of temporary lists only for format
. Is there a way to iterate vectors directly?
Out of curiosity, is there a reason why format
shouldn't support sequences?