开发者

Lisp Format Procedure Applied to Arrays

I need some help with the format function and arrays.

My objective is to print a 2 dimensional array of N·N integer values as N inte开发者_运维技巧gers per line. For example:

#2A((1 2 3)
    (4 5 6)
    (7 8 9))

should be printed as

1 2 3 
4 5 6
7 8 9

I couldn't find any documentation on how to use format to print arrays. Can it actually be done, or should I convert my array into a list and use something like:

(format t "~{~%~{~A~^ ~}~}"  list)


(defun show-board (board)
  (loop for i below (car (array-dimensions board)) do
        (loop for j below (cadr (array-dimensions board)) do
          (let ((cell (aref board i j)))
            (format t "~a " cell)))
        (format t "~%")))


If I am not mistaken, there is no direct way for format to go "into" an array. You could write your own function, to be used by tilde-slash (~/function/, see CLHS), or you could coerce the array to a list and use either the directives you proposed, or ~/pprint-tabular/. If you want to define your own, the CLHS has example code for pprint-tabular which you could modify for arrays.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜