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)
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
(+3 (* 4 5)) or 3 + 4*5
(sqrt A) - square root of A (expt A B) - AB (remainder A B) - Remainder from A/B (log A) - natural logarithm of A (sin A) - sine of A radians ( 180 degrees = PI radians)
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.
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
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)) = .....
There are three main types of errors that can be made in a program (whether in Scheme or another language):
(define (P x) (+ (x) 10)) (define (P x) x 10) defin (P x) (+ x 10))
e.g., (/ 1 0)
(define (average2 x y) (* 2 (+ x y)))
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.
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?
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.
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.
Developing programs requires several steps:
;; ring-area : number number --> number
(define (ring-area outer inner) ....)
;; ring-area number number --> number ;; to compute the ara of a ring whose radius is outer ;; and whose hole has a radius of inner ;; example (ring-area 5 3) = 50.24 define (ring-area outer inner)
In Scheme, the program body is a definition of the expression that we will compute.
(define (ring-area outer inner) (- (disk-area outer) (disk-area inner)))