;;; Funciones para tratar los árboles minimax, tal como los necesita
;;; el algoritmo de poda alpha-beta de los materiales.
;;; Supondremos que un árbol
;;; o bien es una hoja: número
;;; o bien no lo es: ('max/min (hijo_1 hijo_2 . . . hijo_n))
;;; y hijo_j es un árbol
;;;
;;; He intentado maximizar la simplicidad, y NADA esté protegido contra
;;; errores
;;;
(defun hoja (arbol)
(numberp arbol))
(defun evalua (arbol)
(if (hoja arbol) arbol)) ; si no es hoja no se considera; ya se protegerá en el código
(defun nodo-min (arbol)
(eql 'min (car arbol)))
(defun nodo-max (arbol)
(eql 'max (car arbol)))
(defun hijos (arbol)
(if (not (hoja arbol)) (cadr arbol)))
;;; Poda alpha-beta tal como está implementada en el
;;; Módulo 2, página 85, implementado en Common Lisp
;;;
;;; Fijémonos que un árbol es un nodo con hijos, o una hoja
;;; El código necesitará las siguientes funciones:
;;;
;;; (hoja nodo) -------> booleano
;;; (evalua nodo) ------> el valor de los nodos, típicamente un coste, por tanto un número
;;; (tan solo tiene sentido si (hoja nodo) es true)
;;; (nodo-min nodo) ----> booleano
;;; (nodo-max nodo) ----> booleano
;;; (hijos nodo) -------> lista de nodos
;;;
(defun minimax (arbol)
(cond
((hoja arbol) (evalua arbol))
((nodo-min arbol) (apply #'min (mapcar #'minimax (hijos arbol))))
((nodo-max arbol) (apply #'max (mapcar #'minimax (hijos arbol))))))
(defun minimax-alpha-beta (nodo alpha beta)
(cond
((hoja nodo)
(let ((val (evalua nodo)))
(format t "~A " val)
val))
((nodo-min nodo)
(let ((beta-tmp beta))
(do ((ch (hijos nodo) (cdr ch)))
((or (null ch) (<= beta-tmp alpha)) beta-tmp)
(let ((r (minimax-alpha-beta (car ch) alpha beta-tmp)))
(if (< r beta-tmp) (setf beta-tmp r))))))
((nodo-max nodo)
(let ((alpha-tmp alpha))
(do ((ch (hijos nodo) (cdr ch)))
((or (null ch) (<= beta alpha-tmp)) alpha-tmp)
(let ((r (minimax-alpha-beta (car ch) alpha-tmp beta)))
(if (< alpha-tmp r) (setf alpha-tmp r))))))))
;;; La función de la pregunta 3
;;;
(defun mystery (p x1 x2)
(if (hoja p)
(let ((val (evalua p)))
(format t "~A " val)
val)
(let ((w (hijos p))
(m x1))
(dolist (q w m) (let ((k (- (mystery q (- x2) (- m)))))
(if (> k m) (setf m k))
(if (>= m x2) (return m)))))))
;;; Tests
;;; Unos cuantos árboles para hacer pruebas:
(defparameter *tree-001*
'(max ((min ((max ((min (15 14))
(min (13 12))))
(max ((min (11 10))
(min (9 8))))))
(min ((max ((min (7 6))
(min (5 4))))
(max ((min (3 2))
(min (1 0)))))))))
(defparameter *tree-002*
'(max ((min ((max ((min (0 -1))
(min (-2 -3))))
(max ((min (-4 -5))
(min (-6 -7))))))
(min ((max ((min (-8 -9))
(min (-10 -11))))
(max ((min (-12 -13))
(min (-14 -15)))))))))
(defparameter *tree-003*
'(max ((min ((max ((min (0 1))
(min (2 3))))
(max ((min (4 5))
(min (6 7))))))
(min ((max ((min (8 9))
(min (10 11))))
(max ((min (12 13))
(min (14 15)))))))))
(defparameter *tree-004*
'(max ((min (4 5))
(min (6
(max (3 4))
(max (7 9))))
(min (3 8)))))
(defparameter *tree-005*
'(max ((min (21 -85 7 -8))
(min (-11 -65 7 -42))
(min (-14 -15 73 99))
(min (-37 -71 -97 -90)))))
(defparameter *tree-006*
'(max ((min ((max ((min (-76 -28))
(min (3 -60))))
(max ((min (-66 -36))
(min (-88 56))))))
(min ((max ((min (13 -7))
(min (50 19))))
(max ((min (16 -15))
(min (-67 46)))))))))
(defparameter *tree-007*
'(max ((min ((max (-9 18 -5))
(max (16 7 -5))
(max (-3 16 11))))
(min ((max (-17 9 -8))
(max (-13 -2 2))
(max (5 16 1))))
(min ((max (13 18 4))
(max (-12 12 10))
(max (-10 -5 6)))))))
(defparameter *tree-008*
'(max ((min ((max (2 2 0))
(max (-5 4 -6))
(max (-11 5 10))))
(min ((max (-18 -1 1))
(max (-10 -6 -4))
(max (-9 16 19))))
(min ((max (-8 -9 8))
(max (-7 12 -9))
(max (16 -7 19)))))))
(defparameter *tree-009*
'(max ((min ((max (8 7 3))
(max (9 1 6))
(max (2 4 1))))
(min ((max (1 3 5))
(max (3 9 2))
(max (6 5 2))))
(min ((max (1 2 3))
(max (9 7 2))
(max (16 6 4)))))))
(defparameter *tree-010*
'(max ((min (4 5))
(min (6
(max (13 14))
(max (17 19))))
(min (3 8)))))
(defparameter *tree-011*
'(min ((max ((min (8 7 3))
(min (9 1 6))
(min (2 4 1))))
(max ((min (1 3 5))
(min (3 9 2))
(min (6 5 2))))
(max ((min (1 2 3))
(min (9 7 2))
(min (16 6 4)))))))
About Online Lisp Compiler
Try our Online Lisp Compiler (Version GNU CLISP v2.49+) to Edit, Run, and Share your Lisp Code directly from your browser. This online development environment provides you the latest version GNU CLISP v2.49+.
How to use Online Lisp Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online Lisp Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online Lisp Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online Lisp Compiler: Save and Share Lisp Code (Project)
Save Lisp Project Online
You can save your Lisp Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share Lisp Project Online
You can use this feature to share your Lisp Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online Lisp Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online Lisp Compiler
There are several benefits of using the Online Lisp Compiler to run your Lisp code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.