CSC 270 - Survey of Programming Languages: Scheme (Racket)

Processing Simple Forms of Data

A few preliminary definitions

Numbers and Arithmetic

Computers were originally used as number crunchers (to perform complex, large-scale calculations). We now use them for many other purposes as well.

Numbers - can be positive or negative, rational (or fractions) or approximations of real values.

E.g., 5 -5 17/3 2/3 #i1.4142135623731

Scheme allows us to do arithmetic with numbers
(+5 5) (+ -5 5) (+ 5 -5) (- 5 5) (*3 4) (/8 12)

Simple Expressions

We can nest simple expressions in other expressions:

e.g.,
(+ 4 (* 2 7)) = (+ 4 14) = 18
(* 12 (+ 40 (*1.5 (- 46 40 )))) = (* 12 (+ 40 (* 1.5 6)))
= (* 12 (+ 40 9)) = (* 12 49) = 588

(* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2))
= (* 4 (/ (* 8 3) 2))
= (* 4 (/ 24 2))
= (* 4 12)
= 48

Variables and Programs

A variable is a placeholder in an expression that stands for some fixed unknown quantity, .e.g, 3.14 * r * r

r can be used to represent any positive number. If we find a value for it, (such as 5), we can evalueate the expression:

3.14* 52 = 3.15*25 = 78.5

Such expression state a rule that shows us how to calculate a certain value. A program is an example of such a rule.

Scheme Programs

We could write this as a Scheme program:

(define (area-of-disk r)
	(* 3.14 (* r r)))

This defines the funciton area-of-disk. We can now use (area-of-disk 5) to calculate a disk with a radius of 5:

(area-of-disk 5)
	=	(* 3.14 (* 5 5))
	=	(* 3.14 25)
	= 78.5 

Designing a new program

If we wanted to calculate the area of a ring, we would need the outer and inner radii and the ring's area would be:

(define (area-of-ring outer-radius inner-radius)
	(- (area-of-disk outer-radius)
	   (area-of-disk inner-radius)))

(area-of-ring 5 3)
	= (- (area-of-disk 5) (area-of-disk 3))
	= (- (* 3.14 (* 5 5)) (* 3.14 (* 3 3)))
	= (- (* 3.14 25) (* 3.14 9))
	= .....

Errors

There are three main types of errors that can be made in a program (whether in Scheme or another language):

Word Problems

Programmers are usually given word problems and not mathematical expressions to solve. Sometimes the description is ambiguous and may contain irrelevant information. The first job is to extract the important information from the description.

Company XYZ & Co. pays all its employees $12 per hours. An employee may work between 20 and 65 hours per week. Develop a program to determine an employee's gross pay.

The last sentence tells us that the goal is to find the gross pay.

We know that gross = Rate * Hours or 12*h in this case.

Our Scheme program becomes:

	(define	(wage h)
		(* 12 h))

The 20 to 65 hour work week seems irrevelant. We will discover late how to make use of it.

Composing Programs

When we wrote ring-area, we used our definiton of disk-area which we had already written.

We could have written:

(define (ring-area outer inner)
	( - (* 3.14 (* outer outer))
	( - (* 3.14 (* inner inner)))

instead of

(define (ring-area outer inner)
	( - (disk-area outer)
	( - (disk-area inner))

Does this form offer an advantage?

Another example

Imagine a movie owner who has complete freedom to set ticket prices. At $5.00 a ticket, 120 attend a performance.Every time he drops the price by 10 cents, attendance increases by 15.

Every additional attendee cost more that he has to pay to the movie distributor. He pays $1.60 per attendee for an attendance 120, which increases (or decreases) by 4 cents per person) as attendances goes up (or down).

	Profit = Revenue - Cost
	Revenue = ticket price * attendance
	Cost = 1.6*Attendees + (.04*(Attendees-120)
	Attendance = (15/.10) * (5.00 - p) + 120

At what ticket price does he maximize his profit?

Our program becomes

(define (profit price)
	(- (revenue (attendance price) price)
		(cost (attendance price))))
(define (revenue (attendees price)
	(* attendees price))
(define (attendance price)
	(+ (* (/15 .10) (- 5.00 price))
		120))
(define (cost attendees)
	(+ (* attendees 1.6)
	(* 0.04 (- attendees 120))))

Compare this to:

	(define (profit price)
	(- (*(+(/ 15 .10)
		(- 5.00 price)) 120)  price)
	(+ (* (+ (* (/ 15 .10)
		(- 5.00 price)) 120)  price)  1.6)
	(* 0.04 (- (+ (* (/ 15 .10)
		-5.00 price)) 120)	120)))))

Which is clearer?

Guideline: Formulate auxiliary "programs" if a program's definition requires a large expression.

Variable definitions

When a number appears repeatedly, it makes enormous sense to give a name and use it by name. Pi is an obvious example

(define PI 3.14) or
(define PI 3.14159265)
depending on how much accuracy we need.

Designing programs

Developing programs requires several steps:

Understanding the program's purpose

Program Body

In Scheme, the program body is a definition of the expression that we will compute.

Testing

Future Reuse

[Back to the Notes Index]