▼ |  | Guide: PLT Scheme

1 | Welcome to PLT Scheme
2 | Scheme Essentials
3 | Built-In Datatypes
4 | Expressions and Definitions
5 | Programmer-Defined Datatypes
6 | Modules
7 | Contracts
8 | Input and Output
9 | Regular Expressions
10 | Exceptions and Control
11 | Iterations and Comprehensions
12 | Pattern Matching
13 | Classes and Objects
14 | Units (Components)
15 | Reflection and Dynamic Evaluation
16 | Macros
17 | Performance
18 | Running and Creating Executables
19 | Compilation and Configuration
20 | More Libraries
21 | Dialects of Scheme
| Bibliography
| Index

► | 1 | Welcome to PLT Scheme

1.1 | Interacting with Scheme
1.2 | Definitions and Interactions
1.3 | Creating Executables
1.4 | A Note to Readers with Scheme/ Lisp Experience

On this page:

1.1`` Interacting with Scheme
1.2`` Definitions and Interactions
1.3`` Creating Executables
1.4`` A Note to Readers with Scheme/ Lisp Experience

top ← prev up next →

### 1`` Welcome to PLT Scheme

Depending on how you look at it, PLT Scheme is

- a programming language – a descendant of Scheme, which is a dialect of Lisp;

  > > > See Dialects of Scheme for more information on other dialects of Scheme and how they relate to PLT Scheme.

- a family of programming languages – variants of Scheme, and more; or

- a set of tools – for using a family of programming languages.

Where there is no room for confusion, we use simply Scheme to refer to any of these facets of PLT Scheme.

PLT Scheme’s two main tools are

- MzScheme, the core compiler, interpreter, and run-time system; and

- DrScheme, the programming environment (which runs on top of MzScheme).

Most likely, you’ll want to explore PLT Scheme using DrScheme, especially at the beginning. If you prefer, you can also work with the command-line mzscheme interpreter and your favorite text editor. The rest of this guide presents the language mostly independent of your choice of editor.

If you’re using DrScheme, you’ll need to choose the proper language, because DrScheme accommodates many different variants of Scheme. Assuming that you’ve never used DrScheme before, start it up, type the line

#lang scheme

in DrScheme’s top text area, and then click the Run button that’s above the text area. DrScheme then understands that you mean to work in the normal variant of Scheme (as opposed to the smaller scheme/base, or many other possibilities).

> > > More PLT Schemes describes some of the other possibilities.

If you’ve used DrScheme before with something other than a program that starts #lang, DrScheme will remember the last language that you used, instead of inferring the language from the #lang line. In that case, use the Language|Choose Language... menu item. In the dialog that appears, select the first item, which is Module. Put the #lang line above in the top text area, still.

#### 1.1`` Interacting with Scheme

DrScheme’s bottom text area and the mzscheme command-line program (when started with no options) both act as a kind of calculator. You type a Scheme expression, hit return, and the answer is printed. In the terminology of Scheme, this kind of calculator is called a read-eval-print loop or REPL.

A number by itself is an expression, and the answer is just the number:

> 5
5

A string is also an expression that evaluates to itself. A string is written with double quotes at the start and end of the string:

> "Hello, world!"
"Hello, world!"

Scheme uses parentheses to wrap larger expressions – almost any kind of expression, other than simple constants. For example, a function call is written: open parenthesis, function name, argument expression, and closing parenthesis. The following expression calls the built-in function substring with the arguments "the boy out of the country", 4, and 7:

> ( substring "the boy out of the country" 4 7)
"boy"

#### 1.2`` Definitions and Interactions

You can define your own functions that work like substring by using the define form, like this:

( define (extract str)
( substring str 4 7))

> (extract "the boy out of the country")
"boy"
> (extract "the country out of the boy")
"cou"

Although you can evaluate the define form in the REPL, definitions are normally a part of a program that you want to keep and use later. So, in DrScheme, you’d normally put the definition in the top text area – called the definitions area – along with the #lang prefix:

#lang scheme
( define (extract str)
( substring str 4 7))

If calling (extract "the boy") is part of the main action of your program, that would go in the definitions area, too. But if it was just an example expression that you were using to explore extract, then you’d more likely leave the definitions area as above, click Run, and then evaluate (extract "the boy") in the REPL.

With mzscheme, you’d save the above text in a file using your favorite editor. If you save it as "extract.ss", then after starting mzscheme in the same directory, you’d evaluate the following sequence:

> ( enter! "extract.ss")
> (extract "the gal out of the city")
"gal"

The enter! form both loads the code and switches the evaluation context to the inside of the module, just like DrScheme’s Run button.

#### 1.3`` Creating Executables

If your file (or definitions area in DrScheme) contains

#lang scheme
( define (extract str)
( substring str 4 7))
(extract "the cat out of the bag")

then it is a complete program that prints “cat” when run. To package this program as an executable, choose one of the following options:

- In DrScheme, you can select the Scheme|Create Executable... menu item.

- From a command-line prompt, run mzc --exe ‹dest-filename› ‹src-filename›, where ‹src-filename› contains the program. See Stand-Alone Executables from Scheme Code for more information.

- With Unix or Mac OS X, you can turn the program file into an executable script by inserting the line

  > > > See Unix Scripts for more information on script files.

  #! /usr/bin/env mzscheme

  at the very beginning of the file. Also, change the file permissions to executable using chmod +x ‹filename› on the command line.

  The script works as long as mzscheme is in the user’s executable search path. Alternately, use a full path to mzscheme after #! (with a space between #! and the path), in which case the user’s executable search path does not matter.

#### 1.4`` A Note to Readers with Scheme/Lisp Experience

If you already know something about Scheme or Lisp, you might be tempted to put just

( define (extract str)
( substring str 4 7))

into "extract.scm" and run mzscheme with

> ( load "extract.scm")
> (extract "the dog out")
"dog"

That will work, because mzscheme is willing to imitate a traditional Scheme environment, but we strongly recommend against using load or writing programs outside of a module.

Writing definitions outside of a module leads to bad error messages, bad performance, and awkward scripting to combine and run programs. The problems are not specific to mzscheme; they’re fundamental limitations of the traditional top-level environment, which Scheme and Lisp implementations have historically fought with ad hoc command-line flags, compiler directives, and build tools. The module system is to designed to avoid the problems, so start with #lang, and you’ll be happier with PLT Scheme in the long run.

top ← prev up next →