TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010.

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

RAPTOR Syntax and Semantics By Lt Col Schorsch
Events Chapter 7. Interactive Real world is interactive User determines order of actions instead of programmer.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
CSC 160 Computer Programming for Non-Majors Lecture #8: Animations I Prof. Adam M. Wittenstein
16-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
CSC 160 Computer Programming for Non-Majors Lecture #11: Conditionals II Prof. Adam M. Wittenstein
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Section 4.3: Conditions and Conditional Functions.
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
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.
28-Jun-15 Recognizers. 2 Parsers and recognizers Given a grammar (say, in BNF) and a string, A recognizer will tell whether the string belongs to the.
Lecture 3 IAT 800. Sept 15, Fall 2006IAT 8002 Suggestions on learning to program  Spend a lot of time fiddling around with code –Programming is something.
CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein
Copyright © Cengage Learning. All rights reserved.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
TeachScheme, ReachJava Adelphi University Tuesday afternoon June 23, 2009.
Software design and development Marcus Hunt. Application and limits of procedural programming Procedural programming is a powerful language, typically.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
TeachScheme, ReachJava Adelphi University Monday afternoon July 12, 2010.
Lab 9 – User Forms Design. User Forms What are user forms? –Known as dialog boxes –Major ways for getting user input An example of using user forms: Monthly.
Making a Boat Racing Game in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2010.
CompSci 4 Chap 5 Sec 1 Oct 13, 2005 Prof. Susan Rodger Note: thanks to Wanda Dann and Steve Cooper for slide ideas.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Study Guide For Test Chapter 5, 6,& 7 Test is Friday, May 15th.
Invitation to Computer Science, Java Version, Second Edition.
Shorter of two objects and changing color V2 Functions, events and setting the color in sequence and randomly This is a modification of the Changing Color.
Foundations of Computer Science Computing …it is all about Data Representation, Storage, Processing, and Communication of Data 10/4/20151CS 112 – Foundations.
TeachScheme, ReachJava Adelphi University Tuesday afternoon July 13, 2010.
Examining Two Pieces of Data CS 5010 Program Design Paradigms “Bootcamp” Lesson © Mitchell Wand, This work is licensed under a Creative.
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Keyboard and Events. What about the keyboard? Keyboard inputs can be used in many ways---not just for text The boolean variable keyPressed is true if.
Chapter 1 - Getting to know Greenfoot
Something to amuse you… CS UWO minutes.
TeachScheme, ReachJava Adelphi University Wednesday morning June 24, 2008.
Design Recipe Team Challenge! 1)Every team submits their answer in writing. 2)Every team can gain points for every question. 3)Wrong answers are worth.
Using Inheritance to Share Implementations CS 5010 Program Design Paradigms "Bootcamp" Lesson 12.1 © Mitchell Wand, This work is licensed under.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
ITIP © Ron Poet Lecture 12 1 Finding Out About Objects.
Introduction to Universe Programs CS 5010 Program Design Paradigms “Bootcamp” Lesson TexPoint fonts used in EMF. Read the TexPoint manual before.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
CIS 3.5 Lecture 2.2 More programming with "Processing"
TeachScheme, ReachJava Adelphi University Thursday afternoon July 15, 2010.
TeachScheme, ReachJava Stephen Bloch, Adelphi University, Garden City, NY John Clements, Cal Poly, San Luis Obispo, CA Kathi Fisler, Worcester Polytechnic.
TeachScheme, ReachJava Adelphi University Friday morning July 16, 2010.
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
Functional Programming Language 1 Scheme Language: part 2.
1 Teaching Software Correctness May 13-15, 2008, University of Oklahoma Rex Page, U Assistants Carl Eastlund (lead), Northeastern
A Case Study: Space Invaders CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative.
Summer Computing Workshop. Session 3 Conditional Branching  Conditional branching is used to alter the normal flow of execution depending on the value.
Interactive Programming Alice. Control of flow Control of flow -- how the sequence of actions in a program is controlled. What action happens first, what.
31/01/ Selection If selection construct.
Chapter 2 More Controls Programming in C#. NET Objectives Use text boxes, group boxes, check boxes, radio buttons, and picture boxes effectively.
Lecture 04: Theory of Automata:08 Transition Graphs.
Using Inheritance to Share Implementations CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Introduction to TouchDevelop Lesson 3 – Comments & Lists Created by S. Johnson
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Conditionals.
Discussion 4 eecs 183 Hannah Westra.
Examining Two Pieces of Data
Unit 11 – PowerPoint Interaction
slides courtesy of Eric Roberts
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
The Iterative Design Recipe
Presentation transcript:

TeachScheme, ReachJava Adelphi University Tuesday morning July 13, 2010

July TeachScheme, ReachJava Booleans ; = : number number -> boolean ; >, =, <= : similar ; string=? : string string -> boolean ; image=? : image image -> boolean ; not : boolean -> boolean ; and : boolean … -> boolean ; or : boolean … -> boolean

July TeachScheme, ReachJava A Boolean-valued function ; 18-to-25? : number -> boolean (define (18-to-25? age) ; age a number (and (>= age 18) (<= age 25))) (check-expect (18-to-25? 17) false) (check-expect (18-to-25? 18) true) (check-expect (18-to-25? 22) true) (check-expect (18-to-25? 25) true) (check-expect (18-to-25? 26) false)

Type-checking animations Common beginner mistake in writing animations: handler returns wrong type. There's another kind of handler: (check-with function) ; function : anything -> boolean Built-in examples: image? number? string? etc. July TeachScheme, ReachJava 20104

Type-checking animations Example: (big-bang 0 (check-with number?) (on-tick...) (on-mouse...) (on-draw...)) Now if student writes a handler that returns the wrong type, there'll be a more-informative error message. The check-with clause also serves as documentation. July TeachScheme, ReachJava 20105

July TeachScheme, ReachJava Stopping an Animation There's another kind of handler: (stop-when function) where function has contract model -> boolean

July TeachScheme, ReachJava Stopping an Animation Example: a growing disk that stops growing when the radius reaches 100 ; model is a number representing radius ; over-100? : number -> boolean (define (over-100? r) (> r 100)) (big-bang 0 (check-with number?) (on-tick add1 1/4) (on-draw blue-circle-of-size) (stop-when over-100?))

July TeachScheme, ReachJava Conditionals (cond [boolean-expr-1 answer-1] [boolean-expr-2 answer-2] … [boolean-expr-n answer-n]) tries each boolean-expr in turn. As soon as one of them evaluates to true, it evaluates and returns the corresponding answer.

July TeachScheme, ReachJava Functions with conditionals Write a function reply that takes in one of the strings "good morning", "good afternoon", or "good night", and returns "I need coffee!", "I need a nap!", or "Bed time!" respectively.

July TeachScheme, ReachJava Functions with conditionals Contract & data analysis ; reply : string -> string Data analysis: the input falls into three categories: "good morning", "good afternoon", and "good night". The output likewise falls into three categories: "I need coffee!", "I need a nap!", or "Bed time!"

July TeachScheme, ReachJava Functions with conditionals Test cases Need a test case for each category of input, and each category of output. Conveniently, they match up one-to-one in this example. (check-expect (reply "good morning") "I need coffee!") (check-expect (reply "good afternoon") "I need a nap!") (check-expect (reply "good night") "Bed time!")

July TeachScheme, ReachJava Functions with conditionals Skeleton & inventory Since there are three categories of input (and output), we'll probably need a 3-branch cond: (define (reply greeting) ; greeting ; a string (cond [ question answer ] [ question answer ] [ question answer ] ) )

July TeachScheme, ReachJava Functions with conditionals Body Fill in either all three answers, or all three questions, whichever is easier. In this case, the answers. (define (reply greeting) ; greeting ; a string (cond [ question "I need coffee!" ] [ question "I need a nap!" ] [ question "Bed time!" ] ))

July TeachScheme, ReachJava Functions with conditionals Body Then do the other of (questions, answers). (define (reply greeting) ; greeting ; a string (cond [ (string=? greeting "good morning") "I need coffee!" ] [ (string=? greeting "good afternoon") "I need a nap!" ] [ (string=? greeting "good night") "Bed time!" ] ))

July TeachScheme, ReachJava Functions with conditionals Error-checking Quibble: this isn't idiot-proof. What happens if input isn't one of the three recognized inputs? Answer: ugly error message. Solution: revise data analysis (and everything that depended on it) Input is "good morning", "good afternoon", "good night", or anything else. Output is "I need coffee!", "I need a nap!", "Bed time!", or "Huh?" Add one more test case (check-expect (reply "buenas noches") "Huh?")

July TeachScheme, ReachJava Functions with conditionals Error-checking Add one more cond case: (define (reply greeting) ; greeting ; a string (cond [ (string=? greeting "good morning") "I need coffee!" ] [ (string=? greeting "good afternoon") "I need a nap!" ] [ (string=? greeting "good night") "Bed time!" ] [ else "Huh?" ] ))

July TeachScheme, ReachJava Isomorphism The shape of the data determines the shape of the code and tests. Say this ten times before bed.

July TeachScheme, ReachJava Another example ; pepper-scale : number -> string ("serrano", "cayenne", "thai", "habanero") ; Scoville > serrano ; Scoville > cayenne ; Scoville > thai ; Scoville up -> habanero ; Data analysis: input is a number, but falls into 4 categories: ; , , , up. ; Output is likewise four categories: "serrano", "cayenne", "thai"., "habanero"

July TeachScheme, ReachJava Another example ; Test cases therefore need to include all 4 categories plus borderlines. (check-expect (pepper-scale 5000) "serrano") (check-expect (pepper-scale 16500) "serrano") (check-expect (pepper-scale 25000) "serrano") (check-expect (pepper-scale 30000) "cayenne") (check-expect (pepper-scale 42000) "cayenne") (check-expect (pepper-scale 50000) "cayenne") (check-expect (pepper-scale 65000) "thai") (check-expect (pepper-scale 85000) "thai") (check-expect (pepper-scale 90000) "thai") (check-expect (pepper-scale ) "habanero") (check-expect (pepper-scale ) "habanero")

July TeachScheme, ReachJava Another example: skeleton & inventory There are four categories, hence a four-branch cond: (define (pepper-scale scoville) ; scoville a number (cond [ q a ] [ q a ] [ q a ] [ q a ] ))

July TeachScheme, ReachJava Another example: body Fill in the answers (define (pepper-scale scoville) ; scoville a number (cond [ q "serrano" ] [ q "cayenne" ] [ q "thai" ] [ q "habanero" ] ))

July TeachScheme, ReachJava Another example: body Fill in the questions (define (pepper-scale scoville) ; scoville a number (cond [ (and (>= scoville 5000) ( = scoville 30000) ( = scoville 65000) ( = scoville ) "habanero" ] ))

July TeachScheme, ReachJava Another example ; rough-age : number -> string ("child", "teenager", or "adult") ; Data analysis: input is a number, but falls into 3 categories: ; under 13, 13-19, and over 19. ; Output is likewise three categories: "child", "teenager", "adult". ; Test cases therefore need to include all 3 categories plus borderlines. (check-expect (rough-age 7) "child") (check-expect (rough-age 13) "teenager") (check-expect (rough-age 16.3) "teenager") (check-expect (rough-age 19) "teenager") (check-expect (rough-age 20) "adult")

July TeachScheme, ReachJava Functions with conditionals ; rough-age : number -> string ("child", "teenager", or "adult") ; Data analysis: input is a number, but falls into 3 categories: ; under 13, 13-19, and over 19. (define (rough-age age) ; age a number (cond [( = age 13) ( age 19) "adult"])) (check-expect (rough-age 7) "child") (check-expect (rough-age 13) "teenager") (check-expect (rough-age 16.3) "teenager") (check-expect (rough-age 19) "teenager") (check-expect (rough-age 20) "adult")

July TeachScheme, ReachJava We could have written… (define (rough-age age) ; age a number (cond [(< age 13) "child"] [(<= age 19) "teenager"] [else "adult"])) by relying on the fall-through behavior of the conditional. Advantage: less typing. Advantage: save a few nanoseconds of run time (maybe). Disadvantage: you can't tell when a particular branch will happen just by looking at that condition; you have to also look at all the previous ones Disadvantage: branches of conditional can no longer be reordered without changing function behavior. Disadvantage: the isomorphism to the input data type is less clear. Use else only when you really mean "anything else".

July TeachScheme, ReachJava Animations with conditionals Can now assign animations that decide among a finite set of cases, e.g. slide show of a sequence of pictures stop light that cycles red, green, yellow, red… See PP chap. 17 for lots of examples

Mouse handlers Contract : model num(x) num(y) string-> model Events: "button-down", "button-up", "drag", "move", "enter", "leave" … Typically, one or two "interesting" events and an "anything else" category. Code structure: cond with one or two (string=?... …) questions and an else July TeachScheme, ReachJava 2010

Example mouse handler Exercise in book: ; add-dot-on-mouse-down : image(old) num(x) num(y) string(event) -> image (check-expect (add-dot-on-mouse-down BACKGROUND "button-down") (place-image DOT BACKGROUND)) (check-expect (add-dot-on-mouse-down BACKGROUND "button-up") BACKGROUND) July TeachScheme, ReachJava 2010

Example mouse handler (define (add-dot-on-mouse-down old x y event) ; oldimage ; x, ynumbers ; eventstring (cond [(string=? event "button-down") (place-image DOT x y old)] [else old]; ignore this event )) July TeachScheme, ReachJava 2010

A new type: key A key is a string: either a 1-character string like "r", "7", "F", or a longer string specifying a special key: "left", "right", "down", "up", "escape", "f1", "home", …) Built-in function ; key=? : key key -> boolean July TeachScheme, ReachJava 2010

Example Write an animation of a calendar that starts at the middle of the screen and moves left or right in response to left and right arrow keys on the keyboard Ex in book July TeachScheme, ReachJava 2010

Animation: model & handlers Let's use a number as the model, representing the x coordinate (the y coordinate will be fixed at, say, 50) We need a redraw handler ; calendar-at-x: number -> image and a key handler ; handle-key: number key -> number July TeachScheme, ReachJava 2010

Animation: draw handler ; calendar-at-x: number -> image (define (calendar-at-x x) ; x number (place-image calendar x 50 BACKGROUND)) (check-expect (calendar-at-x 27) (place-image calendar BACKGROUND)) (check-expect (calendar-at-x 193) (place-image calendar BACKGROUND)) July TeachScheme, ReachJava 2010

Animation: key handler Model is a number representing x coordinate Want to respond to left, right arrow keys ; handle-key : number(x) key -> number (check-expect (handle-key 10 "D") 10) (check-expect (handle-key 10 "left") 9) (check-expect (handle-key 10 "right") 11) (check-expect (handle-key 10 "up") 10) July TeachScheme, ReachJava 2010

Animation: key handler (define (handle-key x key) ; xnumber ; keykey (cond [(key=? key "left")…] [(key=? key "right")…] [else…] )) July TeachScheme, ReachJava 2010

Animation: key handler (define (handle-key x key) ; xnumber ; keykey (cond [(key=? key "left")(- x 1)] [(key=? key "right")(+ x 1)] [elsex] )) July TeachScheme, ReachJava 2010

Running the animation (big-bang (/ WIDTH 2) (check-with number?) (on-draw calendar-at-x) (on-key handle-key)) July TeachScheme, ReachJava 2010

Defining a type "by choices" Type = "this string or that string or …" Type = "this range of numbers or that range of numbers or …" Type = "this type or that type or …" Test cases: at least one for each category of input or output July TeachScheme, ReachJava 2010

Code for definition by choices A cond w/ N branches, where N is the number of categories in the input type For strings, usually use string=? For numbers,, =, =, … For keys, key=? For types, …? July TeachScheme, ReachJava 2010

Discriminator functions (built-in) number? : anything -> boolean string? : anything -> boolean image? : anything -> boolean key? : anything -> boolean etc. Tell whether something is of the specified type July TeachScheme, ReachJava 2010

Example Write a function big? that takes in a number or a string or an image. Numbers > 1000 are "big"; strings longer than 10 characters are "big"; images with height* width > are "big". July TeachScheme, ReachJava 2010

Contract & data analysis ; big? : number, string, or image -> boolean ; Data analysis: input is either number, string, or image ; In each case, it's broken down into two sub-categories July TeachScheme, ReachJava 2010

Test cases (check-expect (big? 347) false) (check-expect (big? 1000) false) (check-expect (big? 1200) true) (check-expect (big? "alison") false) (check-expect (big? "passphrase") false) (check-expect (big? "brontosaurus") true) (check-expect (big? calendar) false) (check-expect (big? (rectangle "solid" "purple")) false) (check-expect (big? (ellipse "outline" "chartreuse")) true) July TeachScheme, ReachJava 2010

Skeleton & inventory (define (big? thing) ; thing a string, number, or image ) July TeachScheme, ReachJava 2010

Body, first draft (define (big? thing) ; thing string, number, or image (cond [ q a ] [ q a ] [ q a ] )) July TeachScheme, ReachJava 2010

Body, second draft (define (big? thing) ; thing string, number, or image (cond [(string? thing) a ] [(number? thing) a ] [(image? thing) a ] )) July TeachScheme, ReachJava 2010

Body, third draft (define (big? thing) ; thing string, number, or image (cond [(string? thing) (> (string-length thing) 10)] [(number? thing) (> thing 1000)] [(image? thing) (> (* (image-width thing) (image-height thing)) 10000)] )) July TeachScheme, ReachJava 2010