This preview shows pages 1–4. 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. Section 6.2 introduces drawings using the draw.ss teachpack, also used in later examples. This teachpack has been supplanted by image.ss and world.ss in recent releases of DrScheme. Examples using draw.ss are optional reading. CS 135 Fall 2008 04: Structures and Lists 1 Compound data We would like our functions to consume and produce multiple values, rather than just singletons. In many situations, data is naturally grouped, and most programming languages provide some mechanism to do this. The teaching languages provide a general mechanism called structures . There is also one predefined structure, posn , to provide an example. CS 135 Fall 2008 04: Structures and Lists 2 Posn structures A posn structure contains two numbers intended to represent x and y coordinates. It is created by the constructor function makeposn , with contract ;; makeposn: num num posn We extract the values from a posn with the two selector functions posnx and posny , each of which consumes a posn and produces a number. CS 135 Fall 2008 04: Structures and Lists 3 Example: ( define mypoint ( makeposn 8 1 )) ( posnx mypoint ) 8 ( posny mypoint ) 1 posn structures are primarily used in the text to represent positions on a screen window (using the draw.ss teachpack), but they can also be used to represent a geographical position, or a point on an architectural drawing or blueprint. CS 135 Fall 2008 04: Structures and Lists 4 What is the result of the application ( makeposn 8 1 ) ? We will represent this result by the expression itself that is, this expression cannot be simplified. DrScheme has some internal representation of posn s, but we dont need to know about this. If we evaluate an expression which produces a posn , it will be displayed as an expression like ( makeposn 8 1 ) , which is considered a value. CS 135 Fall 2008 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 2008 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 2008 04: Structures and Lists 7 Functions which produce posns ;; scale: posn num posn ;; scales the point by the given factor ;; Example: (scale (makeposn 3 4) 0.5) (makeposn 1.5 2) ( define ( scale point factor ) ( makeposn ( * factor ( posnx point )) ( * factor ( posny point ))) CS 135 Fall 2008 04: Structures and Lists 8 Misusing posns What is the result of evaluating the following expression?...
View Full
Document
 Fall '07
 VASIGA

Click to edit the document details