Laboratory Exercises For Computer Science 151

Symbols and Expressions

Symbols and Expressions

Summary: This laboratory exercise clarifies the concepts of symbols and expressions in Scheme, introduce Scheme's read-eval-print loop, provides experience with the Chez Scheme programming environment, describes the quote procedure, and begins a discussion of the list data structure.

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 one of two implementations of Scheme, called Chez Scheme and DrScheme.

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

LISP is especially used for work in Artificial Intelligence.

Steps for this Lab:

  1. Prepare to run the Chez Scheme programming environment, by following these steps:

  2. In Scheme, processing proceeds by typing expressions into the Scheme environment. After each expression is entered, Scheme 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 real 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. Consider these expressions which take advantage of our numbers being based on the decimal system (base 10).
    
        (modulo (truncate 12345.6789) 10)
        (modulo (truncate (/ 12345.6789 10)) 10)
    
    Explain why each of these results was obtained. Then, write an expression using modulo, truncate, and/or / to extract the thousands digit 2 from the number 12345.6789.

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

  10. Type these expressions into Scheme:
    
       (+ (* 2 2) 3)
       (quote (+ (* 2 2) 3))
       '(+ (* 2 2) 3)
       ('+ (* 2 2) 3)
       (+ '(* 2 2) 3)
    
    In each case, explain the result.

  11. Additional Practice: As you have time, experiment further with define and quote. For example, try to anticipate what happens with the following:
    
    (define x 2)
    (define y 3)
    (+ x y)
    (+ 'x 'y)
    ('+ x y)
    '(+ x y)
    
    Then try entering these lines into Scheme, and observe the results. Be sure you can explain what happens.

This document is available on the World Wide Web as

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

created January 13, 1997
last revised August 28, 2000