Module 4

# Module 4 - Structures and Lists Readings HtDP sections 6 7...

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 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 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 make-posn , with contract ;; make-posn: num num → posn We extract the values from a posn with the two selector functions posn-x and posn-y , each of which consumes a posn and produces a number. CS 135 Fall 2008 04: Structures and Lists 3 Example: ( define mypoint ( make-posn 8 1 )) ( posn-x mypoint ) ⇒ 8 ( posn-y 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 ( make-posn 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 don’t need to know about this. If we evaluate an expression which produces a posn , it will be displayed as an expression like ( make-posn 8 1 ) , which is considered a value. CS 135 Fall 2008 04: Structures and Lists 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 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 (- ( posn-x posn1 ) ( posn-x posn2 ))) ( sqr (- ( posn-y posn1 ) ( posn-y posn2 )))))) ( define first-point ( make-posn 1 1 )) ( define second-point ( make-posn 4 5 )) ( distance first-point second-point ); ⇒ 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 (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 Fall 2008 04: Structures and Lists 8 Misusing posns What is the result of evaluating the following expression?...
View Full Document

{[ snackBarMessage ]}

### Page1 / 32

Module 4 - Structures and Lists Readings HtDP sections 6 7...

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

View Full Document
Ask a homework question - tutors are online