Section 6.1: Structures (continued)
description
Transcript of Section 6.1: Structures (continued)
Section 6.1: StructuresSection 6.1: Structures(continued)(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
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
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
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
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.
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
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
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
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
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))
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…
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…
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…
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…
Example 4: distance
• Takes two points and finds the distance between them.
• Hint: the formula is (sqrt((x1-x2)2 + (y1-y2)2))
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"
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"
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"
Example 5: Function re-use
• Define a function that takes in two points and returns their distance plus 2.
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) ))
Example 6: Function re-use
• Define a function that takes in two points and returns their distance plus a variable “n”.
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) ))
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