2.1. Interacting with Scheme


2.1. Interacting with Scheme

Most Scheme systems provide an interactive programming environment that simplifies program development and experimentation. The simplest interaction with Scheme follows a "read-evaluate-print" cycle. A program (often called a read- evaluate-print loop, or REPL) reads each expression you type at the keyboard, evaluates it, and prints its value.

With an interactive Scheme system, you can type an expression at the keyboard and see its value immediately. You can define a procedure and apply it to arguments to see how it works. You can even type in an entire program consisting of a set of procedure definitions and test it without leaving the system. When your program starts getting longer, it will be more convenient to type it into a file (using a text editor), load the file (using load), and test it interactively. Preparing your program in a file has several advantages: you have a chance to compose your program more carefully, you can correct errors without retyping the program, and you can retain a copy for later use. Scheme treats expressions loaded from a file the same as expressions typed from the keyboard.

While Scheme provides various input and output procedures, the REPL takes care of reading expressions and printing their values. Furthermore, if you need to save the results for later use, you can make a transcript (using transcript-on and transcript-off; see Section 7.4) of an interactive session. This frees you to concentrate on writing your program without worrying about how its results will be displayed or saved.

The examples in this chapter and in the rest of the book follow a regular format. An expression you might type from your keyboard is given first, possibly spanning several lines. The value of the expression is given after the , to be read as "evaluates to." The is omitted when the value of the expression is unspecified, e.g., for variable definitions.

The example programs are formatted in a style that "looks nice" and conveys the structure of the program. The code is easy to read because the relationship between each expression and its subexpressions is clearly shown. Scheme ignores indentation and line breaks, however, so there is no need to follow a particular style. The important thing is to establish one style and keep to it. Scheme sees each program as if it were on a single line, with its subexpressions ordered from left to right.

If you have access to an interactive Scheme system, it might be a good idea to start it up now and type in the examples as you read. One of the simplest Scheme expressions is a string constant. Try typing "Hi Mom!" (including the double quotes) in response to the prompt. The system should respond with "Hi Mom!"; the value of any constant is the constant itself.

 "Hi Mom!"  "Hi Mom!" 

Here is a set of expressions, each with Scheme's response. They are explained in later sections of this chapter, but for now use them to practice interacting with Scheme.

 "hello"  "hello" 42  42 22/7  22/7 3.141592653  3.141592653 +  #<procedure> (+ 76 31)  107 '(a b c d)  (a b c d) 

Be careful not to miss any single quotes ( ' ), double quotes, or parentheses. If you left off a single quote in the last expression, you probably received an error message. Just try again. If you left off a closing parenthesis or double quote, the system may still be waiting for it.

Here are a few more expressions to try. You can try to figure out on your own what they mean or wait to find out later in the chapter.

 (car '(a b c))  a (cdr '(a b c))  (b c) (cons 'a '(b c))  (a b c) (cons (car '(a b c))       (cdr '(d e f)))  (a e f) 

As you can see, Scheme expressions may span more than one line. The Scheme system knows when it has an entire expression by matching double quotes and parentheses.

Next, let's try defining a procedure.

 (define square   (lambda (n)     (* n n))) 

The procedure square computes the square n2 of any number n. We say more about the expressions that make up this definition later in this chapter. For now it suffices to say that define establishes variable bindings, lambda creates procedures, and * names the multiplication procedure. Note the form of these expressions. All structured forms are enclosed in parentheses and written in prefix notation, i.e., the operator precedes the arguments. As you can see, this is true even for simple arithmetic operations such as *.

Try using square.

 (square 5)  25 (square -200)  40000 (square 0.5)  0.25 (square -1/2)  1/4 

Scheme systems that do not support exact ratios internally may print 0.25 for (square -1/2).

Even though the next definition is short, you might enter it into a file. Let's assume you call the file "reciprocal.ss."

 (define reciprocal   (lambda (n)     (if (= n 0)         "oops!"         (/ 1 n)))) 

This procedure, reciprocal, computes the quantity 1/n for any number n 0. For n = 0, reciprocal returns the string "oops!". Return to Scheme and try loading your file with the procedure load.

 (load "reciprocal.ss") 

Finally, try using the procedure we have just defined.

 (reciprocal 10)  1/10 (reciprocal 1/10)  10 (reciprocal 0)  "oops!" (reciprocal (reciprocal 1/10))  1/10 

In the next section we will discuss Scheme expressions in more detail. Throughout this chapter, keep in mind that your Scheme system is one of the most useful tools for learning Scheme. Whenever you try one of the examples in the text, follow it up with your own examples. In an interactive Scheme system, the cost of trying something out is relatively small|usually just the time to type it in.




The Scheme Programming Language
The Scheme Programming Language
ISBN: 026251298X
EAN: 2147483647
Year: 2003
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net