1 / 13

(defun play (first second) ;4.18 p. 125 (cond ((equal first 'rock)

Return value. (defun play (first second) ;4.18 p. 125 (cond ((equal first 'rock) (cond ((equal second 'rock) 'tie) ((equal second 'paper) 'second-wins) (t 'first-wins)) ) ((equal first 'paper)

mareo
Download Presentation

(defun play (first second) ;4.18 p. 125 (cond ((equal first 'rock)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Return value (defun play (first second) ;4.18 p. 125 (cond ((equal first 'rock) (cond ((equal second 'rock) 'tie) ((equal second 'paper) 'second-wins) (t 'first-wins))) ((equal first 'paper) (cond ((equal second 'rock) 'first-wins) ((equal second 'paper) 'tie) (t 'second-wins))) ((equal first 'scissors) (cond ((equal second 'rock) 'second-wins) ((equal second 'paper) 'first-wins) (t 'tie))))) For each possible value of first player, decide based on second player Note: This version isn’t robust with respect to inappropriate input (i.e., something that isn’t rock, paper, scissors)

  2. (defun play (first second) (cond ((equal first 'rock) (cond ((equal second 'rock) 'tie) ((equal second 'paper) 'second-wins) (t 'first-wins))) ((equal first 'paper) (cond ((equal second 'rock) 'first-wins) ((equal second 'paper) 'tie) (t 'second-wins))) ((equal first 'scissors) (cond ((equal second 'rock) 'second-wins) ((equal second 'paper) 'first-wins) (t 'tie))))) IF THIS THEN THIS ELSEIF THIS THEN THIS ELSEIF THIS THEN THIS COND – a decision list

  3. play (paper, scissors) (defun play (first second) (cond ((equal first 'rock) (cond ((equal second 'rock) 'tie) ((equal second 'paper) 'second-wins) (t 'first-wins))) ((equal first 'paper) (cond ((equal second 'rock) 'first-wins) ((equal second 'paper) 'tie) (t 'second-wins))) ((equal first 'scissors) (cond ((equal second 'rock) 'second-wins) ((equal second 'paper) 'first-wins) (t 'tie))))) COND – a decision list

  4. ; nice goal one: simplify -- nice to do for YOUR understanding of solution (defun play (first second) (cond ((equal first second) 'tie) ; factor out easily checked common case ((equal first 'rock) (cond ((equal second 'paper) 'second-wins) (t 'first-wins))) ; second must be scissors ((equal first 'paper) (cond ((equal second 'rock) 'first-wins) (t 'second-wins))) (t ; first must be scissors (cond ((equal second 'rock) 'second-wins) (t 'first-wins))))) Again – it doesn’t guard against inappropriate input

  5. ; and simplify again (defun play (first second) (cond ((equal first second) 'tie) ((equal first 'rock) (cond ((equal second 'paper) 'second-wins) (t 'first-wins))) ((equal first 'paper) (cond ((equal second 'rock) 'first-wins) (t 'second-wins))) ((equal second 'rock) 'second-wins) (t 'first-wins))) BTW: Simplest code (given equal functionality) is often not best in terms of comprehensibility and (even) efficiency

  6. ; nice goal two: make robust (and comprehensible) (defun play (first second) (cond ((equal first 'rock) (cond ((equal second 'rock) 'tie) ((equal second 'paper) 'second-wins) ((equal second 'scissors) 'first-wins) (t (list 'second 'unknown)))) ((equal first 'paper) (cond ((equal second 'rock) 'first-wins) ((equal second 'paper) 'tie) ((equal second 'scissors) 'second-wins) (t (list 'second 'unknown)))) ((equal first 'scissors) (cond ((equal second 'rock) 'second-wins) ((equal second 'paper) 'first-wins) ((equal second 'scissors) 'tie) (t (list 'second 'unknown)))) (t (list 'first 'unknown))))

  7. ; Other formulations (defun play (first second) (if (equal first second) ; IF 'tie ; THEN (if (equal first 'rock) ; ELSE (if (equal second 'paper) 'second-wins 'first-wins) (if (equal first 'paper) (if (equal second 'rock) 'first-wins 'second-wins) (if (equal second 'rock) 'second-wins 'first-wins))))) When I wrote this, it was harder for me to keep track of the explicit nesting -- much harder than keeping track of the logic in the COND (decision list) format

  8. (defun play (first second) (cond ((and (equal first 'rock) (equal second 'rock)) 'tie) ((and (equal first 'rock) (equal second 'paper)) 'second-wins) ((and (equal first 'rock) (equal second 'scissors)) 'first-wins) ((and (equal first 'paper) (equal second 'rock)) 'first-wins) ((and (equal first 'paper) (equal second 'paper)) 'tie) ((and (equal first 'paper) (equal second 'scissors)) 'second-wins) ((and (equal first 'scissors) (equal second 'rock)) 'second-wins) ((and (equal first 'scissors) (equal second 'paper)) 'first-wins) ((and (equal first 'scissors) (equal second 'scissors)) 'tie) (t 'unknown-confuguration))) Doesn’t factor out common conditions at all – a bit unwieldy, but this minimally structured decision list might also be something that would be more amenable to automated construction/revision

  9. ;5.6 pp. 151-152 (defun throw-die () (+ 1 (random 6))) ;a (defun throw-dice () (list (throw-die) (throw-die))) ;b ; (defun throw-dice () (cons (throw-die) (list (throw-die)))) (defun snake-eyes-p (throw) (equal throw '(1 1))) ;c (defun boxcars-p (throw) (equal throw '(6 6))) (defun instant-win-p (throw) ;d (or (equal (eval (cons '+ throw)) 3) ; code should be stylistically consistent (equal (apply #'+ throw) 11))) ; to aid comprehensibility. I’m trying to (defun instant-loss-p (throw) ; illustrate different possibilities (let ((x (apply #'+ throw))) (or (< x 4) (= x 12))))

  10. (defun say-throw (throw) ;e (cond ((snake-eyes-p throw) 'snake-eyes) ((boxcars-p throw) 'boxcars) (t (apply #'+ throw)))) ; ;(defun say-throw (throw) ; (let ((x (apply #'+ throw))) ; (cond ((equal x 2) 'snake-eyes) ; ((equal x 12) 'boxcars) ; (t x)))) Using previously defined functions is often good, as it eliminates redundancy (in effect defining the same functionality repeatedly), which in turn can guard against bugs being introduced when a function is changed (you only have to change one, not many). However, the it can also sometimes result in less efficient code – example later perhaps

  11. (defun craps () ;f (let* ((throw (throw-dice)) (point (say-throw throw)) (suffix (cond ((instant-win-p throw) (list point '-- 'you 'win)) ((instant-loss-p throw) (list point '-- 'you 'lose)) (t (list 'your 'point 'is point))))) (cons 'throw (cons (car throw) (cons 'and (cons (cadr throw) (cons '-- suffix)))))))

  12. ;(defun craps () ; (let* ((throw (throw-dice)) ; (point (say-throw throw)) ; (suffix (cond ((instant-win-p throw) ; (list point '-- 'you 'win)) ; ((instant-loss-p throw) ; (list point '-- 'you 'lose)) ; (t (list 'your 'point 'is point))))) ; (append (list 'throw (car throw) 'and (cadr throw) '--) ; suffix)))

  13. (defun try-for-point (winpoint) ;g (let* ((throw (throw-dice)) (throwpoint (say-throw throw)) (suffix (cond ((equal throwpoint 7) (list 'you 'lose)) ((equal throwpoint winpoint) (list 'you 'win)) (t (list 'throw 'again))))) (cons 'throw (cons (car throw) (cons 'and (cons (cadr throw) (cons '-- (cons throwpoint (cons '-- suffix)))))))))

More Related