lisp - 如何在列表中放置一个元素? (口齿不清)

标签 lisp common-lisp

(defun tictactoe3d ()
'(
 ((NIL NIL NIL) (NIL NIL NIL) (NIL NIL NIL))
 ((NIL NIL NIL) (NIL NIL NIL) (NIL NIL NIL))
 ((NIL NIL NIL) (NIL NIL NIL) (NIL NIL NIL))
))

我需要一个函数来添加 X 或 O 来代替 NIL,我需要该函数来询问用户他想把它放在哪里。棋盘游戏是 tic tac toe 3D(3 个棋盘而不是 1 个,27 个位置而不是 9 个)。第一行是 1 级(它基本上与一个有 9 个位置的井字游戏相同)。如何将元素添加到这样的列表中。我知道我必须验证职位是否为零。

最佳答案

您返回的列表是一个常量。你不能改变常量,因为它有 undefined consequences .如果您想使用纯功能数据结构,您可以定义返回板的修改副本的操作。

你正在使用一个列表,而我认为你真的应该使用一个数组:毕竟,一个板实际上是一个固定大小的网格。以下是您如何分配它:

(make-array '(3 3 3) :initial-element nil)

=> #3A(((nil nil nil) (nil nil nil) (nil nil nil))
       ((nil nil nil) (nil nil nil) (nil nil nil))
       ((nil nil nil) (nil nil nil) (nil nil nil)))

然后,您使用 aref(setf aref):

(setf (aref board z y x) value)

关于lisp - 如何在列表中放置一个元素? (口齿不清),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33872902/

相关文章:

lisp - 使用 lisp 生成随机数列表列表

file - 如何在 Common Lisp 中进行批量文件编辑?

lisp - Scheme 中的列表操作

Lisp 随机化并使用两个函数将列表拉入另一个

functional-programming - 在一行中打印答案、 "should be"和所需的答案 [方案(初级学生语言)]

Lisp排序函数

lisp - 如何将列表列表传递给函数?

lambda - 了解非法嵌套 `lambda` 调用

performance - 为什么这种简化会使我的函数变慢?

lisp - 如何将两个相似的函数合并为一个函数