如何在Common Lisp中将列表打印为矩阵
我在CommonLisp中工作,试图制作Windows游戏扫雷器 我有一个列表如何在Common Lisp中将列表打印为矩阵,lisp,common-lisp,Lisp,Common Lisp,我在CommonLisp中工作,试图制作Windows游戏扫雷器 我有一个列表(1 1 2 2 3 3),想像矩阵一样打印出来 (1 1 1 2 2 2 3 3 3) 怎么做 编辑 我正处在人生的开始 (format t "Input width:") (setf width (read)) (format t "Input height:") (setf height (read)) (format t "How many mines:") (setf brMina (read))
(1 1 2 2 3 3)
,想像矩阵一样打印出来
(1 1 1
2 2 2
3 3 3)
怎么做
编辑
我正处在人生的开始
(format t "Input width:")
(setf width (read))
(format t "Input height:")
(setf height (read))
(format t "How many mines:")
(setf brMina (read))
(defun matrica (i j)
(cond ((= 0 i) '())
(t (append (vrsta j) (matrica (1- i) j) ))))
(setf minefield (matrica width height))
(defun stampaj ()
(format t "~%~a" minefield ))
像这样:
(defun print-list-as-grid (list rows cols)
(assert (= (length list) (* rows cols))
(loop for row from 0 below rows do
(loop for col from 0 below cols do
(princ (car list))
(princ #\space)
(setf list (cdr list)))
(princ #\newline)))
* (print-list-as-grid '(a b c d e f g h i) 3 3)
A B C
D E F
G H I
NIL
另一个例子是,为了好玩而使用漂亮的打印机:
(defun print-list-as-matrix
(list elements-per-row
&optional (cell-width (1+ (truncate (log (apply #'max list) 10)))))
(let ((*print-right-margin* (* elements-per-row (1+ cell-width)))
(*print-miser-width* nil)
(*print-pretty* t)
(format-string (format nil "~~<~~@{~~~ad~~^ ~~}~~@:>~%" cell-width)))
(format t format-string list)))
您应该发布一些您尝试过的代码。谢谢,它正在工作。第一行是(将打印列表定义为网格(列表行cols)
CL-USER> (print-list-as-matrix (loop for i from 1 to 9 collect i) 3)
1 2 3
4 5 6
7 8 9
NIL
CL-USER> (print-list-as-matrix (loop for i from 1 to 25 collect i) 5)
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
NIL
CL-USER> (print-list-as-matrix (loop for i from 1 to 16 collect i) 2)
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16