我怎样才能避免最后打印的nil呢?

时间:2023-01-18 01:09:08

I have coded this function that prints-out the state of the board, but in the end, due to the fact that there isnt no return the function prints an nil!

我已经编写了这个函数,它输出了棋盘的状态,但是最后,由于没有返回,函数输出了一个nil!

Function:

功能:

(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 "~%")))

A board is an 8x8 array!

板是一个8x8的阵列!

function call output on command lines:

命令行上的函数调用输出:

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

How can i get rid of the NIL??

如何消去NIL?

2 个解决方案

#1


1  

adding (values) as return form for dotimes will do it:

添加(值)作为dotimes的返回形式即可:

(dotimes (number 8 (values))
   .....)

after all, show-board indeed doesn't return any values, right?

毕竟,showboard实际上不返回任何值,对吧?

#2


4  

You can get rid of multiple formats in the code:

你可以去掉代码中的多种格式:

Usually in a functional language I would return a value. It makes sense to return the board itself. Since such a function is usually called from game logic, a return value may be useful and it does not matter for output then.

通常在函数语言中,我会返回一个值。返回董事会本身是有意义的。由于这样的函数通常是从博弈逻辑中调用的,因此返回值可能是有用的,因此对于输出来说并不重要。

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

#1


1  

adding (values) as return form for dotimes will do it:

添加(值)作为dotimes的返回形式即可:

(dotimes (number 8 (values))
   .....)

after all, show-board indeed doesn't return any values, right?

毕竟,showboard实际上不返回任何值,对吧?

#2


4  

You can get rid of multiple formats in the code:

你可以去掉代码中的多种格式:

Usually in a functional language I would return a value. It makes sense to return the board itself. Since such a function is usually called from game logic, a return value may be useful and it does not matter for output then.

通常在函数语言中,我会返回一个值。返回董事会本身是有意义的。由于这样的函数通常是从博弈逻辑中调用的,因此返回值可能是有用的,因此对于输出来说并不重要。

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