1 / 24

Section 6.1: Structures (continued)

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

Download Presentation

Section 6.1: Structures (continued)

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. Section 6.1: Structures (continued)

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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.

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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))

  12. 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…

  13. 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…

  14. 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…

  15. 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…

  16. Example 4: distance • Takes two points and finds the distance between them. • Hint: the formula is (sqrt((x1-x2)2 + (y1-y2)2))

  17. 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"

  18. 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"

  19. 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"

  20. Example 5: Function re-use • Define a function that takes in two points and returns their distance plus 2.

  21. 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) ) )

  22. Example 6: Function re-use • Define a function that takes in two points and returns their distance plus a variable “n”.

  23. 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) ) )

  24. 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

More Related