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. R6RS and R7RS libraries are often kept in
.sls and .sld 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.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