The Grinnell Scheme Web: Integers

Computer programs do a lot of arithmetic, don't they? How can I use Scheme to program calculations?

Some computer programs spend almost all of the time it takes to run them doing arithmetic, but others are mainly concerned with text or graphics. Almost all programs contain a little arithmetic (although, as you may have noticed, the ``Hello, world!'' program had none).

Of course, I'm describing the programs from the point of view of the programmer. What she thinks of as a simple operation on text, such as displaying a character string, may actually involve some arithmetic behind the scenes -- for instance, the computer may have to compute the number of characters to be printed and compare it to the width of the window in which the string is to be displayed, and so on.

Most Scheme implementations provide simple ways to talk about numbers of various kinds -- integers (that is, whole numbers), rational numbers (those that can be expressed as fractions in which both the numerator and the denominator are integers), and even real numbers and complex numbers. Let's start with integers, since they're the easiest.

Okay. How do you do arithmetic with integers in Scheme?

All of the common arithmetic operations, such as addition, multiplication, exponentiation (raising a number to a power), and so on, are represented in Scheme by procedures. You can think of a procedure as a small, special-purpose calculator: It can only do one thing, but it can do it to any numbers you give it. The kind of command that gives a procedure some values to operate on and receives in return the result of the computation is called a procedure call or procedure invocation.

Here is a call to Scheme's addition procedure, giving it the integers 2957 and 61438. The addition procedure will find and return the sum of these two numbers. If you type this command at Scheme (running in interactive mode), Scheme will display the sum.

(+ 2957 61438)
Are you saying that this command tells the computer to compute 2957 + 61438? Why is the plus sign at the front instead of between the two numbers?

It's one of the syntactic rules of Scheme: Whenever you want to call a procedure, the name of the procedure precedes the values you want it to operate on. It's just a different notation -- the people who designed Scheme decided to use a different arrangement from the one you learned in first grade.

Why? Wouldn't it make more sense to use the notation that everyone is already familiar with?

It would make things easier for beginners. But the traditional notation is actually rather complicated, as you may remember from high-school algebra, and it also has some ambiguities in it that human beings can learn to resolve contextually, but that cause problems for computers. And the Scheme notation has some advantages of its own. For instance, the addition procedure can add together as many numbers as you like; you can write

(+ 3 14 159 2653 58979)
instead of 3 + 14 + 159 + 2653 + 58979. Always putting the procedure before all of the operands (the values on which the procedure operates) makes it easier to generalize the procedures in such ways.

So what procedures does Scheme provide for working with integers?

You'll want to take some time to explore the complete list. Also, you should look briefly at the ways in which Scheme's names for integers are constructed (mostly you use the familiar numerals, but there are a couple of extra possibilities that you may want to learn about).


Next topic
Previous topic
Table of contents


This document is available on the World Wide Web as

http://www.math.grin.edu/~stone/scheme-web/integer-numerals.html


created June 22, 1995
last revised December 29, 1995

Copyright 1995 by John David Stone (stone@math.grin.edu)