Content deleted Content added
m added "was" to "R6RS ratified" |
Northeastern University no longer uses Scheme-based languages, due to administrative incompetence Tag: references removed |
||
(46 intermediate revisions by 34 users not shown) | |||
Line 2:
{{Infobox programming language
| name = Scheme
| logo =
| logo size = 121px
| paradigms = [[Multi-paradigm programming language|Multi-paradigm]]: [[
| family = [[Lisp (programming language)|Lisp]]
| year = {{Start date and age|1975}}
| designers = [[Guy L. Steele]]<br/>[[Gerald Jay Sussman]]
| latest release version = R7RS
| latest release date = {{Start date and age|2013}}
Line 15 ⟶ 16:
| operating system =
| license =
| website = {{URL|https://www.scheme.org/}}
| file_ext = .scm, .ss
| implementations = Many<br/>(see [[:Category:Scheme (programming language) implementations|Scheme implementations]])
| influenced by = [[ALGOL]], [[Lisp (programming language)|Lisp]], [[MDL (programming language)|MDL]]
| influenced = [[Clojure]], [[Common Lisp]], [[Dylan (programming language)|Dylan]], [[EuLisp]], [[
| wikibooks = Scheme
}}
'''Scheme''' is a [[programming language dialect|dialect]] of the [[Lisp (programming language)|Lisp]] family of [[programming language]]s. Scheme was created during the 1970s at the [[MIT Computer Science and Artificial Intelligence Laboratory
The Scheme language is standardized in the official [[Institute of Electrical and Electronics Engineers
-->and a ''de facto'' standard called the ''Revised{{padlsup|n}} Report on the Algorithmic Language Scheme'' (R''n''RS). A widely implemented standard is R5RS (1998).<ref name="r5rs">{{
-->The most recently ratified standard of Scheme<!--
--> is "R7RS-small" (2013).<ref name="r7rs">{{
-->
Line 35 ⟶ 36:
===Origins===
Scheme started in the 1970s as an attempt to understand [[Carl Hewitt]]'s [[Actor model]], for which purpose Steele and Sussman wrote a "tiny Lisp interpreter" using [[Maclisp]] and then "added mechanisms for creating actors and sending messages".<ref name="revisited">{{
===R6RS===
A new language standardization process began at the 2003 Scheme workshop, with the goal of producing an R6RS standard in 2006. This process broke with the earlier R''n''RS approach of unanimity.
R6RS features a standard module system, allowing a split between the core language and [[Library (computing)|libraries]].
Currently the newest releases of various Scheme implementations<ref name="rs6s_Implementations">{{
A feature of R6RS is the record-type descriptor (RTD). When an RTD is created and used, the record type representation can show the memory layout. It also calculated object field bit mask and mutable Scheme object field bit masks, and helped the garbage collector know what to do with the fields without traversing the whole fields list that are saved in the RTD. RTD allows users to expand the basic RTD to create a new record system.<ref>{{
R6RS introduces numerous significant changes to the language.<ref name="r6rs_Language_changes">{{
===R7RS===
The R6RS standard has caused controversy because some see it as a departure from the minimalist philosophy.<ref name="r6rs_electorate">{{
The ninth draft of R7RS (small language) was made available on April 15, 2013.<ref name="r7rs-draft-9">{{
{{Lisp}}
Line 62 ⟶ 63:
The reliance on lists as data structures is shared by all Lisp dialects. Scheme inherits a rich set of [[List (computing)|list-processing]] primitives such as [[Cons|<code>cons</code>]], [[CAR and CDR|<code>car</code> and <code>cdr</code>]] from its Lisp progenitors. Scheme uses strictly but [[Type system|dynamically typed variables]] and supports [[first-class function|first class procedures]]. Thus, procedures can be assigned as values to variables or passed as arguments to procedures.
This section concentrates mainly on innovative features of the language, including those features that distinguish Scheme from other Lisps. Unless stated otherwise, descriptions of features relate to the R5RS standard. In examples provided in this section, the notation "===> result" is used to indicate the result of evaluating the expression on the immediately preceding line.
===Minimalism===
{{Main|Minimalism (computing)}}
Scheme is a very simple language, much easier to implement than many other languages of comparable [[Expressive power (computer science)|expressive power]].<ref name="easy_to_implement_scheme48">The [[Scheme 48]] implementation is so-named because the interpreter was written by Richard Kelsey and Jonathan Rees in 48 hours (August 6th{{spndash}}7th, 1986. See {{
: '''Fundamental forms''': define, lambda, quote, if, define-syntax, let-syntax, letrec-syntax, syntax-rules, set!
Line 82 ⟶ 83:
Thus using <code>let</code> as defined above a Scheme implementation would rewrite "<code>(let ((a 1)(b 2)) (+ b a))</code>" as "<code>((lambda (a b) (+ b a)) 1 2)</code>", which reduces implementation's task to that of coding procedure instantiations.
In 1998, Sussman and Steele remarked that the minimalism of Scheme was not a conscious design goal, but rather the unintended outcome of the design process.
===Lexical scope===
{{See also|Scope (programming)}}
Like most modern programming languages and unlike earlier Lisps such as [[Maclisp]], Scheme is lexically scoped: all possible variable bindings in a program unit can be analyzed by reading the text of the program unit without consideration of the contexts in which it may be called. This contrasts with dynamic scoping which was characteristic of early Lisp dialects, because of the processing costs associated with the primitive textual substitution methods used to implement lexical scoping algorithms in compilers and interpreters of the day.
The impetus to incorporate lexical scoping, which was an unusual scoping model in the early 1970s, into their new version of Lisp, came from Sussman's studies of [[ALGOL]]. He suggested that [[Block (programming)|ALGOL-like lexical scoping mechanisms]] would help to realize their initial goal of implementing [[Carl Hewitt#Actor model|Hewitt's Actor model]] in Lisp.<ref name="revisited"/>
The key insights on how to introduce lexical scoping into a Lisp dialect were popularized in Sussman and Steele's 1975 Lambda Paper, "Scheme: An Interpreter for Extended Lambda Calculus",<ref name="lambda_paper_1">{{
--><ref name="Moses">{{
===Lambda calculus===
{{See also|Lambda calculus}}
[[Alonzo Church]]'s mathematical notation, the lambda calculus, has inspired Lisp's use of "lambda" as a keyword for introducing a procedure, as well as influencing the development of [[functional programming]] techniques involving the use of [[higher-order function]]s in Lisp.
A formal lambda system has axioms and a complete calculation rule. It is helpful for the analysis using mathematical logic and tools. In this system, calculation can be seen as a directional deduction. The syntax of lambda calculus follows the recursive expressions from x, y, z, ...,parentheses, spaces, the period and the symbol λ.<ref>{{
The introduction of lexical scope resolved the problem by making an equivalence between some forms of lambda notation and their practical expression in a working programming language.
===Block structure===
Scheme inherits its block structure from earlier block structured languages, particularly [[ALGOL]].
<syntaxhighlight lang="Scheme">
Line 108 ⟶ 109:
;; Any reference to var here will be bound to "goose"
(let ((var 10))
;; statements go here.
)
;; Any reference to var here will be bound to "goose"
</syntaxhighlight>
Blocks can be [[Nesting (computing)|nested]] to create arbitrarily complex block structures according to the need of the programmer.
One variant of <code>let</code>, <code>let*</code>, permits bindings to refer to variables defined earlier in the same construct, thus:
Line 153 ⟶ 154:
All procedures bound in a single <code>letrec</code> may refer to one another by name, as well as to values of variables defined earlier in the same <code>letrec</code>, but they may not refer to ''values'' defined later in the same <code>letrec</code>.
A variant of <code>let</code>, the "named let" form, has an identifier after the <code>let</code> keyword.
Example: a simple counter
Line 189 ⟶ 190:
===First-class continuations===
{{Main|Continuation}}
Continuations in Scheme are [[first-class object]]s.
Continuations can be used to emulate the behavior of [[return statement]]s in imperative programming languages. The following function <code>find-first</code>, given function <code>func</code> and list <code>lst</code>, returns the first element <code>x</code> in <code>lst</code> such that <code>(func x)</code> returns true.
Line 219 ⟶ 220:
</syntaxhighlight>
When executed this code displays a counting sequence: <code>@*@**@***@****@*****@******@*******@********...</code>
<!-- Bear with me, I'm writing a clear English explanation of how this works, but it isn't easy. I'll add it when it's done. -->
===Shared namespace for procedures and variables===
In contrast to Common Lisp, all data and procedures in Scheme share a common namespace, whereas in Common Lisp [[Common Lisp#The function namespace|functions and data have separate namespaces]] making it possible for a function and a variable to have the same name, and requiring special notation for referring to a function as a value.
In Scheme, the same primitives that are used to manipulate and bind data can be used to bind procedures.
<syntaxhighlight lang="Scheme">
Line 258 ⟶ 257:
===Numerical tower===
{{Main|Numerical tower}}
Scheme specifies a comparatively full set of numerical datatypes including [[complex number|complex]] and [[rational number|rational]] types, which is known in Scheme as the numerical tower (R5RS sec. 6.2<ref name="r5rs"/>).
Numbers may have the quality of exactness.
The R5RS standard specifies procedures <code>exact->inexact</code> and <code>inexact->exact</code> which can be used to change the exactness of a number.
In the R5RS standard, Scheme implementations are not required to implement the whole numerical tower, but they must implement "a coherent subset consistent with both the purposes of the implementation and the spirit of the Scheme language" (R5RS sec. 6.2.3).<ref name="r5rs"/> The new R6RS standard does require implementation of the whole tower, and "exact integer objects and exact rational number objects of practically unlimited size and precision, and to implement certain procedures...so they always return exact results when given exact arguments" (R6RS sec. 3.4, sec. 11.7.1).<ref name="r6rs"/>
Line 315 ⟶ 314:
===> 22
</syntaxhighlight>
The lexical context of the original definition of the promise is preserved, and its value is also preserved after the first use of <code>force</code>.
These primitives, which produce or handle values known as [[Futures and promises|promises]], can be used to implement advanced [[lazy evaluation]] constructs such as [[stream (computing)|stream]]s.<ref name="srfi-41">{{
In the R6RS standard, these are no longer primitives, but instead, are provided as part of the R5RS compatibility library (rnrs r5rs (6)).
Line 323 ⟶ 322:
In R5RS, a suggested implementation of <code>delay</code> and <code>force</code> is given, implementing the promise as a procedure with no arguments (a [[thunk]]) and using [[memoization]] to ensure that it is only ever evaluated once, irrespective of the number of times <code>force</code> is called (R5RS sec. 6.4).<ref name="r5rs"/>
SRFI 41 enables the expression of both finite and infinite sequences with extraordinary economy.
<syntaxhighlight lang="Scheme">
Line 339 ⟶ 338:
===Order of evaluation of procedure arguments===
Most Lisps specify an order of evaluation for procedure arguments.
<syntaxhighlight lang="Scheme">
(let ((ev (lambda(n) (display "Evaluating ")
Line 347 ⟶ 346:
===> 3
</syntaxhighlight>
<syntaxhighlight lang="output">
ev is a procedure that describes the argument passed to it, then returns the value of the argument. In contrast with other Lisps, the appearance of an expression in the operator position (the first item) of a Scheme expression is quite legal, as long as the result of the expression in the operator position is a procedure.▼
In calling the procedure "+" to add 1 and 2, the expressions (ev +), (ev 1) and (ev 2) may be evaluated in any order, as long as the effect is not as if they were evaluated in parallel. Thus the following three lines may be displayed in any order by standard Scheme when the above example code is executed, although the text of one line may not be interleaved with another because that would violate the sequential evaluation constraint.▼
</syntaxhighlight>
▲ev is a procedure that describes the argument passed to it, then returns the value of the argument.
▲In calling the procedure "{{mono|+}}" to add 1 and 2, the expressions {{mono|(ev +), (ev 1)}} and {{mono|(ev 2)}} may be evaluated in any order, as long as the effect is not as if they were evaluated in parallel.
▲: Evaluating 1
▲: Evaluating 2
▲: Evaluating procedure
===Hygienic macros===
{{Main|Hygienic macro}}
In the R5RS standard and
Implementations of the hygienic macro system, also called <code>syntax-rules</code>, are required to respect the lexical scoping of the rest of the language.
<syntaxhighlight lang="Scheme">
Line 370:
</syntaxhighlight>
Invocations of macros and procedures bear a close resemblance—both are s-expressions—but they are treated differently.
Most Scheme implementations also provide additional macro systems.
The inability to specify whether or not a macro is hygienic is one of the shortcomings of the macro system. Alternative models for expansion such as scope sets provide a potential solution.<ref>{{
===Environments and eval===
Prior to R5RS, Scheme had no standard equivalent of the <code>eval</code> procedure which is ubiquitous in other Lisps, although the first Lambda Paper had described <code>evaluate</code> as "similar to the LISP function EVAL"<ref name="lambda_paper_1"/> and the first Revised Report in 1978 replaced this with <code>enclose</code>, which took two arguments.
The reason for this confusion is that in Scheme with its lexical scoping the result of evaluating an expression depends on where it is evaluated.
Retrieved 2012-08-09</ref>
<syntaxhighlight lang="Scheme">
Line 387:
</syntaxhighlight>
If it is evaluated in the outer environment, where <code>name</code> is defined, the result is the sum of the operands.
R5RS resolves this confusion by specifying three procedures that return environments and providing a procedure <code>eval</code> that takes an s-expression and an environment and evaluates the expression in the environment provided. (R5RS sec. 6.5)<ref name="r5rs"
With modern scheme (usually compatible with R5RS) to evaluate this expression, one needs to define a function <code>evaluate</code> which can look like this:
Line 400:
<code>interaction-environment</code> is the interpreter's global environment.
===Treatment of non-
In most dialects of Lisp including Common Lisp, by convention the value <code>NIL</code> evaluates to the value false in a
Where the constant representing the
===Disjointness of primitive datatypes===
In Scheme the primitive datatypes are disjoint.
Within the numerical datatype, by contrast, the numerical values overlap.
===Equivalence predicates===
Line 421:
===Comments===
{{See also|Comment (computer programming)}}
Up to the R5RS standard, the standard comment in Scheme was a semicolon, which makes the rest of the line invisible to Scheme.
===Input/output===
Scheme's input and output is based on the ''port'' datatype. (R5RS sec 6.6)<ref name="r5rs"/> R5RS defines two default ports, accessible with the procedures <code>current-input-port</code> and <code>current-output-port</code>, which correspond to the Unix notions of [[Standard streams|standard input and standard output]]. Most implementations also provide <code>current-error-port</code>. [[Redirection (computing)|Redirection]] of input and standard output is supported in the standard, by standard procedures such as <code>with-input-from-file</code> and <code>with-output-to-file</code>.
The following examples are written in strict R5RS Scheme.
Line 461:
</syntaxhighlight>
Similar procedures are provided for input. R5RS Scheme provides the predicates <code>input-port?</code> and <code>output-port?</code>.
===Redefinition of standard procedures===
In Scheme, procedures are bound to variables.
<syntaxhighlight lang="Scheme">
Line 487:
In Standard Scheme, procedures that convert from one datatype to another contain the character string "->" in their name, predicates end with a "?", and procedures that change the value of already-allocated data end with a "!". These conventions are often followed by Scheme programmers.
In formal contexts such as Scheme standards, the word "procedure" is used in preference to "function" to refer to a lambda expression or primitive procedure.
As in other Lisps, the term "[[thunk]]" is used in Scheme to refer to a procedure with no arguments. The term "proper tail recursion" refers to the property of all Scheme implementations, that they perform tail-call optimization so as to support an indefinite number of active [[tail call]]s.
The form of the titles of the standards documents since R3RS, "Revised<sup>n</sup> Report on the Algorithmic Language Scheme", is a reference to the title of the [[ALGOL|ALGOL 60]] standard document, "Revised Report on the Algorithmic Language Algol 60," The Summary page of R3RS is closely modeled on the Summary page of the ALGOL 60 Report.<!--
--><ref name="algol_report">{{
== Review of standard forms and procedures ==
The language is formally defined in the standards R5RS (1998)<ref name="r5rs"/> and R6RS (2007).<ref name="r6rs"/> They describe standard "forms": keywords and accompanying syntax, which provide the control structure of the language, and standard procedures which perform common tasks.▼
▲The language is formally defined in the standards R5RS (1998) and R6RS (2007). They describe standard "forms": keywords and accompanying syntax, which provide the control structure of the language, and standard procedures which perform common tasks.
===Standard forms===
This table describes the standard forms in Scheme.
Forms marked "L" in this table are classed as derived "library" forms in the standard and are often implemented as macros using more fundamental forms in practice, making the task of implementation much easier than in other languages.
Line 527 ⟶ 525:
|}
===Standard procedures===
Line 607 ⟶ 605:
== Scheme Requests for Implementation ==
{{Main|Scheme Requests for Implementation}}
Because of Scheme's minimalism, many common procedures and syntactic forms are not defined by the standard.
SRFIs with fairly wide support in different implementations include:<ref name="srfi_support">{{
* 0: feature-based conditional expansion construct
Line 642 ⟶ 640:
== Implementations ==
{{
The elegant, minimalist design has made Scheme a popular target for language designers, hobbyists, and educators, and because of its small size, that of a typical [[Interpreter (computing)|interpreter]], it is also a popular choice for [[embedded system]]s and [[scripting language|scripting]].
Almost all implementations provide a traditional Lisp-style [[read–eval–print loop]] for development and debugging. Many also [[Compiler|compile]] Scheme programs to executable binary. Support for embedding Scheme code in programs written in other languages is also common, as the relative simplicity of Scheme implementations makes it a popular choice for adding scripting capabilities to larger systems developed in languages such as [[C (programming language)|C]]. The [[Gambit (Scheme implementation)|Gambit]], [[Chicken (Scheme implementation)|Chicken]], and [[Bigloo]] Scheme interpreters compile Scheme to C, which makes embedding
Some implementations support
== Usage ==
Scheme is widely used by
The textbook ''[[How to Design Programs]]''
The former introductory
Scheme is/was also used for the following:
* The [[Document Style Semantics and Specification Language]] (DSSSL), which provides a method of specifying [[SGML]] [[Style sheet (web development)|stylesheet]]s, uses a Scheme subset.<ref name="dsssl">{{
* The well-known [[Open-source software|open source]] [[raster graphics editor]] [[GIMP]] uses [[TinyScheme]] as a [[scripting language]].<ref name="gimp">"''The major scripting language for the GIMP that has been attached to it today is Scheme.''" From {{
* [[GNU Guile|Guile]] has been adopted by [[GNU]] project as its official scripting language, and that implementation of Scheme is embedded in such applications as [[GNU LilyPond]] and [[GnuCash]] as a scripting language for extensions. Likewise, Guile used to be the scripting language for the [[desktop environment]] [[GNOME]],<ref name="archive_gnomefaq">{{
* [[Extension Language Kit|Elk Scheme]] is used by [[Synopsys]] as a scripting language for its [[Technology CAD|technology CAD (TCAD)]] tools.<ref name="tcad">{{
* Shiro Kawai, senior programmer on the movie ''[[Final Fantasy: The Spirits Within]]'', used Scheme as a scripting language for managing the real-time rendering engine.<ref name="shiro_kawai">{{
* [[Google App Inventor]] for [[Android (operating system)|Android]] uses Scheme, where [[Kawa (Scheme implementation)|Kawa]] is used to compile the Scheme code down to
== See also ==
Line 673 ⟶ 671:
== Further reading ==
* [https://web.archive.org/web/20220313232321/ftp://ftp.cs.utexas.edu/pub/garbage/cs345/schintro-v14/schintro_toc.html An Introduction to Scheme and its Implementation] ([http://icem-www.folkwang-hochschule.de/~finnendahl/cm_kurse/doc/schintro/schintro_toc.html a mirror])
* {{
* {{
* {{cite wikisource |author1=Gerald Jay Sussman|author1-link=Gerald Sussman |author2=Guy Lewis Steele Jr.|author2-link=Guy Steele|name-list-style=amp |title=Scheme: An Interpreter for Extended Lambda Calculus |publisher=[[MIT Computer Science and Artificial Intelligence Laboratory|MIT Artificial Intelligence Lab]] |volume=AI Memo 349 |date=December 1975|citeseerx=10.1.1.128.80}}
== External links ==
*[https://
▲*[https://schemers.org/ schemers.org] provides links to a number of Scheme resources including the specifications
*[https://lips.js.org/docs/category/introduction-to-scheme Introduction to Scheme]
▲*{{Wikibooks-inline|Scheme Programming}}
*{{Wikibooks
*{{Commons category-inline|Scheme (programming language)}}
*[https://discu.eu/weekly/lisp/ Scheme Weekly]
*[https://lips.js.org/#bookmark Bookmarklet that add Interactive Scheme REPL to any website]
{{Authority control}}
|