Symbols and Expressions

## Symbols and Expressions

Goals: The purposes of this laboratory exercise are to clarify the concepts of symbols and expressions in Scheme, to introduce Scheme's read-eval-print loop, and to gain experience with the Chez Scheme programming environment.

Notes on the History of Scheme: Lisp was invented in the late 1950's and early 1960's at M.I.T. by John McCarthy. Lisp is based in part on the Lambda Calculus, a mathematical formalism developed by Alonzo Church (1903-1995).

Lisp is the second-oldest language still in widespread use (the oldest is Fortran). Two dialects of Lisp are widely used today: Scheme and Common Lisp. Scheme is a small, uniform dialect that is good for teaching because of its simplicity. Common Lisp is a large, ``industrial-strength'' dialect that is standardized and is available in several commercial versions. In this course, we will use an implementation of Scheme called Chez Scheme .

Most programming languages require learning the syntax of many different kinds of statements. In contrast, Scheme (and Lisp) syntax is simple and uniform. Much of the work in learning Scheme is learning the names and effects of the system functions that form the core of the language. This course presents the system functions and covers other functions that are present in Common Lisp.

Lisp is especially used for work in Artificial Intelligence.

Steps for this Lab:

1. Prepare to run the Chez Scheme programming environment, following the steps you used in the previous lab.
• Log onto a workstation.
• Open an hpterm window.
• Run Chez Scheme by typing ` scheme ` in the hpterm window.

2. In Scheme, processing proceeds by typing expressions into the Scheme environment. After each expression is entered, Scheme
• reads the expression,
• evaluates the expression to determine its value, and
• prints the resulting value.
This read-eval-print cycle forms the basis for all processing within Scheme.

Numbers are expressions, whose value or meaning is the number itself.
Type several numbers into Chez Scheme, typing one number at a time. For example, at the Scheme prompt, type

```    7
-10
3.1415926535
```
In each case, note how Chez Scheme responds.

3. Accuracy: Enter a read number with many digits. How many digits are printed in the answer for integers? For reals?

4. Fractions: Determine what happens when you enter a fraction. For example, what value is returned when you enter:
```    3/5
-18/19
10/34
```
How many digits are printed in the answer for rational numbers?

5. Symbols are Scheme's analog of words; symbols are sequences of characters, such as
 hi TwoBeOrNot2Be This-is-a-symbol.
In Scheme, symbols may act as variable names, but they do not have a value until we give them one. Describe what happens when you type one of the above symbols into Scheme.

6. We may give a symbol a value using a define operation. For example, give the symbol pi the value 3.141592 as follows:
```     (define pi 3.141592)
```
Now enter pi into Scheme and determine what happens.

Check if work with symbols within Chez Scheme is case sensitive. What happens if you type Pi or PI.

Check whether Scheme allows you to redefine the value of a symbol. For example, following the 1897 Indiana House, try to declare that the value of pi is three:

```     (define pi 3)
```
Now what happens if you type the symbol pi?

Determine what happens if you make a typographical error. For example, try misspelling define or leaving out a left or right parentheses. In each case, make a note describing what happens.

7. Numeric Procedures: In Scheme, we apply operations to data using a prefix notation with parentheses. For example, the function f(x) would be written (f x). Also, all operation names are listed as the first component within the parentheses. Observe what happens when you type the following operations into Scheme:
```    (- 27 3)
(/ 17 2)
(/ 17 -2)
(/ 17 2.0)
(truncate (/ 17 4))
(modulo 17 3)
(sqrt 4)
(sin 0.5)
(sqrt -2)
(+ (* 3 2) (/ 8 4))
```

8. The Quote Procedure: Sometimes we want Scheme to print the symbol, not its value (e.g., pi, not 3.141592). This is done with the quote procedure. To try this out, type
```    (quote pi)
```
(quote pi) may be abbreviated 'pi . Try typing this at the keyboard as well.

9. Textbook Exercises: As you have time, work on exercises 1.2-1.5 from the textbook. [Skip exercise 1.1 for now, as we will consider it in the next lab.] For exercise 1.5, try various specific values for the variables given. For each exercise, be sure you can explain the results that you observe.

This document is available on the World Wide Web as

```http://www.math.grin.edu/~walker/courses/151/lab-symbols.html
```

created January 13, 1997
last revised January 15, 1997