Introduction
Scheme is a small dialect of Lisp created at MIT in the 1970s by Guy Steele and Gerald Sussman. It is defined by a series of reports, the Revisedn Report on the Algorithmic Language Scheme, of which R5RS (1998), R6RS (2007) and R7RS-small (2013) are the ones in common use.
Source files are usually named .scm, and .ss is common with
Chez Scheme and in older PLT Scheme code. R7RS libraries are kept in .sld
files, and R6RS libraries and programs in .sls and .sps
files. GIMP’s Script-Fu extensions are Scheme in .scm files, and
.scm is also used for Tree-sitter query files, which are not Scheme
programs but borrow its bracket syntax.
Structure
A Scheme file is plain text made of parenthesised forms, evaluated in order. Variables
and functions share one namespace and are defined with define. Local names
come from let and functions from lambda. Implementations must
run tail calls without growing the stack, so loops are often written as recursion, and
call/cc captures the rest of a computation as a value.
Comments start with ;, #| … |# marks a block
and #; comments out the next form. Booleans are #t and
#f. From R6RS on, symbols are case-sensitive. An R7RS program opens with
import to name the libraries it needs, and an R7RS library is wrapped in
define-library.
(import (scheme base) (scheme write))
; A recursive definition, with a tail call in the helper.
(define (factorial n)
(let loop ((i n) (acc 1))
(if (= i 0)
acc
(loop (- i 1) (* acc i)))))
(display (factorial 5))
(newline)
let loop.Reference pages
These pages describe the language against R7RS-small (2013), with the differences of R5RS and R6RS marked where they matter, in the order a reader meets it.
- File structure – extensions, encoding, first lines, programs, libraries and scripts
- Lexical structure – every token form, and how the viewer colours them
- Data types – the disjoint types, truthiness, equivalence and external representation
- Expressions and special forms – every standard syntax keyword
- Control and tail calls – proper tail recursion, iteration, multiple values, promises
- Continuations and dynamic-wind –
call/cc, escapes, generators and unwinding - Macros –
syntax-rules, the pattern language,syntax-caseand the procedural macros of the implementations - Procedures and numbers – lambda lists, the numeric tower, exactness, every numeric procedure
- Lists, vectors and strings – pairs, symbols, characters, strings, vectors and bytevectors
- I/O ports – textual and binary ports, string ports, reading and writing
- Libraries and modules – R7RS
define-library, R6RSlibrary, import sets, and the module systems of the implementations - Records – SRFI 9 and R7RS records, R6RS records, and the record systems of others
- Error handling –
raise,guard, error objects and R6RS conditions - Standard libraries – every library of R7RS-small and every library of R6RS, with their exports
- SRFI – what an SRFI is and the ones that matter most
- Implementations and dialects – Chez, Guile, CHICKEN, Gambit, MIT/GNU, Chibi, Gauche, Bigloo and others, and how their files differ
- History and standards – from the 1975 report to R7RS-large, and what each revision added
Not described
Known, not yet covered
- Reference: the R7RS-small report was read as its LaTeX source, R6RS and R5RS as their HTML editions, and the SRFI index page was read, but the PDF editions and the individual SRFI documents were not (this Mac has no PDF text extractor, and no SRFI document was downloaded). SRFI contents on the SRFI page come from the index entries only, and the procedure lists of SRFI 1, 13, 43, 69 and others that the viewer colours are from memory.
- Reference: the manuals of the implementations (Chez, Guile, CHICKEN, Gambit, MIT/GNU, Chibi, Gauche, Bigloo and others) were not read. Implementations and dialects names what is well known about each and does not describe their libraries, their extensions to the reader or their foreign-function interfaces beyond the few forms the viewer handles.
- Reference: R7RS-large is described from the SRFI index only (which SRFIs its Red and Tangerine editions take in); its text was not read.
- Reference: individual procedures are listed with their signatures where R7RS-small gives them and by area for R6RS, but their full behaviour, edge cases and error conditions are not each described; the reports are the place for that. The formal semantics of R5RS to R7RS is not described.
- Viewer: the colouring follows the text and not the program. A macro the program defines is coloured as a function call unless its name starts
define-; a name that a program shadows (a variable calledlist) is still coloured as the standard procedure; the pattern variables of macros other thansyntax-rules,syntax-case,matchanddefine-syntax-ruleare not told from code. - Viewer: reader extensions of single implementations are handled only where listed on Lexical structure; the others (for example Gambit’s
#!object syntax beyond the ones listed, SRFI 108 and 109 quasi-literals, SRFI 119wispand SRFI 110 sweet-expressions, whose layout is indentation) are coloured by the ordinary rules and may show tokens as plain text. - Viewer: a file with the extension
.schor.spsis shown as Scheme only if it opens like Scheme; Chicken’s.egg,.setupand.metafiles and Guile’s.gocompiled files are not registered.
Unknown
- Viewer: the colouring was tested on hand-written cases for every token form and on the .scm, .ss and .sld files found on this Mac (see the progress notes); most of them are small teaching programs, a Festival speech script and Tree-sitter query files. No large Scheme system was available to test it on.
- Reference: the Bigloo header extension
.schis registered because Bigloo uses it, but no Bigloo file was available to check how its header files are laid out.
Links
Software
- Chez Scheme – fast compiler for R6RS Scheme (Windows, macOS, Linux)
- GNU Guile – Scheme implementation made for embedding in other programs (Linux, macOS)
- CHICKEN – Scheme that compiles through C (Windows, macOS, Linux)
- Chibi-Scheme – small R7RS Scheme for embedding (Windows, macOS, Linux)
Reference
- R7RS small language – the current small report
- R6RS – the sixth report and its libraries
- Scheme standards – the reports and related documents
- Scheme Requests for Implementation – the index of SRFIs