;; File: midterm.review
;; Date: 10/21/93
;; Description: exercises and sample questions for the midterm

;; In the following, asterisks (*) are used to indicate questions 
;; of comparable difficulty to those appearing on the actual exam. 
;; In cases, where an asterisk marks a question with many subparts
;; (e.g., a) .. b) .. c) .., etc.) there will never be more than 
;; four subparts and commonly only two or three subparts.
;; You can construct a sample exam from the following questions:
;;      Lisp: 1(b,e,f), 2(a,b), 4, 6(a),  
;;      Logic: 3(a,c,e), 4(c,d), 5(a,b), 8(a,b,c)
;;      Search: 4, 5
;;      Advanced representation: 1(a), 2(a)

;; Lisp

1. Evaluate the following expressions.  Note that PROGN takes any
number of expressions, evaluates them in order, and returns the 
value of the last one.

a) (cons 'a (append (list 1 2) (list 'c 'd)))                   (*)

b) (second (first (rest '(1 (2 3) 4))))                         (*)

c) (eq '(a b (c d)) '(a b (c d)))                               (*)

d) (member '(y) '(x y z))                                       (*)

e) (member (+ 2 2) '(1 2 3 4))                                  (*)

f) (second (mapcar #'+ '(3 6 1) '(3 1 9)))                      (*)

g) (member 7 (mapcar #'+ '(3 6 1) '(3 1 9)))                    (*)

h) (first (rest (rest (mapcar #'(lambda (x y) (if (> x y) x y))
                              '(9 2 8 7)
                              '(3 8 1 0)))))

i) (progn (setq x (list 3 (list 1 2)))                          (*)
          (setf (first (rest (first (rest x)))) 4)
          x)

j) (progn (setq x (cons 5 
                       (cons (cons 3 
                                   (cons 4 nil)) 
                             nil))) 
          x)

k) (progn (setq x (nconc '(5) 
                         (cons 3 
                               (cons 4 
                                     (cons 7 nil))) 
                         nil)) 
          x)

2. Consider the following Lisp function.

(defun cs141 (list)
  (cs141tas list 0))

(defun cs141tas (this that)
  (cond ((null this) that)
        ((numberp this) (+ this that))
        ((symbolp this) (* 2 that))
        (t (cs141tas (rest this) 
                     (cs141tas (first this) that)))))

a) What is the value of (cs141 '(3 f 5))?                       (*)

b) What is the value of (cs141 '(()))                           (*)

c) What is the value of (cs141 '(0 2f (c f) 6 s 0))             

d) What is the value of (cs141 '((9 0 h) 2f (c f) 6 s 0))

;; > (cs141 '(3 f 5))
;; 11
;; > (cs141 '((5 1) a 2))
;; 14
;; > (cs141 '(0 2f (c f) 6 s 0))
;; 12
;; > (cs141 '((9 0 h) 2f (c f) 6 s 0))
;; 300

3. Consider the following Lisp function. This one is a way too long
for a question on a 1.5 hour exam but it is a good exercise.

(defun fubar (f)
  (let ((gee (foo f nil)))
    (append (first gee) (second gee))))

(defun foo (bar tee)
  (cond ((null bar) tee)
        ((numberp (first bar))
         (foo (rest bar)
              (list (nconc (first tee) (list (first bar)))
                    (second tee)))) 
        ((symbolp (first bar))
         (foo (rest bar)
              (list (first tee)
                    (nconc (second tee) (list (first bar))))))
        (t (foo (rest bar) (foo (first bar) tee)))))

a) What is the value of (fubar '((1 m) 2 (fuzzy)))?

b) What is the value of (fubar '((e r 5 1  a) 5 (6) d 2 (g 15 7) t 8 9))?

;; > (fubar '((1 m) 2 (fuzzy)))
;; (1 2 M FUZZY)
;; > (fubar '((e r 5 1  a) 5 (6) d 2 (g 15 7) t 8 9))
;; (5 1 5 6 2 15 7 8 9 E R A D G T)

4.(*) Write a recursive lisp function that takes a nested list of symbols
and numbers and returns a list in this form:
((list-containing-all-numbers) (list-containing-all-non-numbers))

5. Write a recursive lisp function, insert, that inserts a number x
into a list of numbers arranged in ascending order, such that x is
inserted in a place after a number less than or equal to it, and
before a number larger than it.
With the function insert, write a recursive lisp function,
sorting, that sorts a list of numbers into ascending order.

(defun insert (x alist)
  (cond ((null alist) (list x))
        ((> x (first alist)) (cons (first alist) (insert x (rest alist))))
        (t (cons x alist))))
(defun sorting (alist)
  (cond ((null (rest alist)) alist)
        (t (insert (first alist) (sorting (rest alist))))))

6. Consider the following abstract data type

(defun TREE (name left-child right-child)
  (list name nil left-child right-child))
(defun TREE-NAME (tree) (first tree))
(defun TREE-MARK (tree) (second tree))
(defun TREE-LEFT-CHILD (tree) (third tree))
(defun TREE-RIGHT-CHILD (tree) (fourth tree))
(defun SET-TREE-MARK (tree val) (setf (second tree) val))

a) (*) Write a recursive lisp function that takes a tree and sets the marks of
each tree node with NIL mark to be T.

b) (*) Write a recursive lisp function that takes a tree and returns
all of the non-NIL marks in the tree.

;; Logic

1. (*) Represent the following in propositional logic.  

a) Either Jason is daft or Brenda is a genius.

b) Jennifer has a cold and either Ralph or Max has the flu.

c) If Fred has the answer, Peter and Jennifer also have the answer.

d) Melvin is crazy if he has a ticket and misses the concert.

e) The pastries at the Gate and Josiah's must be terrible 
if they are baked at the Ratty.

f) If Bill can't find a job, he will be a househusband and Hillary will be
the breadwinner.

2. (*) Convert a (relatively simple) formula involving an implication
to conjunctive normal form. Note that a rule of the form p -> (q ^ r)
can be converted to two rules (a conjunction) p -> q and p -> r.

a) (p ^ q) -> r

b) r -> (p v q)

c) r -> (p ^ q)

3. Label the following formulas as either satisfiable (but not valid),
valid or not satisfiable given the specified set of interpretations.

a) (and (not (or a (or b (not b)))) (or b a))                   (*)
given all possible interpretations (assignments to {a,b})

b) (and a (not a) b)                                            (*)
given all possible interpretations (assignments to {a,b})

c) (or a (or b (not b)))                                        (*)
given all possible interpretations (assignments to {a,b})

e) (or (not a) (not b))                                         (*)
interpretations = ((a false) (b false)), 
                  ((a true) (b false))

f) (and b (or (and a c) (and (not a) (not c))))
interpretations = ((a true) (b false) (c true)),
                  ((a true) (b true) (c false)), 
                  ((a false) (b false) (c true)), 
                  ((a true) (b false) (c true))

g) (and a (or (not b) a))                                       (*)
interpretations = ((a true) (b false)), 
                  ((a true) (b true)), 
                  ((a false) (b true))

4. (*) Represent the following in first-order predicate logic.  
Use the predicates Mushroom(x), Purple(x), and Poisonous(x).  

a) All purple mushrooms are poisonous.  

b) No purple mushroom is poisonous.

c) All mushrooms are either purple or poisonous.

d) All purple mushrooms except one are poisonous.

e) There are exactly two purple mushrooms.

Use the predicates Banana(x), Moon(x), InTheSky(x), and OnAnApplePie(x).  

f) All bananas on applepies are in the sky.

g) Only one banana in the sky is on an applepie.

h) There is only one banana in the sky that is not on an applepie.

i) All bananas in the sky are moons.

j) There is only one moon in the sky.

k) There is only one moon in the sky, which is on an applepie.

l) There is a banana in the sky and a moon on an applepie.

m) There are no bananas in the sky.

5. (*) Consider the following axioms and their corresponding 
Lisp representation as rules and facts.  

p                       (p)
q                       (q)
z                       (z)
p -> r                  (r if p)
(q ^ r) -> s            (s if q r)
(r ^ s) -> x            (x if r s)
(z ^ p) -> y            (y if z p)
(r ^ x ^ y) -> t        (t if r s y)

a) Using the rules of inference, modus ponens and conjunction, prove t or
state that no such proof is possible.

b) Will goal reduction succeed on the goal (t)? 

c) In general, if the axioms are restricted to rules (horn clauses)
and the rules of inference restricted to modus ponens and conjunction,
will goal reduction always succeed on a goal if there is a proof of
the corresponding conjunction?

6. (*) Given the facts and rules below, determine if goal reduction
would succeed with the goals (i) and (d b).

(x)                     
(e)                     
(d)                     
(t)                     
(s if e x)              
(i if s)                
(b if a d)              

7. (*) Convert the following wffs to an equivalent wff expressed as a
conjunction of formulas of the form forall x1,x2,...,xn, Phi where Phi
is a disjunction of atomic sentences or their negations containing no
quantifiers. Note that a rule of the form p -> (q ^ r) can be
converted to two rules (a conjunction) p -> q and p -> r.

a) forall x,y, (P(x,y) -> (Q(x,y) ^ R(x,y)))

b) forall z, (exists x,y, Above(x,z) ^ Above(z,y)) -> Sandwiched(z)

8. (*) For each of the following pairs of literals, determine if they
unify.  If so, give the most general substitution; if not, give a
brief explanation.  x, y, and z are variable symbols, a and b are
constants, f is a function symbol, and P is a predicate.

a) P(x,y)        P(z,a)

b) P(f(y),x)     P(z,f(b))

c) P(f(y),x,x)   P(z,z,f(a))

9. Consider the sentence ``Heads I win; tails you lose.''
Representing this sentence plus associated domain knowledge
in FOPC, we have the following axioms (Me and You are constants,
Win and Lose are predicates of one argument, and Heads and Tails are 
predicates of no arguments.

          i. Heads -> Win(Me)
         ii. Tails -> Lose(You)
        iii. ~Heads -> Tails
         iv. Lose(You) -> Win(Me)

a) Convert each of these four wffs to a disjunction of literals.

b) For each of the disjunctions in (a), specify if it is a Horn clause.

c) Is it possible to prove Win(Me) using resolution and the above 
   formulas written as disjunctions of literals. 

d) Can you prove Win(Me) using using goal reduction?

;; Search

1. Consider your favorite (small) tree-structured search space. 
How many steps are required by depth-first and breadth-first search to
find a goal node?

2. Consider your favorite (small) search space defined as a graph.
How many steps are required by depth-first and breadth-first search to
find a goal node?

3. Consider the following grid

                            o o o o o 
                            o o o G o 
                            o o o o o 
                            S o o o o 
                            o o o o o

Assume that you start searching at S, the goal is at G, and that the
next function for all searches generates the next nodes of the node 
H in the following order.

                                 1
                               4 H 2
                                 3

except for heuristic search in which children of the same priority
would be given in that order. (e.g., if the priority goes like Fig 1,
then the order that next returns will be like Fig 2:

                4                  3
              2 H 4              2 H 4
                2                  1

             Fig. 1             Fig. 2

What is the number of nodes (inclusive of the goal and the start) that
each of the following apply the goal predicate to before finding the
goal? Recall that on each iteration, all of the search routines apply
the goal predicate to the first element of the list of nodes unless
that list is empty.

a) depth-first search

b) breath-first search

c) best-first search with the manhattan distance between a node and the goal
as the heuristic. 

d) iterative deepening search with the initial depth as 0 and the depth
incremented by 2 each iteration.

4. (*) Consider the grid shown in Fig 3 with starting node S, goal node G,
and obstacles marked by X.

                            3 o o o G 
                            2 o o X o 
                            1 S o o X 
                            0 o o o o 
                              0 1 2 3

                              Fig. 3

Suppose that you search this grid using the heuristic function that is
the Manhattan distance mod 2.  The starting node is at the coordinates
(0 1) and the goal is at the coordinates (3 3).  List the coordinates
of nodes in the order in which they are evaluated by the goal testing
function. Your answer should be of the form (0 1) ... (3 3).

5. Make sure you understand the tradeoffs involved in using different
blind search methods for different sorts of search spaces. 
(*) For example, the exam might include a question that asks what the
best blind search method is for searching a finite, tree-structured
search space with small depth and large branching factor.

;; Advanced Representation

1. (*) Represent the following in the situation calculus.

a) If the dragon is awake and the dragon is in its hoard and
   the knight enters the hoard then the knight is dead.

b) The only change that results if the knight enters the dragon's hoard 
   when the dragon is awake and in its hoard is that the knight is dead.

2. (*) Consider the following situation calculus axioms,

holds (0, f1)
holds (0, f2)
forall s, holds(s, f1) -> holds(result(s, e3), f3)
forall s, holds(s, f2) -> holds(result(s, e5), f4)
forall s, (holds(s, f3) ^ holds(s, f4)) -> holds(result(s, e4), f5)

a) Add frame axioms so that holds(result(0,e3),f1).

b) Assuming frame axioms as needed, determine s such that holds(s, f5).
