Log in / create account | Login with OpenID
DocForge
Programmer's Wiki

Scheme

From DocForge

Scheme is a multi-paradigm programming language. It is one of the two main dialects of Lisp and supports a number of programming paradigms but is best known for its support of functional programming. It was developed by Guy L. Steele and Gerald Jay Sussman in the 1970s. Scheme was introduced to the academic world via a series of papers now referred to as Sussman and Steele's Lambda Papers. There are two standards that define the Scheme language: the official IEEE standard, and a de facto standard called the Revisedn Report on the Algorithmic Language Scheme, nearly always abbreviated RnRS, where n is the number of the revision. The current standard is R5RS is in development.

Scheme's philosophy is minimalist. Scheme provides as few primitive notions as possible, and, where practical, lets everything else be provided by programming libraries.

Scheme was the first dialect of Lisp to choose static (a.k.a. lexical) over dynamic variable scope. It was also one of the first programming languages to support first-class continuations.

Contents

[edit] Origin

Scheme started as an attempt to understand Carl Hewitt's Actor model. Scheme was originally called "Schemer", in the tradition of other Lisp-derived languages like Planner or Conniver. The current name resulted from the authors' use of the ITS operating system, which limited filenames to two components of at most six characters each. Currently, "Schemer" is commonly used to refer to a Scheme programmer.

[edit] Future

A new language standardization process began at the 2003 Scheme workshop, with the goal of producing an R6RS standard in 2006. It breaks with the earlier RnRS approach of unanimity. R6RS will feature a standard module system; allowing a split between the core language and libraries. A draft of the R6RS specification (whimsically named "R5.91RS") was released for comment in October, 2006. An updated draft, R5.92RS, was released on January 19th 2007, and yet another draft, R5.93RS, on May 22nd 2007. A final draft is due to be released by June 12th 2007, and a subsequent vote will determine whether the draft is accepted or not. The announcement of the final results of the vote, and possible ratification of the new R6RS, will take place on August 7th 2007.

[edit] Distinguishing features

Scheme is a minimalist language. The current language standard is only 50 pages, including a denotational semantics for the language core. The next revision of the standard will be expanded to describe several libraries.

Like all Lisp dialects, Scheme has little syntax. There are no operator precedence rules because fully nested and parenthesized notation is used for all compound forms.

Scheme's macro system allows the user to add new syntactic constructs to the language. It respects the lexical scoping of the rest of the language, which avoids common programming errors that can occur in the macro systems of other programming languages.

Procedures in Scheme are first-class values, which allows for functional programming.

Scheme's call-with-current-continuation operator allows the user to create non-local control constructs that must be built into other languages, such as iterators, coroutines, and backtracking.

[edit] Tail recursion

For additional details, see tail recursion.

Even though looping constructs (such as the do loop and named let) are provided in Scheme, and one can use macros to easily define iterating control structures, Scheme programs often use tail recursion to express loops, much more commonly than in other languages. This is in part due to the fact that Scheme implementations are required to optimize tail calls so as to eliminate use of stack space where possible, so that arbitrarily long loops are guaranteed to run in constant space.

In contrast, implementations of other languages often do not optimize tail calls, making tail recursion impractical due to a limited amount of stack.

[edit] Syntax

[edit] Comments

Each comment is preceded by a semicolon (;) and extends for the rest of the line. Some implementations allow comments to span multiple lines by wrapping them with a #|...|# (possibly nested). Other implementations allow an entire s-expression to be commented out by prepending it with #;.

[edit] Variables

Variables are dynamically typed. Variables are bound by a define, a let expression, and a few other Scheme forms. Variables bound at the top level with a define are in global scope.

(define var1 value)

A define expression is equivalent to a let expression whose body is the rest of the current scope.

Variables bound in a let are in scope for the body of the let.

(let ((var1 value))
    ...
    ; scope of var1
    ...)

let is a convenient syntax that is not fundamentally necessary. A let expression can be implemented using procedures directly. For example, the above is equivalent to:

((lambda (var1)
    ...
    ; scope of var1
    ...) value)

[edit] Functions

1 (define fun
   (lambda (arg1 arg2)
     ...))
2 (define (fun arg1 arg2)
   ...)
3 (fun value1 value2)
4 (apply fun (list value1 value2))

Functions are first-class objects in Scheme. They can be arguments to other functions and be returned by them. They can be assigned to variables. Functions are created by lambda forms. For example a function with two arguments arg1 and arg2 is defined in line 1; line 2 is a shorter, equivalent form. line 3 shows how functions are applied. Note that the function being applied is in the first position of the list while the rest of the list contains the arguments. The apply function will take its first argument and apply it to a given list of arguments, so the previous function call can also be written as seen in line 4.

In Scheme, functions are divided into two basic categories: procedures and primitives. All primitives are procedures, but not all procedures are primitives. Primitives are pre-defined functions in the Scheme language. These include +, -, *, /, set!, car, cdr, and other basic procedures. Procedures are user-defined functions. In several variations of Scheme, a user can redefine a primitive. For example, the code

(define (+ x y)
  (- x y))

or simply

(define + -)

actually redefines the + primitive to perform subtraction, rather than addition.

[edit] Lists

For additional details, see linked list.

Scheme uses the singly-linked list data structure with accessors car and cdr.

[edit] Data Types

Besides procedures and lists, Scheme provides the following data types: atomic symbols, numbers, booleans, characters, strings, vectors and ports. Many Scheme implementations also offer association lists, hash tables and structures.

Since the IEEE Scheme standard and the R4RS Scheme standard, Scheme has asserted that all of the above types are disjoint, that is no value can belong to more than one of these types; however some older implementations of Scheme predate these standards such that #f and '() refer to the same value, as is the case in traditional Lisp including Common Lisp.

The numeric type is further divided into a numerical tower, with subtypes complex, real, rational and integer. (Note that these subtypes are not disjoint; in fact each type is a subset of the last). While it is not required that a Scheme implementation support the entire numerical tower, most implementations do. Scheme also keeps track of whether a given number is "exact" or "inexact".

The boolean type represents true and false by #t and #f respectively. For historical reasons, however, any value can be used where a boolean is expected - any value other than #f is considered to be true, including the empty list (in traditional Lisp and Common Lisp, the empty list is considered to be false).

Symbols can be created in at least the following ways:

'hello
(string->symbol "hello")

[edit] Equality

Scheme has three different types of equality: "eq?" returns #t if its parameters represent the same data object in memory; "eqv?" is generally the same as eq? but treats some objects (eg. characters and numbers) specially so that numbers that are = are eqv? even if they are not eq?; equal? compares data structures such as lists, vectors and strings to determine if they have congruent structure and eqv? contents.

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

[edit] Control Structures

[edit] Conditional Evaluation

(if test then-expr else-expr)

The test expression is evaluated, and if the evaluation result is true (anything other than #f) then the then-expr is evaluated, otherwise else-expr is evaluated.

A form that is more convenient when conditionals are nested is cond:

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

The first expression for which the test evaluates to true will be evaluated. If all tests 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.

[edit] Input/Output

Scheme has the concept of ports to read from or to write to. R5RS defines two default ports, accessible with the functions current-input-port and current-output-port, which correspond to the Unix notions of stdin and stdout. Most implementations also provide current-error-port.

[edit] Usage

Scheme is widely used by a number of schools; in particular, a number of introductory Computer Science courses use Scheme in conjunction with the textbook Structure and Interpretation of Computer Programs. There are relatively few examples of Scheme in apparent usage for non-pedagogical purposes. However, the Document Style Semantics and Specification Language, which provides a method of specifying SGML stylesheets, uses a Scheme subset. In addition, the well-known open source raster graphics editor, the GIMP uses Scheme as a scripting language.

[edit] External Links

Additional copyright notice: Some content of this page is a derivative work of a Wikipedia article under the GNU FDL. The original article and author information can be found at http://en.wikipedia.org/wiki/Scheme_%28programming_language%29.