section 6.1: structures (continued)

24
Section 6.1: Structures Section 6.1: Structures (continued) (continued)

Upload: chick

Post on 17-Mar-2016

39 views

Category:

Documents


0 download

DESCRIPTION

Section 6.1: Structures (continued). Example 1: in-left-side?. ; Purpose: To determine whether a point is in the leftmost 150 pixels of the screen. ; Contract : posn -> boolean "Examples of in-left-side?:" (in-left-side? (make-posn 5 7)) "should be" true - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Section 6.1: Structures (continued)

Section 6.1: StructuresSection 6.1: Structures(continued)(continued)

Page 2: Section 6.1: Structures (continued)

Example 1: in-left-side?; Purpose: To determine whether a point is in the leftmost 150 pixels

of the screen.; Contract : posn -> boolean

"Examples of in-left-side?:"(in-left-side? (make-posn 5 7)) "should be" true(in-left-side? (make-posn 200 7)) "should be" false(in-left-side? (make-posn 150 7)) "should be" false

Page 3: Section 6.1: Structures (continued)

Example 1: in-left-side?; Purpose: To determine whether a point is in the leftmost 150 pixels

of the screen.; Contract : posn -> boolean(define (in-left-side? where) … where … )

"Examples of in-left-side?:"(in-left-side? (make-posn 5 7)) "should be" true(in-left-side? (make-posn 200 7)) "should be" false(in-left-side? (make-posn 150 7)) "should be" false

Page 4: Section 6.1: Structures (continued)

Example 1: in-left-side?; Purpose: To determine whether a point is in the leftmost 150 pixels

of the screen.; Contract : posn -> boolean(define (in-left-side? where) … where … ; a posn … (posn-x where) … ; a number … (posn-y where) … ; a number )"Examples of in-left-side?:"(in-left-side? (make-posn 5 7)) "should be" true(in-left-side? (make-posn 200 7)) "should be" false(in-left-side? (make-posn 150 7)) "should be" false

Page 5: Section 6.1: Structures (continued)

Example 1: in-left-side?; Purpose: To determine whether a point is in the leftmost 150 pixels

of the screen.; Contract : posn -> boolean(define (in-left-side? where) (< (posn-x where) 150) ; a boolean

)"Examples of in-left-side?:"(in-left-side? (make-posn 5 7)) "should be" true(in-left-side? (make-posn 200 7)) "should be" false(in-left-side? (make-posn 150 7)) "should be" false

Page 6: Section 6.1: Structures (continued)

Example 2: above-diagonal?

• Takes a point and determines whether it is above the diagonal.

• The green points return true.

• The red points return false.

Page 7: Section 6.1: Structures (continued)

Example 2: above-diagonal?; Purpose: To determine if a point is above the diagonal.; Contract: posn -> boolean

"Examples of above-diagonal?:"(above-diagonal? (make-posn 5 7)) "should be" false(above-diagonal? (make-posn 200 7)) "should be" true(above-diagonal? (make-posn 7 7)) "should be" false

Page 8: Section 6.1: Structures (continued)

Example 2: above-diagonal?; Purpose: To determine if a point is above the diagonal.; Contract: posn -> boolean(define (above-diagonal? where) ; … where … a posn

)"Examples of above-diagonal?:"(above-diagonal? (make-posn 5 7)) "should be" false(above-diagonal? (make-posn 200 7)) "should be" true(above-diagonal? (make-posn 7 7)) "should be" false

Page 9: Section 6.1: Structures (continued)

Example 2: above-diagonal?; Purpose: To determine if a point is above the diagonal.; Contract: posn -> boolean(define (above-diagonal? where) ; … where … a posn ; … (posn-x where) … a number ; … (posn-y where) … a number

)"Examples of above-diagonal?:"(above-diagonal? (make-posn 5 7)) "should be" false(above-diagonal? (make-posn 200 7)) "should be" true(above-diagonal? (make-posn 7 7)) "should be" false

Page 10: Section 6.1: Structures (continued)

Example 2: above-diagonal?; Purpose: To determine if a point is above the diagonal.; Contract: posn -> boolean(define (above-diagonal? where) (> (posn-x where) (posn-y where)) )

"Examples of above-diagonal?:"(above-diagonal? (make-posn 5 7)) "should be" false(above-diagonal? (make-posn 200 7)) "should be" true(above-diagonal? (make-posn 7 7)) "should be" false

Page 11: Section 6.1: Structures (continued)

Example 3: distance-to-0

• Takes a point and finds its distance from the origin.

• Hint: the formula is (sqrt((x1-x2)2 + (y1-y2)2))

Page 12: Section 6.1: Structures (continued)

Example 3: distance-to-0; Purpose: To determine how far a point is from the origin.; Contract: posn -> number

"Examples of distance-to-0”(distance-to-0 (make-posn 3 4)) "should be" 5(distance-to-0 (make-posn 0 7)) "should be" 7(distance-to-0 (make-posn 10 10)) "should be" 141.4…

Page 13: Section 6.1: Structures (continued)

Example 3: distance-to-0; Purpose: To determine how far a point is from the origin.; Contract: posn -> number; Skeleton:; (define (distance-to-0 a-posn); …a-posn…)

"Examples of distance-to-0”(distance-to-0 (make-posn 3 4)) "should be" 5(distance-to-0 (make-posn 0 7)) "should be" 7(distance-to-0 (make-posn 10 10)) "should be" 141.4…

Page 14: Section 6.1: Structures (continued)

Example 3: distance-to-0; Purpose: To determine how far a point is from the origin.; Contract: posn -> number; Template:; (define (distance-to-0 a-posn); …a-posn… ; a posn; … (posn-x a-posn)… ; a number; … (posn-y a-posn)…) ; a number

"Examples of distance-to-0”(distance-to-0 (make-posn 3 4)) "should be" 5(distance-to-0 (make-posn 0 7)) "should be" 7(distance-to-0 (make-posn 10 10)) "should be" 141.4…

Page 15: Section 6.1: Structures (continued)

Example 3: distance-to-0; Purpose: To determine how far a point is from the origin.; Contract: posn -> number(define (distance-to-0 a-posn) (sqrt (+ (sqr (posn-x a-posn)) (sqr (posn-y a-posn)) )))

"Examples of distance-to-0”(distance-to-0 (make-posn 3 4)) "should be" 5(distance-to-0 (make-posn 0 7)) "should be" 7(distance-to-0 (make-posn 10 10)) "should be" 141.4…

Page 16: Section 6.1: Structures (continued)

Example 4: distance

• Takes two points and finds the distance between them.

• Hint: the formula is (sqrt((x1-x2)2 + (y1-y2)2))

Page 17: Section 6.1: Structures (continued)

Example 4: distance; Purpose: To find the distance between two points.; Contract: distance : posn posn -> number; (define (distance here there); … here … a posn; … there … a posn;)

"Examples of distance:"(distance (make-posn 4 5) (make-posn 4 5)) "should be 0"(distance (make-posn 4 5) (make-posn 1 5)) "should be 3"(distance (make-posn 4 5) (make-posn 4 17)) "should be 12"(distance (make-posn 4 5) (make-posn 7 9)) "should be 5"

Page 18: Section 6.1: Structures (continued)

Example 4: distance; Purpose: To find the distance between two points.; Contract: distance : posn posn -> number(define (distance here there) ; … here … a posn ; … there … a posn ; … (posn-x here) … a number ; … (posn-x there) … a number ; … (posn-y here) … a number ; … (posn-y there) … a number )"Examples of distance:"(distance (make-posn 4 5) (make-posn 4 5)) "should be 0"(distance (make-posn 4 5) (make-posn 1 5)) "should be 3"(distance (make-posn 4 5) (make-posn 4 17)) "should be 12"(distance (make-posn 4 5) (make-posn 7 9)) "should be 5"

Page 19: Section 6.1: Structures (continued)

Example 4: distance; Purpose: To find the distance between two points.; Contract: distance : posn posn -> number(define (distance here there) (sqrt (+ (sqr (- (posn-x here) (posn-x there))) (sqr (- (posn-y here) (posn-y there))))) )

"Examples of distance:"(distance (make-posn 4 5) (make-posn 4 5)) "should be 0"(distance (make-posn 4 5) (make-posn 1 5)) "should be 3"(distance (make-posn 4 5) (make-posn 4 17)) "should be 12"(distance (make-posn 4 5) (make-posn 7 9)) "should be 5"

Page 20: Section 6.1: Structures (continued)

Example 5: Function re-use

• Define a function that takes in two points and returns their distance plus 2.

Page 21: Section 6.1: Structures (continued)

Example 5: Function re-use• Define a function that takes in two points and

returns their distance plus 2.

• Solution:(define (add2-to-distance here there) (+ 2 (distance here there) ))

Page 22: Section 6.1: Structures (continued)

Example 6: Function re-use

• Define a function that takes in two points and returns their distance plus a variable “n”.

Page 23: Section 6.1: Structures (continued)

Example 6: Function re-use• Define a function that takes in two points and

returns their distance plus 2.

• Solution:(define (addn-to-distance here there n) (+ n (distance here there) ))

Page 24: Section 6.1: Structures (continued)

In summary…• You have seen how to work with a structure

that is defined.• It is possible to define your own structures,

but we will skip this due to time constraints.

Next time…• Summary of Computer Programming and

Scheme