# How to Design Programs: An Introduction to Programming and Computing

• Test Prep
• davidvictor
• 24

This preview shows pages 1–3. Sign up to view the full content.

How to Design Programs: An Introduction to Computing and Programming [Go to first , previous , next page; contents ; index ] Section 6 Compound Data, Part 1: Structures The input of a function is seldom a single measurement (number), a single switch position (boolean), or a single name (symbol). Instead, it is almost always a piece of data that represents an object with many properties. Each property is a piece of information. For example, a function may consume a record about a CD; the relevant information might include the artist's name, the CD title, and the price. Similarly, if we are to model the movement of an object across a plane with a function, we must represent the position of the object in the plane, its speed in each direction, and possibly its color. In both cases, we refer to several pieces of information as if they were one: one record and one point. In short, we COMPOUND several pieces of data into a single piece of data. Scheme provides many different methods for compounding data. In this section, we deal with structures . A structure combines a fixed number of values into a single piece of data. In section 9 , we will encounter a method for combining an arbitrarily large number of values into a single piece of data. 6.1 Structures [../icons/plt.gif] Structures Suppose we wish to represent the pixels (colored dots) on our computer monitors. A pixel is very much like a Cartesian point. It has an x coordinate, which tells us where the pixel is in the horizontal direction, and it has a y coordinate, which tells us where the pixel is located in the downwards vertical direction. Given the two numbers, we can locate a pixel on the monitor, and so can a computer program. DrScheme's teachpacks represent pixels with posn structures. A posn structure combines two numbers. That is, a posn is a single value that contains two values. We can create a posn structure with the operation make-posn , which consumes two numbers and makes a posn . For example, (make-posn 3 4) (make-posn 8 6) (make-posn 5 12) are posn structures. Each of these structures has the same status as a number as far as computations are concerned. Both primitive operations and functions can consume and produce structures. Now consider a function that computes how far some pixel is from the origin. The contract, header, and purpose statement are easy to formulate: (1 of 24) [2/5/2008 4:43:57 PM]

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