Re: -2 is not a real number?!?



Other posters have explained the problem with v. But this is good
code for a (relative?) beginner. I thought I'd suggest a few changes
to the style.

(defun min-value (turns turn alpha beta)
(let ((v most-positive-fixnum)
(best-move -1))
(if (evaluate turns)
(list (evaluate turns))
(dolist (x (find-moves) (list v best-move))
(setf (aref *board* (floor x 3) (mod x 3)) (if turn "X" "O"))
(setf v (min v (car (max-value (+ turns 1) (not turn) alpha
beta))))
(setf (aref *board* (floor x 3) (mod x 3)) " ")
(when (<= v alpha) (return-from min-value (list v x)))
(setf beta (min v beta))))))

For a better "functional" style, avoid return-from. I got rid of the
first return-from using the full meaning of "if": the else clause
returns the value of the dolist. I got rid of the third return-from
by putting what you're returning into the third position of the
initial form of the dolist.

"when" is good for ifs that only have a then-clause. See also
"unless".

Most important, the two calls to (evaluate turns) are dangerous. The
first call may take a long time, and it all has to be repeated in
order to return (list [same thing]). You might want to say

(let ((ev-turns (evaluate turns)))
(if ev-turns
(list ev-turns)
[the rest, starting with (let ((v ...)))]))

For another approach, see "anaphoric if" in Paul Graham's book _On
Lisp_ (available in pdf by Googling).

.



Relevant Pages

  • Re: -2 is not a real number?!?
    ... (defun min-value (turns turn alpha beta) ... I got rid of the third return-from ... by putting what you're returning into the third position of the ... (let ((ev-turns (evaluate turns))) ...
    (comp.lang.lisp)
  • Re: -2 is not a real number?!?
    ... (defun min-value (turns turn alpha beta) ... by putting what you're returning into the third position of the ... I'm more used to c style programming, although they say that multiple exit ... (let ((ev-turns (evaluate turns))) ...
    (comp.lang.lisp)