ZetCode

Scheme tutorials

Scheme is a small, dynamically typed language from the Lisp family. Programs are written as nested lists in parentheses, procedures are first-class values, and loops are usually expressed as recursion with proper tail calls. The language has a short definition built on a few powerful ideas: lexical scope, closures, continuations, and hygienic macros. It is widely used for teaching, for research into programming languages, and as an extension language embedded in larger programs.

Scheme and Guile

Scheme is defined by the Revised Reports on the Algorithmic Language Scheme; the current ones are R5RS, R6RS, and R7RS small. The tutorials use GNU Guile, the extension language of the GNU project, which implements most of R7RS and R6RS and adds its own modules. On Debian and Ubuntu it is installed with sudo apt install guile-3.0, and a script runs with guile file.scm. The examples are run with Guile 3.0.7.

Getting started

The introduction covers the history and standards of the language, its implementations, installing Guile, auto-compilation, the REPL, and executable scripts. The basics tutorial writes prefix expressions, prints values with display and write, uses comments, defines values and procedures, makes a first decision with if, and reads an error report.

Language

The expressions and evaluation article explains prefix notation, the evaluation rule for calls, self-evaluating values, quote, special forms, begin, and the use of eval on a quoted list. Data types introduces booleans, numbers, characters, strings, symbols, pairs and lists, vectors, and procedures together with their predicates. The variables and binding article covers set!, let, let*, lambda, letrec, internal definitions, and lexical scope, and numbers works with big integers, exact rationals, exact and inexact values, integer division, rounding, and conversions.

Core language

The booleans and equality article combines tests with not, and, and or and compares values with =, eq?, eqv?, and equal?. Conditionals covers if, cond with the => arrow, case, when, and unless. The procedures article writes rest, optional, and keyword parameters with case-lambda and Guile's define*. Recursion processes numbers, lists, and trees, and tail calls and iteration turns recursion into loops with accumulators, the named let, and do. The pairs and lists article covers cons cells, the cadr family, append, reverse, member, assoc, and shared structure.

Procedures, lists, and text

The higher-order procedures article passes procedures to map, for-each, filter, and apply, writes folds, and builds new procedures with compose and SRFI 26 cut. Closures keep state between calls in counters, generators, private accounts, callbacks, and memoized procedures. The SRFI 1 list library article covers iota, take, fold, reduce, any, every, list sets, and unfold, and association lists adds, updates, groups, and nests key and value pairs. Strings creates, joins, compares, searches, and splits text, and characters converts, classifies, and compares Unicode characters and uses character sets.

Data structures

The symbols article explains interning and uses symbols as enumeration values, keys, and tags, with unique symbols from gensym. Vectors covers indexed access, conversions, the SRFI 43 vector library, and matrices, and bytevectors works with raw bytes, UTF-8 text, endianness, and binary headers. The hash tables article counts and groups data with Guile's tables and SRFI 69. Records defines types with define-record-type, and quasiquote builds lists, vectors, and code from templates.

Syntax extension

The macros with syntax-rules article defines new syntax with patterns, templates, the ellipsis, and literals, builds loops, and explains hygiene. Advanced macros writes transformers with syntax-case, computes code at expansion time, generates identifiers, and covers syntax parameters and Guile's define-macro. Pattern matching takes lists, tagged data, records, and vectors apart with (ice-9 match).

Control flow

The continuations article captures the rest of a computation with call/cc to escape from loops, build generators, and search by backtracking, and covers dynamic-wind and Guile's prompts. Multiple values returns several results with values and receives them with call-with-values, receive, let-values, and define-values. Exceptions signals and catches errors with error and guard, writes handlers, defines exception types, and shows Guile's catch and throw.

Modules and I/O

The modules and libraries article loads library modules, writes modules with define-module, resolves name conflicts, and covers R7RS define-library. Ports and output writes to ports, string ports, and the standard error, and fills templates with format. Reading input reads lines, characters, and Scheme data until the end of input, and files writes, reads, appends, and deletes text and binary files.

The system

Command-line arguments reads arguments, sets the exit status, parses options with getopt-long, and turns a script into a command. Environment and processes reads and changes environment variables, runs other programs, and talks to them through pipes. Directories creates, lists, and walks directory trees, and regular expressions searches, extracts, and replaces text with the (ice-9 regex) module.

Applied text and data

String processing trims, tokenizes, pads, and wraps text, recognises palindromes, and parses CSV lines with quoted fields. Number formatting prints numbers in fields, with grouped digits, decimal places, and other bases, aligns them in columns, and explains rounding. Streams and lazy evaluation creates promises with delay, builds infinite streams by hand and with SRFI 41, and reads a file lazily. Structured data in lists queries rows and property lists, joins tables, and writes, walks, and updates JSON-like trees.

Algorithms and data structures

Sorting writes insertion sort, quicksort, and merge sort, counts their comparisons, and uses Guile's sort and stable-sort. Searching covers linear and binary search, lower and upper bounds, and binary search for numeric answers. Stacks, queues and trees builds functional stacks and queues, a mutable queue, and binary search trees with their traversals. Object-oriented programming with closures builds objects that answer messages, inheritance by delegation, polymorphism, and prototype objects.

Libraries and concurrency

GOOPS introduces Guile's object system: classes, slots, generic functions, inheritance, next-method, and multiple dispatch. Date and time covers the current time, strftime and strptime, time zones, elapsed time, and SRFI 19 date arithmetic. Random numbers explains random states and seeds, shuffling, distributions, and Monte Carlo simulation. Threads starts and joins threads, protects shared data with mutexes and condition variables, and computes in parallel with par-map and futures.

Projects and testing

A small Scheme interpreter writes a metacircular evaluator in steps: environments of frames, special forms, closures, a table of special forms, definitions and recursion, derived forms, and a runner for program files. Unit testing uses SRFI 64 to test a module: assertions, testing for errors, test groups, table-driven tests, the exit status, skipped tests and expected failures, and a custom test runner.

Every example in these tutorials is run with GNU Guile 3.0.7 before its output is shown.

last modified November 25, 2026