Scheme records
last modified November 24, 2026 Guile 3.0
A record is a value with a fixed set of named fields, like a
structure in C or a simple class without methods. A list such as
(3 4) can hold the coordinates of a point, but nothing in it says
which number is which, and any other two-element list looks the same. A
record type gives the data a name, a constructor, a type test, and an
accessor for every field. Records are defined with
define-record-type from SRFI 9, which R7RS adopted. The examples
run with GNU Guile 3.0.7.
Defining a record type
define-record-type takes the name of the type, a constructor
with its fields, the name of a predicate, and a description of each field. In
a Guile 3.0.7 script it is not bound by default; the module
(srfi srfi-9) provides it. Type names are written in angle
brackets by convention.
(use-modules (srfi srfi-9))
(define-record-type <point>
(make-point x y)
point?
(x point-x)
(y point-y))
(define p (make-point 3 4))
(write p)
(newline)
(write (list (point-x p) (point-y p)))
(newline)
(write (list (point? p) (point? (list 3 4)) (point? 5)))
(newline)
(define (distance a b)
(let ((dx (- (point-x b) (point-x a)))
(dy (- (point-y b) (point-y a))))
(sqrt (+ (* dx dx) (* dy dy)))))
(write (distance (make-point 0 0) p))
(newline)
The definition creates four procedures: make-point builds a
point from two values, point? tests for a point, and
point-x and point-y read the fields. Guile prints a
record with its type name and its fields. A list with the same numbers is not
a point. distance works with points through the accessors only,
so it does not depend on how a point is stored.
$ guile defining.scm #<<point> x: 3 y: 4> (3 4) (#t #f #f) 5
Modifiers
A field description can name a third procedure, a modifier, which changes the field in place. Fields without a modifier are read-only.
(use-modules (srfi srfi-9))
(define-record-type <account>
(make-account owner balance)
account?
(owner account-owner)
(balance account-balance set-account-balance!))
(define (deposit! acc amount)
(set-account-balance! acc (+ (account-balance acc) amount)))
(define (withdraw! acc amount)
(if (> amount (account-balance acc))
#f
(begin
(set-account-balance! acc (- (account-balance acc) amount))
#t)))
(define acc (make-account "Mária" 100))
(deposit! acc 50)
(write (account-balance acc))
(newline)
(write (list (withdraw! acc 30) (withdraw! acc 500)))
(newline)
(write acc)
(newline)
The owner of an account cannot change, but the balance can, through
set-account-balance!. deposit! adds to the balance,
and withdraw! refuses to take more than the account holds and
reports the result with a boolean. The record printed at the end shows the
final balance of 120.
$ guile modifiers.scm 150 (#t #f) #<<account> owner: "Mária" balance: 120>
Constructor fields
The constructor does not have to take every field. Fields left out of the constructor have no defined initial value and must be set with a modifier. A wrapper procedure can supply sensible defaults.
(use-modules (srfi srfi-9))
(define-record-type <task>
(new-task title)
task?
(title task-title)
(done task-done? set-task-done!)
(priority task-priority set-task-priority!))
(define raw (new-task "raw"))
(write (list (task-done? raw) (task-priority raw)))
(newline)
(define* (make-task title #:key (priority 'normal))
(let ((t (new-task title)))
(set-task-done! t #f)
(set-task-priority! t priority)
t))
(define t1 (make-task "write report"))
(define t2 (make-task "fix login bug" #:priority 'high))
(set-task-done! t1 #t)
(write t1)
(newline)
(write t2)
(newline)
new-task takes only the title. Guile sets the other fields to
#f, but the standard leaves them unspecified, so the program
should not rely on it. make-task, written with
define* from the procedures
article, fills in both fields and accepts the priority as an optional keyword
argument.
$ guile constructor.scm (#f #f) #<<task> title: "write report" done: #t priority: normal> #<<task> title: "fix login bug" done: #f priority: high>
Lists of records
Records combine well with the list procedures. Accessors are ordinary
procedures, so they can be passed directly to map, and
predicates over records work with filter,
fold, and reduce.
(use-modules (srfi srfi-1)
(srfi srfi-9))
(define-record-type <book>
(make-book title author year price)
book?
(title book-title)
(author book-author)
(year book-year)
(price book-price))
(define library
(list (make-book "Dune" "Frank Herbert" 1965 9.5)
(make-book "Neuromancer" "William Gibson" 1984 11.0)
(make-book "Solaris" "Stanislaw Lem" 1961 8.25)
(make-book "Hyperion" "Dan Simmons" 1989 12.0)))
(write (map book-title library))
(newline)
(define old (filter (lambda (b) (< (book-year b) 1970)) library))
(write (map book-title old))
(newline)
(write (fold + 0 (map book-price library)))
(newline)
(define newest
(reduce (lambda (b best) (if (> (book-year b) (book-year best)) b best))
#f
library))
(display (book-title newest))
(display " by ")
(display (book-author newest))
(newline)
(map book-title library) extracts one field from every book.
filter keeps the books published before 1970, and
fold from SRFI 1 adds up the
prices. reduce compares the books pairwise and keeps the one
with the later year. With records, the code names the fields it uses instead
of taking the third element of a list.
$ guile collections.scm
("Dune" "Neuromancer" "Solaris" "Hyperion")
("Dune" "Solaris")
40.75
Hyperion by Dan Simmons
Type checking
Every record type is distinct from the other types, even from a record type with the same number of fields. Predicates tell them apart, and accessors check the type of their argument.
(use-modules (srfi srfi-9))
(define-record-type <point>
(make-point x y)
point?
(x point-x)
(y point-y))
(define-record-type <size>
(make-size width height)
size?
(width size-width)
(height size-height))
(define (describe value)
(cond ((point? value) "a point")
((size? value) "a size")
((pair? value) "a list")
(else "something else")))
(write (map describe (list (make-point 1 2) (make-size 1 2) (list 1 2) 12)))
(newline)
(write (point-x (make-point 1 2)))
(newline)
(write (point-x (make-size 1 2)))
(newline)
describe recognises points and sizes, although both have two
numeric fields, and distinguishes them from a list. Calling
point-x on a size is an error: the program stops with a wrong
type message that shows the offending record. A list-based point would have
returned the width without complaint. The backtrace is omitted from the
output.
$ guile checking.scm
("a point" "a size" "a list" "something else")
1
checking.scm:25:7: In procedure point-x: Wrong type argument: #<<size> width: 1 height: 2>
Record equality
Each call to a constructor creates a new record, so eq? and
eqv? distinguish two records with the same field values. In
Guile, equal? compares records field by field, but R7RS leaves
this unspecified, and portable code defines its own comparison.
(use-modules (srfi srfi-9))
(define-record-type <colour>
(make-colour red green blue)
colour?
(red colour-red)
(green colour-green)
(blue colour-blue))
(define (grey level)
(make-colour level level level))
(define a (grey 128))
(define b (grey 128))
(define c (grey 64))
(write (list (eq? a a) (eq? a b) (eqv? a b)))
(newline)
(write (list (equal? a b) (equal? a c)))
(newline)
(define (colour=? x y)
(and (= (colour-red x) (colour-red y))
(= (colour-green x) (colour-green y))
(= (colour-blue x) (colour-blue y))))
(write (list (colour=? a b) (colour=? a c)))
(newline)
a and b are two grey colours with the same
components, created by separate calls. They are not the same object, so
eq? and eqv? return false, while Guile's
equal? returns true. colour=? states the comparison
explicitly and works in any Scheme. It can also define equality differently,
for example to ignore some fields.
$ guile equality.scm (#t #f #f) (#t #f) (#t #f)
Custom printing
The default printed form shows every field, which is not always the most
useful form. The Guile module (srfi srfi-9 gnu) provides
set-record-type-printer!, which installs a procedure that prints
records of a type to a port.
(use-modules (srfi srfi-9)
(srfi srfi-9 gnu))
(define-record-type <money>
(make-money amount currency)
money?
(amount money-amount)
(currency money-currency))
(define price (make-money 1250 'EUR))
(write price)
(newline)
(set-record-type-printer!
<money>
(lambda (m port)
(display "#<money " port)
(display (quotient (money-amount m) 100) port)
(display (if (< (remainder (money-amount m) 100) 10) ".0" ".") port)
(display (remainder (money-amount m) 100) port)
(display " " port)
(display (money-currency m) port)
(display ">" port)))
(write price)
(newline)
(display (list price (make-money 1907 'CZK)))
(newline)
The amount of money is stored in cents to avoid inexact numbers. The printer
receives the record and a port, and every display call writes to
that port. It formats the amount with two decimal places, adding a zero for
cents below 10. After the printer is installed, both write and
display use it, also for records inside a list.
$ guile printing.scm #<<money> amount: 1250 currency: EUR> #<money 12.50 EUR> (#<money 12.50 EUR> #<money 19.07 CZK>)
Immutable records
Guile's define-immutable-record-type, also from
(srfi srfi-9 gnu), defines a record type whose fields cannot
change. Instead of modifiers, it creates functional setters, which
return a new record with one field replaced.
(use-modules (srfi srfi-9 gnu))
(define-immutable-record-type <point>
(make-point x y)
point?
(x point-x set-point-x)
(y point-y set-point-y))
(define origin (make-point 0 0))
(define moved (set-point-x origin 5))
(write origin)
(newline)
(write moved)
(newline)
(define (move p dx dy)
(set-point-y (set-point-x p (+ (point-x p) dx))
(+ (point-y p) dy)))
(define path
(let loop ((p origin) (steps (list (cons 1 0) (cons 0 2) (cons -3 1))) (acc '()))
(if (null? steps)
(reverse (cons p acc))
(loop (move p (car (car steps)) (cdr (car steps)))
(cdr steps)
(cons p acc)))))
(write (map (lambda (p) (list (point-x p) (point-y p))) path))
(newline)
set-point-x returns a new point and leaves origin
unchanged. move chains two functional setters to shift a point.
The named let applies a list of steps and collects every
position in path; since no point is ever modified, each element
of the path keeps its own coordinates.
$ guile immutable.scm #<<point> x: 0 y: 0> #<<point> x: 5 y: 0> ((0 0) (1 0) (1 2) (-2 3))
Nested records
A field may hold another record. A rectangle defined by its corner point and its size is a record that contains a point.
(use-modules (srfi srfi-9))
(define-record-type <point>
(make-point x y)
point?
(x point-x)
(y point-y))
(define-record-type <rect>
(make-rect corner width height)
rect?
(corner rect-corner)
(width rect-width)
(height rect-height))
(define (rect-area r)
(* (rect-width r) (rect-height r)))
(define (rect-contains? r p)
(let ((c (rect-corner r)))
(and (<= (point-x c) (point-x p) (+ (point-x c) (rect-width r)))
(<= (point-y c) (point-y p) (+ (point-y c) (rect-height r))))))
(define room (make-rect (make-point 2 1) 6 4))
(write (rect-area room))
(newline)
(write (point-x (rect-corner room)))
(newline)
(write (map (lambda (p) (rect-contains? room p))
(list (make-point 3 3) (make-point 8 5) (make-point 9 2))))
(newline)
(write room)
(newline)
rect-area uses only the rectangle's own fields, while
rect-contains? reaches into the corner point. The three-argument
<= tests that a coordinate lies between the two edges. Points
on the edge, such as (8, 5), count as inside. Guile prints the nested record
inside the outer one.
$ guile nested.scm 24 2 (#t #t #f) #<<rect> corner: #<<point> x: 2 y: 1> width: 6 height: 4>
Records in a hash table
Records are often stored in a hash table under a unique identifier. The program below keeps a registry of students and their grades.
(use-modules (srfi srfi-9)
(srfi srfi-69))
(define-record-type <student>
(make-student id name grades)
student?
(id student-id)
(name student-name)
(grades student-grades set-student-grades!))
(define registry (make-hash-table))
(define (register! name)
(let* ((id (+ 1 (hash-table-size registry)))
(s (make-student id name '())))
(hash-table-set! registry id s)
id))
(define (add-grade! id grade)
(let ((s (hash-table-ref/default registry id #f)))
(when s
(set-student-grades! s (cons grade (student-grades s))))))
(define (average s)
(let ((g (student-grades s)))
(if (null? g) 0 (exact->inexact (/ (apply + g) (length g))))))
(define eva (register! "Eva"))
(define jan (register! "Ján"))
(add-grade! eva 1)
(add-grade! eva 2)
(add-grade! jan 3)
(add-grade! 99 1)
(for-each (lambda (id)
(let ((s (hash-table-ref/default registry id #f)))
(display (student-name s))
(display " ")
(display (average s))
(newline)))
(list eva jan))
register! assigns the next number as the identifier, stores a
new student with no grades, and returns the identifier.
add-grade! looks up the student and conses the grade onto the
list; a grade for the unknown identifier 99 is ignored. Because the table
holds the record itself, changing the record through
set-student-grades! is visible in later lookups.
average returns an inexact mean of the grades.
$ guile registry.scm Eva 1.5 Ján 3.0
Source
SRFI-9 Records — GNU Guile Reference Manual
In this article we have defined record types with constructors, predicates, accessors, and modifiers, supplied default field values, processed lists of records, met the type error of an accessor, compared records, installed a custom printer, created immutable records with functional setters, nested records, and stored records in a hash table. The symbols article shows the simpler tagged lists that records replace, and the association lists article covers another way to name fields.
Author
List all Scheme tutorials.