(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)))

Slides:



Advertisements
Similar presentations
Falcon on a Cloudy Day A Ro Sham Bo Algorithm by Andrew Post.
Advertisements

Chapter 2: Understanding Structure
Computational Methods for Management and Economics Carla Gomes Module 8b The transportation simplex method.
Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while.
Limitations of ANOVA ©2005 Dr. B. C. Paul. The Data Size Effect We Did ANOVA with one factor We Did ANOVA with one factor We Did it with two factors (Driver.
Question 1 Which is an example of a rational expression? A. ½ B. 5 C. D. None of the above.
Use logic to teach the computer how to play a game
For the ultimate in entertainment, play with 2 or more people, individually or as a team Players alternate turns Each player picks an “answer” and must.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
Slide 1 CS3 Fall 2005 Lecture week 13: Introduction to the Big Project.
Intro to Robots 10 Artificial Intelligence “I want to make a computer that will be proud of me” Daniel Hillis.
Coye Cheshire & Andrew Fiore June 19, 2015 // Computer-Mediated Communication Iterated Prisoner’s Dilemma.
Midterm Logistics Where? 2050 VLSB When? Monday, 4:10 to 5:40 What to do? –“Review problems” from Thursday/Friday in UCWise. –Two practice exams and solutions.
Time for playing games Form pairs You will get a sheet of paper to play games with You will have 12 minutes to play the games and turn them in.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Recursion1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Jan 2010 Recursion in Alice.
Club Philosophy Winning 1)Select players 2)Measure success by league tables, trophies + tournaments 3)Play strongest team for the majority/all of the games.
Learning what questions to ask. 8/29/03Decision Trees2  Job is to build a tree that represents a series of questions that the classifier will ask of.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
SPANISH CRYPTOGRAPHY DAYS (SCD 2011) A Search Algorithm Based on Syndrome Computation to Get Efficient Shortened Cyclic Codes Correcting either Random.
Z. Z Scissors Paper Stone  Scissors beats paper (cuts it)  Paper beats rock (wraps it)  Rock beats scissors (blunts it)  Showing the same is a draw.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Loops CS 103 February 19, 2007 Presented by Nate.
Game Time! Why I Should Have More Video Game Time.
Debugging Strategies from Software Carpentry. Agan's Rules Many people make debugging harder than it needs to be by: Using inadequate tools Not going.
Good old lessons in teamwork from an age-old fable The Tortoise And The Hare.
Chapter 3 Fair Division.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
1/38. 2/38 Tagged Data Tag: a symbol in a data structure that identifies its type Why we need tags Extended example: evaluating arithmetic expressions.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
1. Understand the application of Pseudo Code for programming purposes 2. Be able to write algorithms in Pseudo Code.
Recitation 7: Data Abstraction II 28 Feb 2007 RI: Gerald Dalley Announcements Solutions, handouts, etc.:
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
SICP Tagged data Why do we need tags Concept of tags Extended example.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12.
Review of Previous Classes Declaring Variables - var myVar:DataType = value Data Types – Number, uint, String, Boolean Functions – parameters, return.
ARTIFICIAL INTELLIGENCE (CS 461D) Princess Nora University Faculty of Computer & Information Systems.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Game Creation Terminology. Procedure A set of instructions that performs a specific routine when required at several different points in the game. EXAMPLE:
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
Elimination To solve systems by elimination. The goal in solving a system is to eliminate the 2 equations with 2 unknowns (variables) to 1 equation with.
Design of Rock-Paper-Scissors end Print instructions Play game Ask user If (s)he wants to play again Play again? Y Print stats: wins, losses and ties start.
MOOR ERROR HANDLING Types of error How to test your code for errors How to detect errors How to recover from errors.
SICP Tagged data Why do we need tags Concept of tags Extended example.
Additional Scheme examples
Recap: If, elif, else If <True condition>:
Tagged Data Tag: a symbol in a data structure that identifies its type
Class 11: Two-argument recursion
Example of formula (defun roots (a b c) (list
UNIT 3 – LESSON 5 Creating Functions.
CSC 533: Organization of Programming Languages Spring 2008
Week 8 - Programming II Today – more features: Loop control
Writing Functions( ) (Part 5)
Visual Basic – Decision Statements
Intro to Programming CURIE 2011.
The first number is posted telling what random number was selected, I did this for testing purposes, in the real thing it would not be there. Since the.
Selection Statements Chapter 3.
CSC 533: Organization of Programming Languages Spring 2007
Fun… Tree Diagrams… Probability.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Pseudo-Code Conditional Branches
Presentation transcript:

(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) Return value

(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

(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))))) play (paper, scissors) COND – a decision list

; 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

; 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

; 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))))

; 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

(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

;5.6 pp (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))))

(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

(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)))))))

;(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)))

(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)))))))))