In: Computer Science
LISP FUNCTIONS
Imagine you are writing the AI for a simple game that will require an adversarial search.
The state of the game can be represented using a list of 3 elements, such as (B B B). The list representing the game state can contain any combination of 3 values: A, B, or C. It will always have exactly 3 items. (Let's call it the "state list").
With that in mind, write the following LISP functions:
State | Action |
(B * *) | A1 |
(* B *) | A2 |
( * * B) | A3 |
State | Action | Returned |
(B * *) | A1 | (A * *) |
(* B *) | A2 | (* A *) |
(* * B) | A3 | (* * A) |
Demonstrate your functions work by calling each one in turn, passing each of them appropriate arguments, and then printing to the screen the values they returned.
The code, including the test cases is given below, followed by the snapshot.
(defun TERMINAL-TEST (state)
(and
(and (not (eq (car state) 'B)) (not (eq (car (cdr state)) 'B)
))
(not (eq (car (cdr (cdr state))) 'B))
)
)
(defun UTILITY (state)
(cond
((and
(and (eq (car state) 'A) (eq (car (cdr state)) 'A) )
(eq (car (cdr (cdr state))) 'A)
) 1 )
((and
(and (eq (car state) 'C) (eq (car (cdr state)) 'C) )
(eq (car (cdr (cdr state))) 'C)
) -1 )
(T 0)
)
)
(defun ACTIONS (state)
(append
(if (eq (car state) 'B)
'(A1) '())
(if (eq (car (cdr
state)) 'B) '(A2) '())
(if (eq (car (cdr (cdr
state))) 'B) '(A3) '())
)
)
(defun RESULT (state action)
(cond
((and (eq action 'A1)
(eq (car state) 'B)) (cons 'A (cdr state)))
((and (eq action 'A2)
(eq (car (cdr state)) 'B))
(cons (car state) (cons 'A (cdr (cdr state)))))
((and (eq action 'A3)
(eq (car (cdr (cdr state))) 'B))
(cons (car state) (cons (car (cdr state)) '(A))))
(T state)
)
)
(write-line "")(format t "(TERMINAL-TEST '(B B B))=~a"
(TERMINAL-TEST '(B B B)))
(write-line "")(format t "(TERMINAL-TEST '(A B C))=~a"
(TERMINAL-TEST '(A B C)))
(write-line "")(format t "(TERMINAL-TEST '(A C A))=~a"
(TERMINAL-TEST '(A C A)))
(write-line "")(format t "(UTILITY '(A A A))=~a" (UTILITY '(A A
A)))
(write-line "")(format t "(UTILITY '(C C C))=~a" (UTILITY '(C C
C)))
(write-line "")(format t "(UTILITY '(A B C))=~a" (UTILITY '(A B
C)))
(write-line "")(format t "(ACTIONS '(B C B))=~a" (ACTIONS '(B C
B)))
(write-line "")(format t "(ACTIONS '(B B B))=~a" (ACTIONS '(B B
B)))
(write-line "")(format t "(ACTIONS '(A C B))=~a" (ACTIONS '(A C
B)))
(write-line "")(format t "(ACTIONS '(A C C))=~a" (ACTIONS '(A C
C)))
(write-line "")(format t "(RESULT '(C C C) 'A1)=~a" (RESULT '(C
C C) 'A1))
(write-line "")(format t "(RESULT '(B C C) 'A1)=~a" (RESULT '(B C
C) 'A1))
(write-line "")(format t "(RESULT '(A B C) 'A2)=~a" (RESULT '(A B
C) 'A2))
(write-line "")(format t "(RESULT '(B C B) 'A3)=~a" (RESULT '(B C
B) 'A3))
(write-line "")(format t "(RESULT '(B C C) 'A2)=~a" (RESULT '(B C
C) 'A2))