The Scheme programming language is a functional programming language which is 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 let 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. Like Lisp, 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 minimalism of the specification, there is no standard syntax for creating structures with named fields, or for doing object oriented progamming, but many individual implementations have such features.
Why the curious name? Well, it was originally called "Schemer", in the tradition of the languages Planner[?] and Conniver[?], but its authors used the ITS operating system[?] which didn't allow filenames longer than 6 characters.
|
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.
Thanks to its macro facilities, Scheme can be adapted to any problem domain. They can even 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 macros, 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, automatically verifiable and have more of these nice properties. However, Scheme also supports variable assignment[?] for those who want it.
In Scheme, functions are first class citizens. This means they can be passed as arguments to another function or stored in a variable 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 do not need to 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 (http://www.schemers.org/Documents/Standards/R5RS/).
Comments are preceded by a semi-colon (;) and continue for the rest of the line.
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 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))
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.
Other common data types in Scheme besides functions and lists are: integer, rational, real, complex numbers, symbols, strings, ports. Most Scheme systems also offer association lists[?], hash tables, vectors, arrays and structures.
Most Scheme systems 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")
Scheme has three different types of equality.
Type dependent equivalence operations also exist in Scheme:
(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 then-expr else-expr)
but it is used much less because cond
is both more general and has overall better readability.
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 simpify 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.
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 at least the following Wikipedia articles:
Search Encyclopedia
|
Featured Article
|