Main Page | See live article | Alphabetical index

Scheme programming language

The Scheme programming language is a functional programming language and a dialect of Lisp. It was developed by Guy L. Steele and Gerald Jay Sussman in the 1970s and introduced to the academic world via a series of papers now referred to as Sussman and Steele's Lambda Papers.

Scheme's philosophy is unashamedly minimalist. Its goal is not to pile feature upon feature, but to remove weaknesses and restrictions that make new features appear necessary. Therefore, Scheme provides as few primitive notions as possible, and lets everything else be implemented on top of them. For example, the main mechanism for governing control flow is tail recursion.

Scheme was the first variety of Lisp to use lexical variable scoping (as opposed to dynamic variable scoping) exclusively. Scheme supports garbage collection of unreferenced data. It uses lists as the primary data structure, but also has good support for arrays. Owing to the minimalist specification, there is no standard syntax for creating structures with named fields, or for doing object oriented programming, but many individual implementations have such features.

Scheme was originally called "Schemer", in the tradition of the languages Planner and Conniver. The current name resulted from the authors' use of the ITS operating system, which limited filenames to 6 characters.

Table of contents
1 Advantages of Scheme
2 Disadvantages of Scheme
3 Standards
4 Language Elements
5 Examples
6 Implementations
7 Additional Resources

Advantages of Scheme

Scheme has very little syntax compared to many other programming languages. It has no operator precedence rules because there are essentially no operators—as in Lisp, prefix notation is used for all function calls.

Scheme's macro facilities allow it to be adapted to any problem domain. They can be used to add support for object-oriented programming. Scheme provides a hygienic macro system which, while not quite as powerful as Common Lisp's macro system, is much safer and often easier to work with.

Scheme encourages functional programming. Pure functional programs need no global variables and don't have side-effects, and are therefore automatically thread-safe and automatically verifiable. However, Scheme also supports variable assignment for those who want it.

In Scheme, functions are first class data types. This means they can be passed as arguments to other functions or stored in variables and manipulated. This allows higher order functions that can further abstract program logic. Functions can also be created anonymously.

Disadvantages of Scheme

Unlike scripting languages such as Perl or Python, Scheme is not standardized beyond its core. Functions that exist in one Scheme implementation will not necessarily exist in another, or may have a completely different name and/or interface. The Scheme Requests for Implementation (SRFI) process tries to remedy this.

Standards

There are two standards that define the Scheme language: the official IEEE standard, and a de facto standard called the Revisedn-th Report on the Algorithmic Language Scheme, nearly always abbreviated RnRS, where n is the number of the revision. The latest RnRS version is R5RS, also available online.

Language Elements

Comments

Comments are preceded by a semicolon (;) and continue for the rest of the line.

Variables

Variables are dynamically typed. Variables are defined by either a define or a let statement. Variables declared outside of any function are in global scope.

 (define var1 value)

(let ((var2 value)) ...)

Functions

Functions are first-class objects in Scheme. They can be assigned to variables. For example a function with two arguments arg1 and arg2 can be defined as

 (define fun
   (lambda (arg1 arg2)
      ...))

which can be abbreviated as follows:

 (define (fun arg1 arg2)
   ...)

Functions can be called with the following syntax:

 (fun value1 value2)

Note that the function being called is in the first position of the list while the rest of the list contain the arguments. The apply function will take the first argument and apply the rest of the arguments to the first argument, so the previous function call can also be written as

 (apply fun (list value1 value2))

Lists

Some consider the list Scheme's fundamental data type. A list can be constructed by consing elements together. The empty list is denoted as '(). As an example a list containing 1, 2, 3, 4 can be constructed as

 (cons 1 (cons 2 (cons 3 (cons 4 '()))))

which can be abbreviated as

 (list 1 2 3 4)

or as

 '(1 2 3 4)

Note the quote ('), this tells Scheme to not interpret the expression that follows the quote.

Data Types

Other common data types in Scheme besides functions and lists are: integer, rational, real, complex numbers, symbols, strings, portss. Most Scheme implementations also offer association lists, hash tables, vectorss, arrays and structures.

Most Scheme implementations offer a full numerical tower as well as exact and inexact arithmetic.

True and false are represented by the symbols #t and #f. Actually only #f is really false when a Boolean type is required, everything else will be interpreted by Scheme as #t including the empty list. This is in contrast with LISP where the empty list is also interpreted as representing false.

Symbols can be defined in at least the following ways:

 'symbol
 (string->symbol "symbol")

Equality

Scheme has three different types of equality.

; eqv?: Returns #t if two objects can be considered the same object. ; eq?: Returns #t in mostly the same situations as eqv? but uses some finer distinctions. ; equal?: Compares the content of data structures such as lists, vectors and strings to determine if they are the same.

Type dependent equivalence operations also exist in Scheme: ; string=?: To compare two strings ; char=?: To compare characters ; =: To compare numbers

Control Structures

Conditional evaluation

 (cond (test1 expr1)
       (test2 expr2)
       ...
       (else exprn))

The first expression for which the test evaluates to true (anything other than #f counts as true) will be evaluated. If all test result in #f, the else clause is evaluated.

A variant of the cond clause is

 (cond ...
       (test => expr)
       ...)

In this case, expr should evaluate to a function that takes one argument. If test evaluates to true, the function is called with the return value of test.

Scheme also has

 (if test then-expr else-expr)

but it is used much less because cond is both more general and has overall better readability.

Loops

Loops in Scheme usually take the form of tail recursion. A classical example is the factorial function, which can be defined non-tail-recursively:

 (define (factorial n)
   (cond ((= n 0) 1)
         (else (* n (factorial (- n 1))))))

(factorial 5) ;; => 120

or a higher order function like map which applies a function to every element of a list, and can be defined non-tail-recursively:

 (define (map f lst)
   (cond ((null? lst) lst)
         (else (cons (f (car lst))
                     (map f (cdr lst))))))

(map (lambda (x) (* x x)) '(1 2 3 4)) ;; => (1 4 9 16)

We can define both of these tail-recursively as follows. The named let statement and the do statement are syntactic sugar which simplify tail-recursive definitions. To use factorial and map again to illustrate both forms:

 (define (factorial n)
   (let loop ((fact 1)
              (n n))
     (cond ((= n 0) fact)

(else (loop (* n fact) (- n 1))))))

(factorial 5) ;; => 120

 (define (map f lst)
   (do ((lst lst (cdr lst))
        (res '() (cons (f (car lst)) res)))
       ((null? lst) (reverse res))))

(map (lambda (x) (* x x)) '(1 2 3 4)) ;; => (1 4 9 16)

Please note that in both cases the tail-recursive version is preferrable due to its decreased use of space.

Input/output

Scheme has the concept of ports to read from or to write to. Scheme defines three default ports, accessible with the functions: current-input-port, current-output-port and current-error-port.

Examples

Scheme code can be found in the following Wikipedia articles:

Implementations

Additional Resources