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 DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full 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 makeposn , with contract ;; makeposn: Num Num Posn selector functions posnx and posny , with contracts ;; posnx: Posn Num ;; posny: Posn Num CS 135 Winter 2012 04: Structures 3 Example: ( define mypoint ( makeposn 8 1 )) ( posnx mypoint ) 8 ( posny mypoint ) 1 Possible uses: coordinates of a point on a twodimensional plane positions on a screen or in a window a geographical position CS 135 Winter 2012 04: Structures 4 An expression such as ( makeposn 8 1 ) is considered a value. This expression will not be rewritten by the Stepper or our semantic rules. The expression ( makeposn ( + 4 4 ) ( 3 2 )) would be rewritten to (eventually) yield ( makeposn 8 1 ) . CS 135 Winter 2012 04: Structures 5 Example: pointtopoint 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: ( checkexpect ( distance ( makeposn 1 1 ) ( makeposn 4 5 )) 5 ) ( define ( distance posn1 posn2 ) ( sqrt ( + ( sqr ( ( posnx posn2 ) ( posnx posn1 ))) ( sqr ( ( posny posn2 ) ( posny posn1 )))))) CS 135 Winter 2012 04: Structures 7 Functions which produce posns ;; scale: Posn Num Posn ;; scales the point by the given factor ;; Example: ( checkexpect ( scale ( makeposn 3 4 ) 0.5 ) ( makeposn 1.5 2 )) ( define ( scale point factor ) ( makeposn ( * factor ( posnx point )) ( * factor ( posny point )))) CS 135 Winter 2012 04: Structures 8 Misusing posns What is the result of evaluating the following expression? ( distance ( makeposn Hannah Montana ) ( makeposn Miley Cyrus )) This causes a runtime 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 ( mysteryfn 5 )) CS 135 Winter 2012 04: Structures 10 This is known as dynamic typing ....
View
Full
Document
This note was uploaded on 04/02/2012 for the course CS 135 taught by Professor Vasiga during the Winter '07 term at Waterloo.
 Winter '07
 VASIGA

Click to edit the document details