gpt4 book ai didi

lisp - 如何在 Common Lisp 中将列表打印为矩阵

转载 作者:太空宇宙 更新时间:2023-11-03 18:36:43 24 4
gpt4 key购买 nike

我在 Common Lisp 工作,试图制作 Windows 游戏扫雷。

我有一个列表 (1 1 1 2 2 2 3 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 ))

最佳答案

另一个例子,使用 pretty-printer 的乐趣:

(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)))

像这样工作:

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

关于lisp - 如何在 Common Lisp 中将列表打印为矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16507689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com