How can I avoid the nil printed in the end?

百般思念 提交于 2019-12-02 02:15:37

问题


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!

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!

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??


回答1:


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

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

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




回答2:


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)


来源:https://stackoverflow.com/questions/17429729/how-can-i-avoid-the-nil-printed-in-the-end

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!