[previous] [up] [next]     [contents]
Next: Variables and Programs Up: NumbersExpressions, Simple Programs Previous: NumbersExpressions, Simple Programs

Numbers and Arithmetic

To type Scheme numbers into DrScheme, first click in DrScheme's bottom area, the interactions window, to set the keyboard focus to a point after the ``> '' prompt.

Beware! The insertion caret (the focus point for typing) must be placed after the space following the last ``>'' in the window; otherwise, DrScheme won't let you type a new expression. You can still select old expressions and results for copying, but they cannot be modified.
After typing a Scheme expression, type ENTER (a.k.a. RETURN). If you type a number, DrScheme echoes the number back. If you type a more complex expression, DrScheme prints the expression's value. For example, evaluating (+ 5 5) in DrScheme looks like this:

Numbers

Scheme numbers are printed in the standard way, but spaces are not allowed within a number. Thus, -5, with no space between the - and 5, is the negative integer -5. But - 5, with a space in between, is two separate expressions: the minus operator and the number 5. Similarly, 3/2 is the fraction 3/2, while 3 / 2 is three separate expressions: the number 3, the division operator, and the number 2.

There is no limit on the size of exact numbers in Scheme. Try giving DrScheme the number 123456789012345678901234567890.

Try the number 10/12. DrScheme echoes back 5/6, automatically putting the fraction in its simplest form.

DrScheme also accepts decimal numbers, such as 3.2, and treats them in the same way as fractions. Fractions with a precise decimal format are printed in decimal. For example, 1/2 prints as 0.5.

Numbers with a leading #i are treated as inexact numbers. For example, the input #i2 means ``roughly 2'', and prints as #i2.0. DrScheme always prints inexact results with a leading #i to alert you that the number is inexact. For example, an abbreviation for pi (the ratio between a circle's circumference and diameter) is built into DrScheme, so the input pi produces the output #i3.14159 (perhaps with more digits). This result means ``roughly 3.14159''. Computations with inexact inputs produce inexact results. Sometimes, computations with exact inputs also produce inexact output, such as when computing a square root.

For the Especially Curious: Inexact numbers have a limited size as well as a limited precision. Thus, (expt #i2 #i5000) calculates the inexact value of tex2html_wrap_inline3255 to be #i+inf.0, which means ``roughly infinity''--very inexact, indeed!

Numeric Expressions

Every numeric expression that is not a number must start with an open parenthesis ( and end with a close parenthesis ). If, instead of (+ 1 2), you type + 1 2 or 1 + 2, DrScheme echos back each of the three separate pieces: 1, +, and 2.

Beware! If you type an open parenthesis without a matching close parenthesis (or vice versa), DrScheme does not even try to evaluate the malformed expression; instead, typing ENTER creates a new line for typing.
To help you match parentheses, DrScheme highlights parenthesis-balanced expressions with a gray background when the insertion caret is immediately before an open parenthesis or after a close parenthesis. However, this highlighting is only an indicator. The highlight does not actually select any text; in particular, the copy and cut commands do not apply to the highlighted region. The normal caret, which is used to select text for cut and paste poerations, is still visible as a blinking black line on the end of the highlighted region.

If a numeric expression starts with an open parenthesis, an operator must follow the parenthesis. Unlike arithmetic, where extra parentheses are always allowed, extra parentheses are illegal in a Scheme expression. Thus, (1) is not a numeric expression, neither is (+ (1) 2), and neither is ((+ 1 2)). If you give DrScheme the expression (1), it replies with the error ``First term after parenthesis is illegal in an application.'' In other words, since 1 appears after the parenthesis, DrScheme thinks you want to apply the ``operator'' 1 to some arguments. But 1 is not an operator, so it is illegal for 1 to follow an open parenthesis. DrScheme gives the same error message for ((+ 1 2)) because (+ 1 2) is also not an operator.

When typing a compound expression, you may accidentally omit a set of internal parentheses. In this case, DrScheme may report an error that a primitive operation was received when a number was expected. For example:

> (* (+ 2 - 4 5) 43 5)
+: expects type <number> as 2nd argument; given -; other 
arguments were: 2 4 5 

When an error occurs, DrScheme highlights the erroneous expression in pink. In the above example, (+ 2 - 4 5) is highlighted, pinpointing the specific part of the expression that contained the error: parentheses are missing around the sub-expression - 4 5.

An expression like (+ 1 2 3) does not signal an error because the standard arithmetic operations accept more than two arguments. (+ 1 2 3) is legal Scheme notation for 1 + 2 + 3, (- 1 2 3) means (1 - 2) - 3, (* 1 2 3) means 1 * 2 * 3, and (/ 1 2 3) means (1 / 2) / 3.


[previous] [up] [next]     [contents]
Next: Variables and Programs Up: NumbersExpressions, Simple Programs Previous: NumbersExpressions, Simple Programs

PLT