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 and Lists Readings: HtDP, sections 6, 7, 9, 10. • Avoid 6.2, 6.6, 6.7, 7.4 and 10.3. • These use the obsolete draw.ss teachpack. • The new image.ss and world.ss are more functional. CS 135 Fall 2009 04: Structures and Lists 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 Fall 2009 04: Structures and Lists 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 Fall 2009 04: Structures and Lists 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 window • a geographical position CS 135 Fall 2009 04: Structures and Lists 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 Fall 2009 04: Structures and Lists 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 Fall 2009 04: Structures and Lists 6 ;; distance: posn posn → num ;; computes the Euclidean distance between posn1 and posn2 ( define ( distance posn1 posn2 ) ( sqrt ( + ( sqr ( ( posnx posn1 ) ( posnx posn2 ))) ( sqr ( ( posny posn1 ) ( posny posn2 )))))) ( define firstpoint ( makeposn 1 1 )) ( define secondpoint ( makeposn 4 5 )) ( distance firstpoint secondpoint ) ⇒ 5 CS 135 Fall 2009 04: Structures and Lists 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 Fall 2009 04: Structures and Lists 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 Fall 2009 04: Structures and Lists 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.)....
View
Full
Document
This note was uploaded on 02/12/2010 for the course CS 135 taught by Professor Vasiga during the Fall '07 term at Waterloo.
 Fall '07
 VASIGA

Click to edit the document details