LOOP is beautiful

From: Jim Bushnell (jcbushnell2_at_comcast.net)
Date: 04/23/04


Date: Thu, 22 Apr 2004 17:34:14 -0600

The following is an implementation of zero-window search (alpha-beta pruning
with a minimal window) for searching trees with integer leaves, and a driver
function for finding the minimax value. I admit to a number of false starts
before I got the logic right, but I attribute this to my obtuseness.

I think this qualifies as beautiful.

(defun zw (tree max alpha)
  (if (numberp tree)
      (eq max (>= tree alpha))
      (loop for child in tree
            thereis (not (zw child (not max) alpha)))))

(defun mtdf-zw (tree max guess)
  (if (eq max (zw tree max guess))
      (loop for v from (1+ guess)
            while (eq max (zw tree max v))
            finally return (1- v))
    (loop for v downfrom (1- guess)
          until (eq max (zw tree max v))
          finally return v)))

Jim Bushnell