I have the following Common Lisp function:
(defun get-positions (marker)
(let ((result nil))
(dotimes (i (length board))
(if (eq (nth i board) marker)
(push i result)))
(nreverse result)))
Here’s what board is and here’s the output of the function:
CL-USER> board
(X X O NIL NIL NIL NIL NIL NIL)
CL-USER> (get-positions 'x)
(0 1)
It seems like the function I wrote might be a little verbose. Is there a more concise way to write it?
I’d write it like this, passing in the list to be searched rather than using the global variable
board. I’d also add a docstring, and since usingeqfor comparison seems rather arbitrary, I’d make it a keyword argument so you could supply=for numeric comparison orequalfor strings: