Section 2

How to Design Programs: An Introduction to Programming and Computing

Info iconThis preview shows pages 1–3. 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
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: How to Design Programs: An Introduction to Computing and Programming [Go to first , previous , next page; contents ; index ] Section 2 Numbers, Expressions, Simple Programs In the beginning, people thought of computers as number crunchers. And indeed, computers are very good at working with numbers. Since teachers start their first-graders on computing with numbers, we start with numbers, too. Once we know how computers deal with numbers, we can develop simple programs in no time; we just translate common sense into our programming notation. Still, even developing such simple programs requires discipline, and so we introduce the outline of the most fundamental design recipe and the basic programming guideline at the end of this section. 2.1 Numbers and Arithmetic [../icons/plt.gif] Computing Numbers come in many different flavors: positive and negative integers, fractions (also known as rationals), and reals are the most widely known classes of numbers: 5-5 2/3 17/3 #i1.4142135623731 The first is an integer, the second one a negative integer, the next two are fractions, and the last one is an inexact representation of a real number. Like a pocket calculator, the simplest of computers, Scheme permits programmers to add, subtract, multiply, and divide numbers: (+ 5 5) (+ -5 5) (+ 5 -5) (- 5 5) (* 3 4) (/ 8 12) The first three ask Scheme to perform additions; the last three demand a subtraction, a multiplication, and a division. All arithmetic expressions are parenthesized and mention the operation first; the numbers follow the operation and are separated by spaces. [../icons/plt.gif] Stepper file:///C|/Documents%20and%20Settings/Linda%20Graue.../How%20to%20Design%20Programs/curriculum-Z-H-5.html (1 of 18) [2/5/2008 4:42:35 PM] How to Design Programs: An Introduction to Computing and Programming As in arithmetic or algebra, we can nest expressions: (* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2)) Scheme evaluates these expressions exactly as we do. It first reduces the innermost parenthesized expressions to numbers, then the next layer, and so on: (* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2)) = (* 4 (/ (* 8 3) 2)) = (* 4 (/ 24 2)) = (* 4 12) = 48 Because every Scheme expression has the shape (operation A ... B) there is never any question about which part has to be evaluated first. Whenever A ... B are numbers, the expression can be evaluated; otherwise, A ... B are evaluated first. Contrast this with [curriculum1a-Z-G-1.gif] which is an expression that we encounter in grade school. Only a substantial amount of practice guarantees that we remember to evaluate the multiplication first and the addition afterwards. 4 Finally, Scheme not only provides simple arithmetical operations but a whole range of advanced mathematical operations on numbers. Here are five examples: 1. (sqrt A) computes ( A ) 1/2 ; 2. (expt A B) computes A B ; 3. (remainder A B) computes the remainder of the integer division A / B ; 4. (log A) computes the natural logarithm of A ; and 5. (sin A) computes the sine of...
View Full Document

This test prep was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Spring '07 term at WPI.

Page1 / 18

Section 2 - How to Design Programs: An Introduction to...

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

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