04-structlist-post3up-1

04-structlist-post3up-1 - Structures and Lists Readings:...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
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. • 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 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 Fall 2009 04: Structures and Lists 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 window • a geographical position CS 135 Fall 2009 04: Structures and Lists 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 Fall 2009 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 2009 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 2009 04: Structures and Lists 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 Fall 2009 04: Structures and Lists 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 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.

Page1 / 33

04-structlist-post3up-1 - Structures and Lists Readings:...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online