Tuesday, July 5, 2011

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.

Numbers and Arithmetic


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.
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

No comments:

Post a Comment

This is Good Computer Information Blog.they will give
best information about computer.