The Grinnell Scheme Web: A first
program in Scheme

I need an example. What does a simple program in Scheme look like?

Here's one that consists of a single command:

(sqrt 23)
What does it do?

It directs Scheme to compute the square root of 23. Start Scheme in interactive mode, type in that command, and watch what happens:

> (sqrt 23)
4.79583152331272
In interactive mode, when you ask Scheme to compute the value of an arithmetic expression, it writes out the computed value before printing the next prompt. (In the implementation of Scheme that I use, the prompt is a greater-than sign followed by a space -- you can see it at the beginning of the interaction shown above. I've used this font for what the programmer types and this one for what Scheme displays so that you can follow the exchange more easily.)

In fact, one can use Scheme as a desk calculator. If you type in a sequence of arithmetic expressions, Scheme will simply print out the value of each one:

> (sqrt 39)
6.2449979983984
> (cos 0.7853981634)
0.707106781184743
> (log 14.7)
2.68784749378469
Scheme's logarithms are natural (base e) rather than common (base 10).


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/first-program.html


created June 20, 1995
last revised December 27, 1995

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