Re: -2 is not a real number?!?
- From: Ken Tilton <kentilton@xxxxxxxxx>
- Date: Wed, 28 Feb 2007 15:19:28 -0500
mmcconnell17704@xxxxxxxxx wrote:
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))
Can't we just add BIF to the standard?
(bif (et (evaluate turns))
(list et)
....
(defmacro bif ((bindvar boundform) yup &optional nope)
`(let ((,bindvar ,boundform))
(if ,bindvar
,yup
,nope)))
Or PGs AIF:
(aif (evaluate turns)
(list it)....
kt
(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).
--
Well, I've wrestled with reality for 35 years, Doctor, and
I'm happy to state I finally won out over it.
-- Elwood P. Dowd
In this world, you must be oh so smart or oh so pleasant.
-- Elwood's Mom
.
- References:
- -2 is not a real number?!?
- From: cdombroski
- Re: -2 is not a real number?!?
- From: mmcconnell17704
- -2 is not a real number?!?
- Prev by Date: Useless SBCL on Win32: memory problem
- Next by Date: Re: A style question
- Previous by thread: Re: -2 is not a real number?!?
- Next by thread: sexp-aware theory of patches and "meta common lisp"
- Index(es):
Relevant Pages
|