Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scheme: Compound Data Chapter 6 of HTDP Ms. Knudtzon September 19.

Similar presentations


Presentation on theme: "Scheme: Compound Data Chapter 6 of HTDP Ms. Knudtzon September 19."— Presentation transcript:

1 Scheme: Compound Data Chapter 6 of HTDP Ms. Knudtzon September 19

2 Checkpoint Homework: check-color Exercise

3 Introduction Rarely do we have such simple data that we can represent it with a single number, string, symbol, or boolean  Usually we have several pieces of data about one object that we are interested in Example: A cd with artist name, title, price, etc  We compound several pieces of data into one In Scheme, this is a structure

4 Representing Pixels A pixel is like a Cartesian point, with x & y coordinates (but the y direction is downwards) In Scheme, there is a posn structure that combines two numbers  To create a posn: (make-posn 3 4)  To get the individual numbers back: (posn-x myPosn) (posn-y myPosn)

5 Distance to 0 Let’s write a function that computes how far a pixel is from the origin ;; distance-to-0: posn --> number ;; computes distance of a-posn to the origin ;; Example: (distance-to-0 (make-posn 8 6)) = 10 (define (distance-to-0 a-posn) What is our next step?  Let’s start with what we know about the data

6 Distance Function (define (distance-to-0 a-posn) … (posn-x a-posn) … … (posn-y a-posn) … ) This is basically our template for any posn function - then we can just fill in the parts that are specific to this specific problem: (define (distance-to-0 a-posn) (sqrt (+ (sqr (posn-x a-posn)) (sqr (posn-y a-posn))))) ;; Test Cases should follow

7 Structure Definitions You’ve seen how to use an already defined structure, but how do you create your own structures? Let’s look at how posn is written as an example

8 Posn Definition (define-struct posn (x y)) When DrScheme evaluates this definition, it automatically creates three operations for use to create data and to program:  make-posn, the constructor, which creates posn structures  posn-x, a selector, which extracts the x coordinate  posn-y, a selector, which extracts the y coordinate

9 Another Example (define-struct entry (name zip phone)) To use: (make-entry “Ms. K” 20016 ‘537-2932) (define x (make-entry “Ms. K” 20016 ‘537-2932)) Think of the structure as a box with a compartment for each field: And you can access each compartment separately: (entry-name x) or (entry-zip x) or (entry-phone x) Name: Ms. K Zip: 20016 Phone: 537-2932 x Just gives the struct a name

10 Formalizing Matters What if we said: (define p (make-posn ‘Albert ‘Einstein)) and then tried to use the “posn” with: (distance-to-0 p) We would get errors! So we need to formalize how we intend to use out structures and how we construct the elements of those structures  We do this with a Data Definition

11 Data Definition Combination of English and Scheme that is the contract for that structure  Scheme doesn’t check the contract, but you assume that users of your structure or functions will follow what you have specified For example, we would say ; A posn is a structure ; (make-posn x y) ; Where x and y are numbers

12 Design Recipe Let’s see how the Design Recipe scales to make it easy to think out the problems for programs with structures Data Definition Examples of Data Template Contract & Purpose Examples (Test Cases) Header and Body

13 Data Definition Search the problem statement for descriptions of the objects needed and design data representations based on that (define-struct student (last first teacher)) ; A student is a structure: (make-student l f t) where l, f & t ; are symbols Or a shortcut I use: (define-struct student (last first teacher)) ; A student is a structure: (make-student symbol symbol symbol)

14 Examples of Data Then show (or define) the kinds of data that are acceptable with your data definition (make-student ‘henry ‘mills ‘Ms.K) (define nickI (make-student ‘nick ‘ink ‘Ms.K)) (define nickM (make-student ‘nick ‘m ‘ms.k))

15 Template A function that inputs a structure is likely to compute its results from the components of the structure, so it helps to start with those (using the selector expressions) ;; student-func: student --> ???? ;; purpose: (define (student-func aStudent) … (student-last aStudent) … … (student-first aStudent) … … (student-teacher aStudent) … )

16 Rest of Recipe Contract & Purpose: do these for the specific functions you are writing Examples: Write examples (test cases) for the function  Use the data examples you wrote earlier (that’s why I recommend using the defines) (my-s-func nickM) Header & Body: Using the template, formulate an expression that computes the answer from the available data and other Scheme functions

17 Putting It All Together Class Exercise: Create data for boas containing name, length, what they eat  Write a function isShorter? that determines whether a boa is shorter than a given amount Design Recipe  Data Definition  Examples Of Data  Template  Contract & Purpose  Examples (Test Cases)  Header & Body

18 Scheme Mini-Project Draw Teachpack: Introduces simple graphics operations using posn structures  draw-solid-line: posn posn  draw-solid-rect: posn number number color  draw-solid-disk: posn number color  draw-circle: posn number color Each operation produces boolean Each has a matching clear- operation Your Scheme mini-project (see project handout) will use this drawing package

19 Scheme: Mixed Data Chapter 7 of HTDP Ms. Knudtzon September 20

20 Checkpoint 80-point Quiz #3

21 Graphical Test Cases Special > Insert Test Case

22 Structures Yesterday our structures were made up of symbols, numbers, booleans & strings Today, let’s consider structures that can be made of other structures We will also look at how to do some error checking in our functions to make sure we get the expected kind of data

23 Distinguishing Data The other day, I introduced the symbol? and string? operators called predicates  They recognize particular kinds of data  Also available: number? boolean? struct? For each structure that we make, Scheme also makes a predicate  posn?  student?  boa?  Etc So any function we write can check the type of its inputs

24 Mixed Data We can have one name refer to multiple types of data Yesterday we defined a dillo and a boa Let’s do a data definition for an animal ; An animal is either ; a boa structure: ; (make-boa symbol number), or ; a dillo stucture ; (make-dillo symbol number number boolean)

25 Animal-func So given that we can use predicates to distinguish our data, let’s make a template for animal data ; animal-func: animal --> ??? (define (animal-func an-ani) (cond [(boa? an-ami) … (boa-name an-ami) (boa-length an-ami) (boa-food an-ami) … ] [(dillo? an-ami) … (dillo-name an-ami) (dillo-age an-ami) (dillo-timesRunOver an-ami) (dillo-dead? an-ami) … ]))

26 New template Make a cond block to distinguish the different kind of available data Pull out the available data for each case Then you can put this all in a comment box When you need to write a new function, you can copy the template, fill in the blanks and erase the stuff you don’t need anymore

27 Animal Function Let’s add a length field to the dillo definition so that we could compare animal lengths Using our template, we could then create a new animal function, tooShort? which takes an animal and returns true if the boa is shorter than 6 or 2 for a dillo

28 Another Example Lets define a shape class which can be circle or square structures (which also need to be defined), each of which require a posn and a number  The posn is the circle’s center and the upper left corner of the square  The number is the circle’s radius and the square’s length Follow the design recipe to sketch out everything for a shape  Then write a perimeter function for a shape

29 Scheme: Mixed Data Cont’d and Lists Chapter 7 & 9 of HTDP Ms. Knudtzon September 21

30 Shapes Continued Lets define a shape class which can be circle or square structures (which also need to be defined), each of which require a posn and a number  The posn is the circle’s center and the upper left corner of the square  The number is the circle’s radius and the square’s length Follow the design recipe to sketch out everything for a shape  Then write a perimeter function for a shape

31 Error checking Note: the predicates (type questions) that we have been using to check type for functions could also be used for simple input checking If we expect a function to only receive numbers and we get something else, we could cause an error, which input a symbol (the function name) and a string (the error message you want displayed) (define (my-func foo) (cond [ (number? foo) …] [ else (error ‘my-func “number expected”)])) This way you aren’t returning something that isn’t expected (like a string message when the function contract says it returns a number)

32 Lab Exercise (Homework) Develop structure and data definitions for a collection of vehicles. The collection should include at least buses, limos, cars, and police- cars. All vehicles should have a licensePlate, milesDriven, and gasTankCapacity. Add a structure-specific attribute for each class of vehicle. Then develop a template for functions that inputs a vehicle. Expanding on the template, write a function that checks the license plate of a vehicle against a “wantedVehicle” and returns true if it is a match (inputting two vehicles to the function) Make another function that calculates the miles per gallon that a given vehicle has gotten, assuming it has used a whole tank.

33 Lists The wonderful world of arbitrary-length structures

34 Lists We all understand what a list is - we make them all the time So the question is how do we make these lists in Scheme? When we form a list, we always start with the empty list and then we add elements to it

35 Lists in Scheme In Scheme, the empty list is simply: empty And then to construct a longer list, we use the cons operator: (cons “Buy milk” empty) First “Buy milk” rest empty

36 And again To add more items, we keep using cons again: (cons “Water plants” (cons “Buy milk” empty)) First “Buy milk” rest empty restFirst “Water plants” Etc

37 Lists of Numbers We can makes lists of anything - here’s a list of 10 numbers: (cons 0 (cons 1 (cons 2 (cons 3 (cons 4 (cons 5 (cons 6 (cons 7 (cons 8 (cons 9 empty ))))))))))

38 Mixed Data Lists Lists can also have values of multiple kinds: (cons ‘APCS (cons 3 (cons “testing testing” (cons true empty))))

39 Using Lists Let’s says we are given a list a numbers and we want to add the numbers on the list. How do we do this? For simplicity, let’s say that we only have a list of three numbers ;; A list-of-3-nums is ; (cons x (cons y (cons z empty))) ;; where x, y, and z are numbers

40 Writing add-up-3 ;; add-up-3: list-of-3-nums --> number ;; to add up 3 numbers in a list ;; example/test case: ;; (= (add-up-3 (cons 2 (cons 1 (cons 3 empty)))) 6) (define (add-up-3 mylist) …)

41 Selectors for lists first and rest are the operators we need to pull out parts of a list The data definitions for first and last are:  ;; first: non-empty-list --> value  ;; rest: non-empty-list --> list (define li (cons 10 (cons 20 (cons 5 empty))) What is:  (rest li)  (first (rest li))  (rest (rest li))  (first (rest (rest li)))  (rest (rest (rest li)))

42 Add-up-3 So what do we need to do here? ;; add-up-3: list-of-3-nums --> number ;; to add up 3 numbers in a list ;; example/test case: ;; (= (add-up-3 (cons 2 (cons 1 (cons 3 empty)))) 6) (define (add-up-3 mylist) …)

43 List Data Definitions We need a way to describe the lists we want to store and the kind of data they store.  The data definitions good references and they remind us how to build data Example: ;; A list-of-symbols is either: ; the empty list, empty, or ; (cons s los) where s is a symbol and los is a list of symbols Note here we have the data definition referring back to itself

44 LOS or LOST?? ;; A list-of-symbols is either: ; the empty list, empty, or ; (cons s los) where s is a symbol and los is a list of symbols How does this work? Does it make sense?  We can construct elements from it (the data definition)  It works for any cases of our list that we can think of

45 Using the Design Recipe Everything is as before, but the template needs to have cond- expressions for each clause in the data definition  In the case of a list, empty and cons ;; General Template for list-of-symbols ;; alos-func: list-of-symbols --> ??? (define (alos-func alos) (cond [ (empty? alos) … ] [ (cons? alos) … (first alos) … … (alos-func (rest alos)) …. ])) Same number of arrows as in data definition

46 LOS Function Example 1 ;; length : list-of-symbols --> number ;; consumes a list of symbols and returns ;; the number of items in the list (define (length alos) … ) Try this together in Scheme

47 Homework Lab Exercise from yesterday (Vehicle Structures) Review lengthof function from class

48 (Thursday Lecture) Review and practice on the whiteboard about lists and list functions Individual students should have notes

49 Scheme: Lists Cont’d Chapter 9-10 of HTDP Ms. Knudtzon September 23

50 Checkpoint 80 point quiz #4b (instead of yesterday’s) List exercises  dollar-store?  delta  check-range1?  check-range?  average-price we will do together

51 Average-price Note: This next one requires some additional thought. Make sure that you do the test cases before writing the method and pay attention to what actions you are doing to calculate the result. Exercise 9.5.7. Define the function average-price. It consumes a list of toy prices and computes the average price of a toy. The average is the total of all prices divided by the number of toys. Iterative Refinement: First develop a function that works on non-empty lists. Then produce a checked function that signals an error when the function is applied to an empty list.

52 What is recursion? In Computer Science, recursion is typically defined as self-reference (having a function/method call itself) Fractals are a visual example of recursion  Each small part is a replica of the original part  A “self-similar” structure that can be seen at different levels of detail

53 Fractals http://en.wikipedia.org/wiki/Image:Fractal_Broccoli.jpg http://en.wikipedia.org/wiki/Image:Julia_set_%28indigo%29.png Julia Set Romanesco broccoli

54 Recursion In mathematics and computer science, we use recursion as an elegant way to approach problem solving  You can reduce the number of steps in a problem solution if the steps all do the same thing  It’s a way to solve a problem by basing the solution on the known result of a “smaller” problem In Scheme, we’ve seen lists, in which the way we define the data is recursive  So the recursive functions (problem-solving) just “fall-out” from the templates which are based on the data definitions

55 List Project The person responsible for counting the votes in a recent Washington DC primary election needs some help. Given the recent problems with vote counting across the nation, he is concerned that the old system might cause controversy. As an expert programmer, you have been asked to help develop a new system that will correctly count the votes in a recent election, as well as providing some statistics about the election. Unfortunately, the person you are working for is very busy, and has only provided you with some raw data and told you what you are expected to program. It is your job to figure out a way to finish the tasks before the deadline next Thursday.


Download ppt "Scheme: Compound Data Chapter 6 of HTDP Ms. Knudtzon September 19."

Similar presentations


Ads by Google