Arrays 我怎样才能避免最后打印的零?
我已经编写了这个函数来打印电路板的状态,但是最后,由于没有返回,这个函数会打印一个nil 功能:Arrays 我怎样才能避免最后打印的零?,arrays,format,lisp,null,read-eval-print-loop,Arrays,Format,Lisp,Null,Read Eval Print Loop,我已经编写了这个函数来打印电路板的状态,但是最后,由于没有返回,这个函数会打印一个nil 功能: (defun show-board (board) (dotimes (number 8) (dotimes (number2 8) (let ((pos (aref board number number2))) (cond ((equa
(defun show-board (board)
(dotimes (number 8)
(dotimes (number2 8)
(let ((pos (aref board number number2)))
(cond
((equal pos 0) (format t "~a " "B"))
((equal pos 1) (format t "~a " "P"))
(t (format t "~a " "L")))))
(format t "~%")))
单板是一个8x8阵列
命令行上的函数调用输出:
B P B P B P B P
P B P B P B P B
B P B P B P B P
P B P B P B P B
B P B P B P B P
P B P B P B P B
B P B P B P B P
P B P B P B P B
NIL
我怎样才能去掉NIL???添加(值)
作为dotimes的返回表单将可以:
(dotimes (number 8 (values))
.....)
毕竟,ShowBoard
确实不会返回任何值,对吗?您可以在代码中去掉多种格式:
通常在函数式语言中,我会返回一个值。返回董事会本身是有道理的。因为这样的函数通常是从游戏逻辑中调用的,所以返回值可能很有用,并且它对输出并不重要
(defun show-board (board)
(dotimes (i 8)
(dotimes (j 8)
(write-string (case (aref board i j)
(0 "B ")
(1 "P ")
(otherwise "L "))))
(terpri))
board)
这很挑剔,但值得指出的是,show board
没有打印NIL
。如果您在REPL中执行CL-USER>(show board…
,您将看到零,但如果执行CL-USER>(progn(show board)2)
,则不会看到(但您将看到2
)。是REPL打印最后一个计算表单的值。这意味着,从调用show board的任何应用程序代码中,您都不会看到NIL
,因此,尽管这对REPL来说是一个不错的选择,但您通常不需要担心这一点。即使使用(值)
,您仍然会得到,例如,(列表(展示板))=>(NIL)
。