Answers to exam questions This document contains suggested answers to selected questions from the exam in TDDC65 from 2008-12-20. Question 1: Lisp prorgamming (8p) 1a (2p) Each exactly correct answers gives you 0.5 points. CL-USER(1): (setq fruits (list 'apple 'banana (cons 'orange '()))) (APPLE BANANA (ORANGE)) CL-USER(2): (cons (+ 1 2) '(* 3 4)) (3 * 3 4) CL-USER(3): (append '(clockwork) (third fruits)) (CLOCKWORK ORANGE) CL-USER(4): (list (second fruits) 'phone) (BANANA PHONE) 1b (4p) An otherwise correct function that only works on the top level, i.e. no double recursion, gives you 2 points. (defun change (sequence old new) (cond ((endp sequence) '()) ((listp (first sequence)) (cons (change (first sequence) old new) (change (rest sequence) old new))) ((eq old (first sequence)) (cons new (change (rest sequence) old new))) (t (cons (first sequence) (change (rest sequence) old new))))) CL-USER(5): (change '(do re me do re mi) 'me 'mi) (DO RE MI DO RE MI) CL-USER(5): (change '((14 17 19) -1 (4 -1 (3 4))) -1 0) ((14 17 19) 0 (4 0 (3 4))) 1c (2p) For this question you can assume that the function change2 exists, so you don’t have to define it. You will get 1 point for each of the lambda expressions in the second example below (or you can write your own functions). (defun change2 (sequence old-fn new-fn) (cond ((endp sequence) '()) ((listp (first sequence)) (cons (change2 (first sequence) old-fn new-fn) (change2 (rest sequence) old-fn new-fn))) ((funcall old-fn (first sequence)) (cons (funcall new-fn (first sequence)) (change2 (rest sequence) old-fn new-fn))) (t (cons (first sequence) (change2 (rest sequence) old-fn new-fn))))) CL-USER(20): (change2 '(one 2 three 4) #'numberp #'(lambda (x) (* x 2))) (ONE 4 THREE 8) CL-USER(21): (change2 '(1 2 3 4 5 6 7 8 9) #'(lambda (x) (= 0 (mod x 3))) #'(lambda (x) 'hop)) (1 2 HOP 4 5 HOP 7 8 HOP) Question 2: Logic (12p) 2a (2p) There are at least two suitable ways. The first one is to try to rewrite the first formula to the other using the rules in the appendix. This can e.g. be done like this: P ⇒ (Q ∧ R) ¬P ∨ (Q ∧ R) (Q ∧ R) ∨ ¬P (Q ∨ ¬P) ∧ (R ∨ ¬P) ¬ ¬ (Q ∨ ¬P) ∧ (R ∨ ¬P) ¬(¬Q ∧ P) ∧ (R ∨ ¬P) implication elimination commutativity distributivity double negation De Morgan The other way is to construct a truth table like this: P 0 0 0 0 1 1 1 1 Q 0 0 1 1 0 0 1 1 R 0 1 0 1 0 1 0 1 Q∨R 0 0 0 1 0 0 0 1 P ⇒ (Q ∧ R) 1 1 1 1 0 0 0 1 ¬Q ∧ P 0 0 0 0 1 1 0 0 R ∨ ¬P 1 1 1 1 0 1 0 1 ¬(¬Q ∧ P) ∧ (R ∨ ¬P) 1 1 1 1 0 0 0 1 The final columns for the left hand formula and the right hand formula are identical, so they are equivalent. 2b (2p) The two formulas are satisfiable, since there are at least one row in the truth table where they are true. They are however not valid, since not all rows in the truth table make them true. Since the formulas are equivalent, the reasoning goes for both of them. 2c (2p) There might be several correct ways of answering this question, but this is a suggestion. We will use the following propositional symbols: TV: There is “something” on TV, probably meaning something that John want’s to watch. P: John goes to a party. S: John goes to sleep. L: John feels lonely. Using these we can interpret the sentences like this: a) b) c) d) ¬TV ⇒ P ∨ S ¬P ⇒ L L ∧ ¬TV ⇒ ¬S ¬TV ⇒ P 2d (3p) In order to prove that d) follows from the conjunction of a), b) and c) we negate d), convert all formulas to CNF and use the resolution rule to find a contradiction. Formula Clauses in CNF ¬TV ⇒ P ∨ S TV ∨ P ∨ S ¬P ⇒ L P∨L L ∧ ¬TV ⇒ ¬S ¬L ∨ TV ∨ ¬S ¬(¬TV ⇒ P) ¬TV ¬P Now we can make a proof using resolution: 1. TV ∨ P ∨ S 2. P ∨ L 3. ¬L ∨ TV ∨ ¬S 4. ¬TV 5. ¬P 6. P ∨ S 7. S 8. ¬L ∨ TV 9. ¬L 10. P 11. Contradiction 1, 4 5, 6 3, 7 4, 8 2, 9 5, 10 There might be other ways of deducing a contradiction. This can also be proven using graphical notation. 2e (3p) See the start of chapter 7 in the course book. The Wumpus world might be a good example from the book to illustrate the merits of predicate logic. Question 3: Search (11p) 3a (5p) Note that in each stage it is the node with the lowest g + h that is expanded. At one point there is a choice between two nodes with the same value. Since the algorithm does not know anything besides this value, a node is chosen randomly. Also note that the expansion will continue after we have found a route to the goal, since there are still some nodes with lower g + h. (A complete illustration can be found on the next page.) 3b (3p) See chapters 4 and 5 in the course book. 3c (3p) See chapter 5 in the course book. Question 4: Planning (8p) 4a (4p) Here is a suggestion: (define (domain hanoi) (:requirements :strips) (:predicates (clear ?x) (on ?x ?y) (smaller ?x ?y)) (:action move :parameters (?disc ?from ?to) :precondition (and (smaller ?to ?disc) (on ?disc ?from) (clear ?disc) (clear ?to)) :effect (and (clear ?from) (on ?disc ?to) (not (on ?disc ?from)) (not (clear ?to))))) 4b (2p) Here is a suggestion: (define (problem hanoi-3) (:domain hanoi) (:objects peg1 peg2 peg3 d1 d2 d3) (:init (smaller peg1 d1) (smaller peg1 d2) (smaller peg1 d3) (smaller peg2 d1) (smaller peg2 d2) (smaller peg2 d3) (smaller peg3 d1) (smaller peg3 d2) (smaller peg3 d3) (smaller d1 d1) (smaller d2 d1) (smaller d3 d1) (smaller d2 d2) (smaller d3 d2) (clear peg2) (clear peg3) (clear d1) (on d3 peg1) (on d2 d3) (on d1 d2)) (:goal (and (on d3 peg3) (on d2 d3) (on d1 d2)))) 4c (2p) See e.g. the lecture notes on planning. Question 5: Miscellanous (6p) 5a (2p) See chapter 2 in the course book. 5c (2p) See chapter 1 in the course book. (Sorry about the confusing numbering.) 5b (2p) See chapter 10 in the course book.