{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04-structures-post3up

# 04-structures-post3up - Structures Readings HtDP sections 6...

This preview shows pages 1–5. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Structures Readings: HtDP, sections 6, 7. • Avoid 6.2, 6.6, 6.7, 7.4. • These use the obsolete draw.ss teachpack. • The new image.ss and world.ss are more functional. CS 135 Winter 2012 04: Structures 1 Compound data The teaching languages provide a general mechanism called structures . They permit the “bundling” of several values into one. In many situations, data is naturally grouped, and most programming languages provide some mechanism to do this. There is also one predefined structure, posn , to provide an example. CS 135 Winter 2012 04: Structures 2 Posn structures • constructor function make-posn , with contract ;; make-posn: Num Num → Posn • selector functions posn-x and posn-y , with contracts ;; posn-x: Posn → Num ;; posn-y: Posn → Num CS 135 Winter 2012 04: Structures 3 Example: ( define mypoint ( make-posn 8 1 )) ( posn-x mypoint ) ⇒ 8 ( posn-y mypoint ) ⇒ 1 Possible uses: • coordinates of a point on a two-dimensional plane • positions on a screen or in a window • a geographical position CS 135 Winter 2012 04: Structures 4 An expression such as ( make-posn 8 1 ) is considered a value. This expression will not be rewritten by the Stepper or our semantic rules. The expression ( make-posn ( + 4 4 ) (- 3 2 )) would be rewritten to (eventually) yield ( make-posn 8 1 ) . CS 135 Winter 2012 04: Structures 5 Example: point-to-point distance ( x 1 , y 1 ) ( x 2 , y 2 ) distance = q ( x 2- x 1 ) 2 + ( y 2- y 1 ) 2 CS 135 Winter 2012 04: Structures 6 ;; distance: Posn Posn → Num ;; computes the Euclidean distance between posn1 and posn2 ;; Example: ( check-expect ( distance ( make-posn 1 1 ) ( make-posn 4 5 )) 5 ) ( define ( distance posn1 posn2 ) ( sqrt ( + ( sqr (- ( posn-x posn2 ) ( posn-x posn1 ))) ( sqr (- ( posn-y posn2 ) ( posn-y posn1 )))))) CS 135 Winter 2012 04: Structures 7 Functions which produce posns ;; scale: Posn Num → Posn ;; scales the point by the given factor ;; Example: ( check-expect ( scale ( make-posn 3 4 ) 0.5 ) ( make-posn 1.5 2 )) ( define ( scale point factor ) ( make-posn ( * factor ( posn-x point )) ( * factor ( posn-y point )))) CS 135 Winter 2012 04: Structures 8 Misusing posns What is the result of evaluating the following expression? ( distance ( make-posn ’ Hannah ’ Montana ) ( make-posn ’ Miley ’ Cyrus )) This causes a run-time error, but at a surprising point. CS 135 Winter 2012 04: Structures 9 Scheme does not enforce contracts, which are just comments, and ignored by the machine. Each value created during the running of a program has a type (integer, Boolean, etc.). Types are associated with values, not with variables or parameters. ( define p 5 ) ( define q ( mystery-fn 5 )) CS 135 Winter 2012 04: Structures 10 This is known as dynamic typing ....
View Full Document

{[ snackBarMessage ]}

### Page1 / 13

04-structures-post3up - Structures Readings HtDP sections 6...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online