Chapter 7. Operations on Objects

This chapter describes operations specific to Chez Scheme on nonnumeric objects, including standard objects such as pairs and numbers and Chez Scheme extensions such as boxes and records. Chapter 8 describes operations on numbers. See Chapter 6 of The Scheme Programming Language, 4th Edition or the Revised6 Report on Scheme for a description of standard operations on objects.

Section 7.1. Missing R6RS Type Predicates

procedure: (enum-set? obj)
returns: #t if obj is an enum set, #f otherwise
libraries: (chezscheme)

This predicate is not defined by the Revised6 Report, but should be.

procedure: (record-constructor-descriptor? obj)
returns: #t if obj is a record constructor descriptor, #f otherwise
libraries: (chezscheme)

This predicate is not defined by the Revised6 Report, but should be.

Section 7.2. Pairs and Lists

procedure: (atom? obj)
returns: #t if obj is not a pair, #f otherwise
libraries: (chezscheme)

atom? is equivalent to (lambda (x) (not (pair? x))).

(atom? '(a b c)) <graphic> #f
(atom? '(3 . 4)) <graphic> #f
(atom? '()) <graphic> #t
(atom? 3) <graphic> #t

procedure: (list-head list n)
returns: a list of the first n elements of list
libraries: (chezscheme)

n must be an exact nonnegative integer less than or equal to the length of list.

list-head and the standard Scheme procedure list-tail may be used together to split a list into two separate lists. While list-tail performs no allocation but instead returns a sublist of the original list, list-head always returns a copy of the first portion of the list.

list-head may be defined as follows.

(define list-head
  (lambda (ls n)
    (if (= n 0)
        '()
        (cons (car ls) (list-head (cdr ls) (- n 1))))))

(list-head '(a b c) 0) <graphic> ()
(list-head '(a b c) 2) <graphic> (a b)
(list-head '(a b c) 3) <graphic> (a b c)
(list-head '(a b c . d) 2) <graphic> (a b)
(list-head '(a b c . d) 3) <graphic> (a b c)
(list-head '#1=(a . #1#) 5) <graphic> (a a a a a)

procedure: (last-pair list)
returns: the last pair of a list
libraries: (chezscheme)

list must not be empty. last-pair returns the last pair (not the last element) of list. list may be an improper list, in which case the last pair is the pair containing the last element and the terminating object.

(last-pair '(a b c d)) <graphic> (d)
(last-pair '(a b c . d)) <graphic> (c . d)

procedure: (list-copy list)
returns: a copy of list
libraries: (chezscheme)

list-copy returns a list equal? to list, using new pairs to reform the top-level list structure.

(list-copy '(a b c)) <graphic> (a b c)

(let ([ls '(a b c)])
  (equal? ls (list-copy ls))) <graphic> #t

(let ([ls '(a b c)])
  (let ([ls-copy (list-copy ls)])
    (or (eq? ls-copy ls)
        (eq? (cdr ls-copy) (cdr ls))
        (eq? (cddr ls-copy) (cddr ls))))) <graphic> #f

procedure: (list* obj ... final-obj)
returns: a list of obj ... terminated by final-obj
libraries: (chezscheme)

list* is identical to the Revised6 Report cons*.

procedure: (make-list n)
procedure: (make-list n obj)
returns: a list of n objs
libraries: (chezscheme)

n must be a nonnegative integer. If obj is omitted, the elements of the list are unspecified.

(make-list 0 '()) <graphic> ()
(make-list 3 0) <graphic> (0 0 0)
(make-list 2 "hi") <graphic> ("hi" "hi")

procedure: (iota n)
returns: a list of integers from 0 (inclusive) to n (exclusive)
libraries: (chezscheme)

n must be an exact nonnegative integer.

(iota 0) <graphic> ()
(iota 5) <graphic> (0 1 2 3 4)

procedure: (enumerate ls)
returns: a list of integers from 0 (inclusive) to the length of ls (exclusive)
libraries: (chezscheme)

(enumerate '()) <graphic> ()
(enumerate '(a b c)) <graphic> (0 1 2)
(let ([ls '(a b c)])
  (map cons ls (enumerate ls))) <graphic> ((a . 0) (b . 1) (c . 2))

procedure: (remq! obj list)
procedure: (remv! obj list)
procedure: (remove! obj list)
returns: a list containing the elements of list with all occurrences of obj removed
libraries: (chezscheme)

These procedures are similar to the Revised6 Report remq, remv, and remove procedures, except remq!, remv! and remove! use pairs from the input list to build the output list. They perform less allocation but are not necessarily faster than their nondestructive counterparts. Their use can easily lead to confusing or incorrect results if used indiscriminately.

(remq! 'a '(a b a c a d)) <graphic> (b c d)

(remv! #\a '(#\a #\b #\c)) <graphic> (#\b #\c)

(remove! '(c) '((a) (b) (c))) <graphic> ((a) (b))

procedure: (substq new old tree)
procedure: (substv new old tree)
procedure: (subst new old tree)
procedure: (substq! new old tree)
procedure: (substv! new old tree)
procedure: (subst! new old tree)
returns: a tree with new substituted for occurrences of old in tree
libraries: (chezscheme)

These procedures traverse tree, replacing all objects equivalent to the object old with the object new.

The equivalence test for substq and substq! is eq?, for substv and substv! is eqv?, and for subst and subst! is equal?.

substq!, substv!, and subst! perform the substitutions destructively. They perform less allocation but are not necessarily faster than their nondestructive counterparts. Their use can easily lead to confusing or incorrect results if used indiscriminately.

(substq 'a 'b '((b c) b a)) <graphic> ((a c) a a)

(substv 2 1 '((1 . 2) (1 . 4) . 1)) <graphic> ((2 . 2) (2 . 4) . 2)

(subst 'a
       '(a . b)
       '((a . b) (c a . b) . c)) <graphic> (a (c . a) . c)

(let ([tr '((b c) b a)])
  (substq! 'a 'b tr)
  tr) <graphic> ((a c) a a)

procedure: (reverse! list)
returns: a list containing the elements of list in reverse order
libraries: (chezscheme)

reverse! destructively reverses list by reversing its links. Using reverse! in place of reverse reduces allocation but is not necessarily faster than reverse. Its use can easily lead to confusing or incorrect results if used indiscriminately.

(reverse! '()) <graphic> ()
(reverse! '(a b c)) <graphic> (c b a)

(let ([x '(a b c)])
    (reverse! x)
    x) <graphic> (a)

(let ([x '(a b c)])
    (set! x (reverse! x))
    x) <graphic> (c b a)

procedure: (append! list ...)
returns: the concatenation of the input lists
libraries: (chezscheme)

Like append, append! returns a new list consisting of the elements of the first list followed by the elements of the second list, the elements of the third list, and so on. Unlike append, append! reuses the pairs in all of the arguments in forming the new list. That is, the last cdr of each list argument but the last is changed to point to the next list argument. If any argument but the last is the empty list, it is essentially ignored. The final argument (which need not be a list) is not altered.

append! performs less allocation than append but is not necessarily faster. Its use can easily lead to confusing or incorrect results if used indiscriminately.

(append! '(a b) '(c d)) <graphic> (a b c d)

(let ([x '(a b)])
    (append! x '(c d))
    x) <graphic> (a b c d)

Section 7.3. Characters

Chez Scheme extends the syntax of characters in two ways. First, a #\ prefix followed by exactly three octal digits is read as a character whose numeric code is the octal value of the three digits, e.g., #\044 is read as #\$. Second, it recognizes several nonstandard named characters: #\rubout (which is the same as #\delete), #\bel (which is the same as #\alarm), #\vt (which is the same as #\vtab), #\nel (the Unicode NEL character), and #\ls (the Unicode LS character). The set of nonstandard character names may be changed via the procedure char-name (page 9.14).

These extensions are disabled in an input stream after #!r6rs has been seen by the reader, unless #!chezscheme has been seen more recently.

procedure: (char=? char1 char2 ...)
procedure: (char<? char1 char2 ...)
procedure: (char>? char1 char2 ...)
procedure: (char<=? char1 char2 ...)
procedure: (char>=? char1 char2 ...)
procedure: (char-ci=? char1 char2 ...)
procedure: (char-ci<? char1 char2 ...)
procedure: (char-ci>? char1 char2 ...)
procedure: (char-ci<=? char1 char2 ...)
procedure: (char-ci>=? char1 char2 ...)
returns: #t if the relation holds, #f otherwise
libraries: (chezscheme)

These predicates are identical to the Revised6 Report counterparts, except they are extended to accept one or more rather than two or more arguments. When passed one argument, each of these predicates returns #t.

(char>? #\a) <graphic> #t
(char<? #\a) <graphic> #t
(char-ci=? #\a) <graphic> #t

procedure: (char- char1 char2)
returns: the integer difference between char1 and char2
libraries: (chezscheme)

char- subtracts the integer value of char2 from the integer value of char1 and returns the difference. The following examples assume that the integer representation is the ASCII code for the character.

(char- #\f #\e) <graphic> 1

(define digit-value
 ; returns the digit value of the base-r digit c, or #f if c
 ; is not a valid digit
  (lambda (c r)
    (let ([v (cond
               [(char<=? #\0 c #\9) (char- c #\0)]
               [(char<=? #\A c #\Z) (char- c #\7)]
               [(char<=? #\a c #\z) (char- c #\W)]
               [else 36])])
      (and (fx< v r) v))))
(digit-value #\8 10) <graphic> 8
(digit-value #\z 10) <graphic> #f
(digit-value #\z 36) <graphic> 35

char- might be defined as follows.

(define char-
  (lambda (c1 c2)
    (- (char->integer c1) (char->integer c2))))

Section 7.4. Strings

Chez Scheme extends the standard string syntax with two character escapes: \', which produces the single quote character, and \nnn, i.e., backslash followed by 3 octal digits, which produces the character equivalent of the octal value of the 3 digits. These extensions are disabled in an input stream after #!r6rs has been seen by the reader, unless #!chezscheme has been seen more recently.

All strings are mutable by default, including constants. A program can create immutable strings via string->immutable-string. Any attempt to modify an immutable string causes an exception to be raised.

The length and indices of a string in Chez Scheme are always fixnums.

procedure: (string=? string1 string2 string3 ...)
procedure: (string<? string1 string2 string3 ...)
procedure: (string>? string1 string2 string3 ...)
procedure: (string<=? string1 string2 string3 ...)
procedure: (string>=? string1 string2 string3 ...)
procedure: (string-ci=? string1 string2 string3 ...)
procedure: (string-ci<? string1 string2 string3 ...)
procedure: (string-ci>? string1 string2 string3 ...)
procedure: (string-ci<=? string1 string2 string3 ...)
procedure: (string-ci>=? string1 string2 string3 ...)
returns: #t if the relation holds, #f otherwise
libraries: (chezscheme)

These predicates are identical to the Revised6 Report counterparts, except they are extended to accept one or more rather than two or more arguments. When passed one argument, each of these predicates returns #t.

(string>? "a") <graphic> #t
(string<? "a") <graphic> #t
(string-ci=? "a") <graphic> #t

procedure: (string-copy! src src-start dst dst-start n)
returns: unspecified
libraries: (chezscheme)

src and dst must be strings, and dst must be mutable. src-start, dst-start, and n must be exact nonnegative integers. The sum of src-start and n must not exceed the length of src, and the sum of dst-start and n must not exceed the length of dst.

string-copy! overwrites the n bytes of dst starting at dst-start with the n bytes of dst starting at src-start. This works even if dst is the same string as src and the source and destination locations overlap. That is, the destination is filled with the characters that appeared at the source before the operation began.

(define s1 "to boldly go")
(define s2 (make-string 10 #\-))

(string-copy! s1 3 s2 1 3)
s2 <graphic> "-bol------"
 
(string-copy! s1 7 s2 4 2)
s2 <graphic> "-bolly----"
 
(string-copy! s2 2 s2 5 4)
s2 <graphic> "-bollolly-"

procedure: (substring-fill! string start end char)
returns: unspecified
libraries: (chezscheme)

string must be mutable. The characters of string from start (inclusive) to end (exclusive) are set to char. start and end must be nonnegative integers; start must be strictly less than the length of string, while end may be less than or equal to the length of string. If endstart, the string is left unchanged.

(let ([str (string-copy "a tpyo typo")])
  (substring-fill! str 2 6 #\X)
  str) <graphic> "a XXXX typo"

procedure: (string-truncate! string n)
returns: string or the empty string
libraries: (chezscheme)

string must be mutable. n must be an exact nonnegative fixnum not greater than the length of string. If n is zero, string-truncate! returns the empty string. Otherwise, string-truncate! destructively truncates string to its first n characters and returns string.

(define s (make-string 7 #\$))
(string-truncate! s 0) <graphic> ""
<graphic> "$$$$$$$"
(string-truncate! s 3) <graphic> "$$$"
<graphic> "$$$"

procedure: (mutable-string? obj)
returns: #t if obj is a mutable string, #f otherwise
procedure: (immutable-string? obj)
returns: #t if obj is an immutable string, #f otherwise
libraries: (chezscheme)

(mutable-string? (string #\a #\b #\c)) <graphic> #t
(mutable-string? (string->immutable-string "abc")) <graphic> #f
(immutable-string? (string #\a #\b #\c)) <graphic> #f
(immutable-string? (string->immutable-string "abc")) <graphic> #t
(immutable-string? (cons 3 4)) <graphic> #f

procedure: (string->immutable-string string)
returns: an immutable string equal to string
libraries: (chezscheme)

The result is string itself if string is immutable; otherwise, the result is an immutable string with the same content as string.

(define s (string->immutable-string (string #\x #\y #\z)))
(string-set! s 0 #\a) <graphic> exception: not mutable

Section 7.5. Vectors

Chez Scheme extends the syntax of vectors to allow the length of the vector to be specified between the # and open parenthesis, e.g., #3(a b c). If fewer elements are supplied in the syntax than the specified length, each element after the last printed element is the same as the last printed element. This extension is disabled in an input stream after #!r6rs has been seen by the reader, unless #!chezscheme has been seen more recently.

The length and indices of a vector in Chez Scheme are always fixnums.

All vectors are mutable by default, including constants. A program can create immutable vectors via vector->immutable-vector. Any attempt to modify an immutable vector causes an exception to be raised.

procedure: (vector-copy vector)
returns: a copy of vector
libraries: (chezscheme)

vector-copy creates a new vector of the same length and contents as vector. The elements themselves are not copied.

(vector-copy '#(a b c)) <graphic> #(a b c)

(let ([v '#(a b c)])
  (eq? v (vector-copy v))) <graphic> #f

procedure: (vector-set-fixnum! vector n fixnum)
returns: unspecified
libraries: (chezscheme)

vector must be mutable. vector-set-fixnum! changes the nth element of vector to fixnum. n must be an exact nonnegative integer strictly less than the length of vector.

It is faster to store a fixnum than an arbitrary value, since for arbitrary values, the system has to record potential assignments from older to younger objects to support generational garbage collection. Care must be taken to ensure that the argument is indeed a fixnum, however; otherwise, the collector may not properly track the assignment. The primitive performs a fixnum check on the argument except at optimization level 3.

See also the description of fixnum-only vectors (fxvectors) below.

(let ([v (vector 1 2 3 4 5)])
  (vector-set-fixnum! v 2 73)
  v) <graphic> #(1 2 73 4 5)

procedure: (vector-cas! vector n old-obj new-obj)
returns: #t if vector is changed, #f otherwise
libraries: (chezscheme)

vector must be mutable. vector-cas! atomically changes the nth element of vector to new-obj if the replaced nth element is eq? to old-obj. If the nth element of vector that would be replaced is not eq? to old-obj, then vector is unchanged.

(define v (vector 'old0 'old1 'old2))
(vector-cas! v 1 'old1 'new1) <graphic> #t
(vector-ref v 1) <graphic> 'new1
(vector-cas! v 2 'old1 'new2) <graphic> #f
(vector-ref v 2) <graphic> 'old2

procedure: (mutable-vector? obj)
returns: #t if obj is a mutable vector, #f otherwise
procedure: (immutable-vector? obj)
returns: #t if obj is an immutable vector, #f otherwise
libraries: (chezscheme)

(mutable-vector? (vector 1 2 3)) <graphic> #t
(mutable-vector? (vector->immutable-vector (vector 1 2 3))) <graphic> #f
(immutable-vector? (vector 1 2 3)) <graphic> #f
(immutable-vector? (vector->immutable-vector (vector 1 2 3))) <graphic> #t
(immutable-vector? (cons 3 4)) <graphic> #f

procedure: (vector->immutable-vector vector)
returns: an immutable vector equal to vector
libraries: (chezscheme)

The result is vector itself if vector is immutable; otherwise, the result is an immutable vector with the same content as vector.

(define v (vector->immutable-vector (vector 1 2 3)))
(vector-set! v 0 0) <graphic> exception: not mutable

thread parameter: self-evaluating-vectors
libraries: (chezscheme)

The default value of this parameter is #f, meaning that vector literals must be quoted, as required by the Revised6 Report. Setting self-evaluating-vectors to a true value may be useful to provide compatibility with R7RS, as the latter states that vectors are self-evaluating.

#(a b c) <graphic> exception: invalid syntax

(self-evaluating-vectors #t)
#(a b c) <graphic> #(a b c)

Section 7.6. Fixnum-Only Vectors

Fixnum-only vectors, or "fxvectors," are like vectors but contain only fixnums. Fxvectors are written with the #vfx prefix in place of the # prefix for vectors, e.g., #vfx(1 2 3) or #10vfx(2). The fxvector syntax is disabled in an input stream after #!r6rs has been seen by the reader, unless #!chezscheme has been seen more recently.

The length and indices of an fxvector are always fixnums.

Updating an fxvector is generally less expensive than updating a vector, since for vectors, the system records potential assignments from older to younger objects to support generational garbage collection. The storage management system also takes advantage of the fact that fxvectors contain no pointers to place them in an area of memory that does not have to be traced during collection.

All fxvectors are mutable by default, including constants. A program can create immutable fxvectors via fxvector->immutable-fxvector. Any attempt to modify an immutable fxvector causes an exception to be raised.

See also vector-set-fixnum! above.

procedure: (fxvector? obj)
returns: #t if obj is an fxvector, #f otherwise
libraries: (chezscheme)

(fxvector? #vfx()) <graphic> #t
(fxvector? #vfx(1 2 3)) <graphic> #t
(fxvector? (fxvector 1 2 3)) <graphic> #t
(fxvector? '#(a b c)) <graphic> #f
(fxvector? '(a b c)) <graphic> #f
(fxvector? "abc") <graphic> #f

procedure: (fxvector fixnum ...)
returns: an fxvector of the fixnums fixnum ...
libraries: (chezscheme)

(fxvector) <graphic> #vfx()
(fxvector 1 3 5) <graphic> #vfx(1 3 5)

procedure: (make-fxvector n)
procedure: (make-fxvector n fixnum)
returns: an fxvector of length n
libraries: (chezscheme)

n must be a fixnum. If fixnum is supplied, each element of the fxvector is initialized to fixnum; otherwise, the elements are unspecified.

(make-fxvector 0) <graphic> #vfx()
(make-fxvector 0 7) <graphic> #vfx()
(make-fxvector 5 7) <graphic> #vfx(7 7 7 7 7)

procedure: (fxvector-length fxvector)
returns: the number of elements in fxvector
libraries: (chezscheme)

(fxvector-length #vfx()) <graphic> 0
(fxvector-length #vfx(1 2 3)) <graphic> 3
(fxvector-length #10vfx(1 2 3)) <graphic> 10
(fxvector-length (fxvector 1 2 3 4)) <graphic> 4
(fxvector-length (make-fxvector 300)) <graphic> 300

procedure: (fxvector-ref fxvector n)
returns: the nth element (zero-based) of fxvector
libraries: (chezscheme)

n must be a nonnegative fixnum strictly less than the length of fxvector.

(fxvector-ref #vfx(-1 2 4 7) 0) <graphic> -1
(fxvector-ref #vfx(-1 2 4 7) 1) <graphic> 2
(fxvector-ref #vfx(-1 2 4 7) 3) <graphic> 7

procedure: (fxvector-set! fxvector n fixnum)
returns: unspecified
libraries: (chezscheme)

fxvector must be mutable. n must be a nonnegative fixnum strictly less than the length of fxvector. fxvector-set! changes the nth element of fxvector to fixnum.

(let ([v (fxvector 1 2 3 4 5)])
  (fxvector-set! v 2 (fx- (fxvector-ref v 2)))
  v) <graphic> #vfx(1 2 -3 4 5)

procedure: (fxvector-fill! fxvector fixnum)
returns: unspecified
libraries: (chezscheme)

fxvector must be mutable. fxvector-fill! replaces each element of fxvector with fixnum.

(let ([v (fxvector 1 2 3)])
  (fxvector-fill! v 0)
  v) <graphic> #vfx(0 0 0)

procedure: (fxvector->list fxvector)
returns: a list of the elements of fxvector
libraries: (chezscheme)

(fxvector->list (fxvector)) <graphic> ()
(fxvector->list #vfx(7 5 2)) <graphic> (7 5 2)

(let ([v #vfx(1 2 3 4 5)])
  (apply fx* (fxvector->list v))) <graphic> 120

procedure: (list->fxvector list)
returns: an fxvector of the elements of list
libraries: (chezscheme)

list must consist entirely of fixnums.

(list->fxvector '()) <graphic> #vfx()
(list->fxvector '(3 5 7)) <graphic> #vfx(3 5 7)

(let ([v #vfx(1 2 3 4 5)])
  (let ([ls (fxvector->list v)])
    (list->fxvector (map fx* ls ls)))) <graphic> #vfx(1 4 9 16 25)

procedure: (fxvector-copy fxvector)
returns: a copy of fxvector
libraries: (chezscheme)

fxvector-copy creates a new fxvector with the same length and contents as fxvector.

(fxvector-copy #vfx(3 4 5)) <graphic> #vfx(3 4 5)

(let ([v #vfx(3 4 5)])
  (eq? v (fxvector-copy v))) <graphic> #f

procedure: (mutable-fxvector? obj)
returns: #t if obj is a mutable fxvector, #f otherwise
procedure: (immutable-fxvector? obj)
returns: #t if obj is an immutable fxvector, #f otherwise
libraries: (chezscheme)

(mutable-fxvector? (fxvector 1 2 3)) <graphic> #t
(mutable-fxvector? (fxvector->immutable-fxvector (fxvector 1 2 3))) <graphic> #f
(immutable-fxvector? (fxvector 1 2 3)) <graphic> #f
(immutable-fxvector? (fxvector->immutable-fxvector (fxvector 1 2 3))) <graphic> #t
(immutable-fxvector? (cons 3 4)) <graphic> #f

procedure: (fxvector->immutable-fxvector fxvector)
returns: either an immutable copy of fxvector or fxvector itself
libraries: (chezscheme)

The result is fxvector itself if fxvector is immutable; otherwise, the result is an immutable fxvector with the same content as fxvector.

(define v (fxvector->immutable-fxvector (fxvector 1 2 3)))
(fxvector-set! v 0 0) <graphic> exception: not mutable

Section 7.7. Bytevectors

As with vectors, Chez Scheme extends the syntax of bytevectors to allow the length of the vector to be specified between the # and open parenthesis, e.g., #3vu8(1 105 73). If fewer elements are supplied in the syntax than the specified length, each element after the last printed element is the same as the last printed element. This extension is disabled in an input stream after #!r6rs has been seen by the reader, unless #!chezscheme has been seen more recently.

Chez Scheme also extends the set of bytevector primitives, including primitives for loading and storing 3, 5, 6, and 7-byte quantities.

The length and indices of a bytevector in Chez Scheme are always fixnums.

All bytevectors are mutable by default, including constants. A program can create immutable bytevectors via bytevector->immutable-bytevector. Any attempt to modify an immutable bytevector causes an exception to be raised.

procedure: (bytevector fill ...)
returns: a new bytevector containing fill ...
libraries: (chezscheme)

Each fill value must be an exact integer representing a signed or unsigned 8-bit value, i.e., a value in the range -128 to 255 inclusive. A negative fill value is treated as its two's complement equivalent.

(bytevector) <graphic> #vu8()
(bytevector 1 3 5) <graphic> #vu8(1 3 5)
(bytevector -1 -3 -5) <graphic> #vu8(255 253 251)

procedure: (bytevector->s8-list bytevector)
returns: a new list of the 8-bit signed elements of bytevector
libraries: (chezscheme)

The values in the returned list are exact eight-bit signed integers, i.e., values in the range -128 to 127 inclusive. bytevector->s8-list is similar to the Revised6 Report bytevector->u8-list except the values in the returned list are signed rather than unsigned.

(bytevector->s8-list (make-bytevector 0)) <graphic> ()
(bytevector->s8-list #vu8(1 127 128 255)) <graphic> (1 127 -128 -1)

(let ([v #vu8(1 2 3 255)])
  (apply * (bytevector->s8-list v))) <graphic> -6

procedure: (s8-list->bytevector list)
returns: a new bytevector of the elements of list
libraries: (chezscheme)

list must consist entirely of exact eight-bit signed integers, i.e., values in the range -128 to 127 inclusive. s8-list->bytevector is similar to the Revised6 Report procedure u8-list->bytevector, except the elements of the input list are signed rather than unsigned.

(s8-list->bytevector '()) <graphic> #vu8()
(s8-list->bytevector '(1 127 -128 -1)) <graphic> #vu8(1 127 128 255)

(let ([v #vu8(1 2 3 4 5)])
  (let ([ls (bytevector->s8-list v)])
    (s8-list->bytevector (map - ls)))) <graphic> #vu8(255 254 253 252 251)

procedure: (bytevector-truncate! bytevector n)
returns: bytevector or the empty bytevector
libraries: (chezscheme)

bytevector must be mutable. n must be an exact nonnegative fixnum not greater than the length of bytevector. If n is zero, bytevector-truncate! returns the empty bytevector. Otherwise, bytevector-truncate! destructively truncates bytevector to its first n bytes and returns bytevector.

(define bv (make-bytevector 7 19))
(bytevector-truncate! bv 0) <graphic> #vu8()
bv <graphic> #vu8(19 19 19 19 19 19 19)
(bytevector-truncate! bv 3) <graphic> #vu8(19 19 19)
bv <graphic> #vu8(19 19 19)

procedure: (bytevector-u24-ref bytevector n eness)
returns: the 24-bit unsigned integer at index n (zero-based) of bytevector
procedure: (bytevector-s24-ref bytevector n eness)
returns: the 24-bit signed integer at index n (zero-based) of bytevector
procedure: (bytevector-u40-ref bytevector n eness)
returns: the 40-bit unsigned integer at index n (zero-based) of bytevector
procedure: (bytevector-s40-ref bytevector n eness)
returns: the 40-bit signed integer at index n (zero-based) of bytevector
procedure: (bytevector-u48-ref bytevector n eness)
returns: the 48-bit unsigned integer at index n (zero-based) of bytevector
procedure: (bytevector-s48-ref bytevector n eness)
returns: the 48-bit signed integer at index n (zero-based) of bytevector
procedure: (bytevector-u56-ref bytevector n eness)
returns: the 56-bit unsigned integer at index n (zero-based) of bytevector
procedure: (bytevector-s56-ref bytevector n eness)
returns: the 56-bit signed integer at index n (zero-based) of bytevector
libraries: (chezscheme)

n must be an exact nonnegative integer and indexes the starting byte of the value. The sum of n and the number of bytes occupied by the value (3 for 24-bit values, 5 for 40-bit values, 6 for 48-bit values, and 7 for 56-bit values) must not exceed the length of bytevector. eness must be a valid endianness symbol naming the endianness.

The return value is an exact integer in the appropriate range for the number of bytes occupied by the value. Signed values are the equivalent of the stored value treated as a two's complement value.

procedure: (bytevector-u24-set! bytevector n u24 eness)
procedure: (bytevector-s24-set! bytevector n s24 eness)
procedure: (bytevector-u40-set! bytevector n u40 eness)
procedure: (bytevector-s40-set! bytevector n s40 eness)
procedure: (bytevector-u48-set! bytevector n u48 eness)
procedure: (bytevector-s48-set! bytevector n s48 eness)
procedure: (bytevector-u56-set! bytevector n u56 eness)
procedure: (bytevector-s56-set! bytevector n s56 eness)
returns: unspecified
libraries: (chezscheme)

bytevector must be mutable. n must be an exact nonnegative integer and indexes the starting byte of the value. The sum of n and the number of bytes occupied by the value must not exceed the length of bytevector. u24 must be a 24-bit unsigned value, i.e., a value in the range 0 to 224 - 1 inclusive; s24 must be a 24-bit signed value, i.e., a value in the range -223 to 223 - 1 inclusive; u40 must be a 40-bit unsigned value, i.e., a value in the range 0 to 240 - 1 inclusive; s40 must be a 40-bit signed value, i.e., a value in the range -239 to 239 - 1 inclusive; u48 must be a 48-bit unsigned value, i.e., a value in the range 0 to 248 - 1 inclusive; s48 must be a 48-bit signed value, i.e., a value in the range -247 to 247 - 1 inclusive; u56 must be a 56-bit unsigned value, i.e., a value in the range 0 to 256 - 1 inclusive; and s56 must be a 56-bit signed value, i.e., a value in the range -255 to 255 - 1 inclusive. eness must be a valid endianness symbol naming the endianness.

These procedures store the given value in the 3, 5, 6, or 7 bytes starting at index n (zero-based) of bytevector. Negative values are stored as their two's complement equivalent.

procedure: (mutable-bytevector? obj)
returns: #t if obj is a mutable bytevector, #f otherwise
procedure: (immutable-bytevector? obj)
returns: #t if obj is an immutable bytevector, #f otherwise
libraries: (chezscheme)

(mutable-bytevector? (bytevector 1 2 3)) <graphic> #t
(mutable-bytevector?
  (bytevector->immutable-bytevector (bytevector 1 2 3))) <graphic> #f
(immutable-bytevector? (bytevector 1 2 3)) <graphic> #f
(immutable-bytevector?
  (bytevector->immutable-bytevector (bytevector 1 2 3))) <graphic> #t
(immutable-bytevector? (cons 3 4)) <graphic> #f

procedure: (bytevector->immutable-bytevector bytevector)
returns: an immutable bytevector equal to bytevector
libraries: (chezscheme)

The result is bytevector itself if bytevector is immutable; otherwise, the result is an immutable bytevector with the same content as bytevector.

(define bv (bytevector->immutable-bytevector (bytevector 1 2 3)))
(bytevector-u8-set! bv 0 0) <graphic> exception: not mutable

procedure: (bytevector-compress bytevector)
returns: a new bytevector containing compressed content of bytevector
libraries: (chezscheme)

The result is the raw compressed data with a minimal header to record the uncompressed size and the compression mode. The result does not include the header that is written by port-based compression using the compressed option. The compression format is determined by the compress-format parameter, and the compression level is determined by the compress-level parameter.

procedure: (bytevector-uncompress bytevector)
returns: a bytevector containing uncompressed content of bytevector
libraries: (chezscheme)

Uncompresses a bytevector produced by bytevector-compress to a new bytevector with the same content as the original given to bytevector-compress.

Section 7.8. Boxes

Boxes are single-cell objects that are primarily useful for providing an "extra level of indirection." This extra level of indirection is typically used to allow more than one body of code or data structure to share a reference, or pointer, to an object. For example, boxes may be used to implement call-by-reference semantics in an interpreter for a language employing this parameter passing discipline.

Boxes are written with the prefix #& (pronounced "hash-ampersand"). For example, #&(a b c) is a box holding the list (a b c). The box syntax is disabled in an input stream after #!r6rs has been seen by the reader, unless #!chezscheme has been seen more recently.

All boxes are mutable by default, including constants. A program can create immutable boxes via box-immutable. Any attempt to modify an immutable box causes an exception to be raised.

procedure: (box? obj)
returns: #t if obj is a box, #f otherwise
libraries: (chezscheme)

(box? '#&a) <graphic> #t
(box? 'a) <graphic> #f
(box? (box 3)) <graphic> #t

procedure: (box obj)
returns: a new box containing obj
libraries: (chezscheme)

(box 'a) <graphic> #&a
(box (box '(a b c))) <graphic> #&#&(a b c)

procedure: (unbox box)
returns: contents of box
libraries: (chezscheme)

(unbox #&a) <graphic> a
(unbox #&#&(a b c)) <graphic> #&(a b c)

(let ([b (box "hi")])
  (unbox b)) <graphic> "hi"

procedure: (set-box! box obj)
returns: unspecified
libraries: (chezscheme)

box must be mutable. set-box! sets the contents of box to obj.

(let ([b (box 'x)])
  (set-box! b 'y)
  b) <graphic> #&y

(let ([incr!
       (lambda (x)
         (set-box! x (+ (unbox x) 1)))])
  (let ([b (box 3)])
    (incr! b)
    (unbox b))) <graphic> 4

procedure: (box-cas! box old-obj new-obj)
returns: #t if box is changed, #f otherwise
libraries: (chezscheme)

box must be mutable. box-cas! atomically changes the content of box to new-obj if the replaced content is eq? to old-obj. If the content of box that would be replaced is not eq? to old-obj, then box is unchanged.

(define b (box 'old))
(box-cas! b 'old 'new) <graphic> #t
(unbox b) <graphic> 'new
(box-cas! b 'other 'wrong) <graphic> #f
(unbox b) <graphic> 'new

procedure: (mutable-box? obj)
returns: #t if obj is a mutable box, #f otherwise
procedure: (immutable-box? obj)
returns: #t if obj is an immutable box, #f otherwise
libraries: (chezscheme)

(mutable-box? (box 1)) <graphic> #t
(mutable-box? (box-immutable 1)) <graphic> #f
(immutable-box? (box 1)) <graphic> #f
(immutable-box? (box-immutable 1)) <graphic> #t
(mutable-box? (cons 3 4)) <graphic> #f

procedure: (box-immutable obj)
returns: a new immutable box containing obj
libraries: (chezscheme)

Boxes are typically intended to support shared, mutable structure, so immutable boxes are not often useful.

(define b (box-immutable 1))
(set-box! b 0) <graphic> exception: not mutable

Section 7.9. Symbols

Chez Scheme extends the standard symbol syntax in several ways:

The printer always prints symbols using the standard R6RS syntax, so that, e.g., @abc prints as \x40;abc and 1- prints as \x31;-. '

Gensyms are printed #{ and } brackets that enclose both the "pretty" and "unique" names, e.g., #{g1426 e5g1c94g642dssw-a}. They may also be printed using the pretty name only with the prefix #:, e.g., #:g1426.

These extensions are disabled in an input stream after #!r6rs has been seen by the reader, unless #!chezscheme has been seen more recently.

procedure: (gensym)
procedure: (gensym pretty-name)
procedure: (gensym pretty-name unique-name)
returns: a unique generated symbol
libraries: (chezscheme)

Each call to gensym returns a unique generated symbol, or gensym. Each generated symbol has two names: a "pretty" name and a "unique" name.

In the first form above, the pretty name is formed (lazily---see below) by combining an internal prefix with the value of an internal counter. After each name is formed, the internal counter is incremented. The parameters gensym-prefix and gensym-count, described below, may be used to access and set the internal prefix and counter. By default, the prefix is the single-character string "g". In the second and third forms, the pretty name of the new gensym is pretty-name, which must be a string. The pretty name of a gensym is returned by the procedure symbol->string.

In both the first and second forms, the unique name is an automatically generated globally unique name. Globally unique names are constructed (lazily---see below) from the combination of a universally unique identifier and an internal counter. In the third form of gensym, the unique name of the new gensym is unique-name, which must be a string. The unique name of a gensym may be obtained via the procedure gensym->unique-string.

The unique name allows gensyms to be written in such a way that they can be read back and reliably commonized on input. The syntax for gensyms includes both the pretty name and the unique name, as shown in the example below:

(gensym) <graphic> #{g0 bcsfg5eq4e9b3h9o-a}

When the parameter print-gensym is set to pretty, the printer prints the pretty name only, with a #: syntax, so

(parameterize ([print-gensym 'pretty])
  (write (gensym)))

prints #:g0.

When the reader sees the #: syntax, it produces a gensym with the given pretty name, but the original unique name is lost.

When the parameter is set to #f, the printer prints just the pretty name, so

(parameterize ([print-gensym #f])
  (write (gensym)))

prints g0. This is useful only when gensyms do not need to be read back in as gensyms.

In order to reduce construction and (when threaded) synchronization overhead when gensyms are frequently created but rarely printed or stored in an object file, generated pretty and unique names are created lazily, i.e., not until first requested, either by the printer, fasl writer, or explicitly by one of the procedures symbol->string or gensym->unique-string. In addition, a gensym is not placed into the system's internal symbol table (the oblist; see page 156) until the unique name is requested. This allows a gensym to be reclaimed by the storage manager if no references to the gensym exist and no unique name exists by which to access it, even if it has a top-level binding or a nonempty property list.

(define x (gensym))
x                         <graphic> #{g2 bcsfg5eq4e9b3h9o-c}
(symbol->string x)        <graphic> "g2"
(gensym->unique-string x) <graphic> "bcsfg5eq4e9b3h9o-c"

Gensyms subsume the notion of uninterned symbols supported by earlier versions of Chez Scheme. Similarly, the predicate uninterned-symbol? has been replaced by gensym?.

thread parameter: gensym-prefix
thread parameter: gensym-count
libraries: (chezscheme)

The parameters gensym-prefix and gensym-count are used to access and set the internal prefix and counter from which the pretty name of a gensym is generated when gensym is not given an explicit string argument. gensym-prefix defaults to the string "g" and may be set to any object. gensym-count starts at 0 and may be set to any nonnegative integer.

As described above, Chez Scheme delays the creation of the pretty name until the name is first requested by the printer or by an explicit call to symbol->string. These parameters are not consulted until that time; setting them when gensym is called thus has no effect on the generated name.

(let ([x (parameterize ([gensym-prefix "genny"]
                        [gensym-count 17]
                        [print-gensym 'pretty])
           (gensym))])
  (format "~s" x))                       <graphic> "#{g4 bcsfg5eq4e9b3h9o-e}"
(let ([x (gensym)])
  (parameterize ([gensym-prefix "genny"]
                 [gensym-count 17]
                 [print-gensym #f])
    (format "~s" (gensym))))             <graphic> "genny17"

procedure: (gensym->unique-string gensym)
returns: the unique name of gensym
libraries: (chezscheme)

(gensym->unique-string (gensym)) <graphic> "bd3kufa7ypjcuvut-g"

procedure: (gensym? obj)
returns: #t if obj is gensym, #f otherwise
libraries: (chezscheme)

(gensym? (string->symbol "z")) <graphic> #f
(gensym? (gensym "z")) <graphic> #t
(gensym? 'a) <graphic> #f
(gensym? 3) <graphic> #f
(gensym? (gensym)) <graphic> #t
(gensym? '#{g2 bcsfg5eq4e9b3h9o-c}) <graphic> #t

procedure: (putprop symbol key value)
returns: unspecified
libraries: (chezscheme)

Chez Scheme associates a property list with each symbol, allowing multiple key-value pairs to be stored directly with the symbol. New key-value pairs may be placed in the property list or retrieved in a manner analogous to the use of association lists, using the procedures putprop and getprop. Property lists are often used to store information related to the symbol itself. For example, a natural language program might use symbols to represent words, using their property lists to store information about use and meaning.

putprop associates value with key on the property list of symbol. key and value may be any types of object, although key is typically a symbol.

putprop may be used to establish a new property or to change an existing property.

See the examples under getprop below.

procedure: (getprop symbol key)
procedure: (getprop symbol key default)
returns: the value associated with key on the property list of symbol
libraries: (chezscheme)

getprop searches the property list of symbol for a key identical to key (in the sense of eq?), and returns the value associated with this key, if any. If no value is associated with key on the property list of symbol, getprop returns default, or #f if the default argument is not supplied.

(putprop 'fred 'species 'snurd)
(putprop 'fred 'age 4)  
(putprop 'fred 'colors '(black white))

(getprop 'fred 'species) <graphic> snurd
(getprop 'fred 'colors) <graphic> (black white)
(getprop 'fred 'nonkey) <graphic> #f
(getprop 'fred 'nonkey 'unknown) <graphic> unknown
 
(putprop 'fred 'species #f)
(getprop 'fred 'species 'unknown) <graphic> #f

procedure: (remprop symbol key)
returns: unspecified
libraries: (chezscheme)

remprop removes the property with key key from the property list of symbol, if such a property exists.

(putprop 'fred 'species 'snurd)
(getprop 'fred 'species) <graphic> snurd

(remprop 'fred 'species)
(getprop 'fred 'species 'unknown) <graphic> unknown

procedure: (property-list symbol)
returns: a copy of the internal property list for symbol
libraries: (chezscheme)

A property list is a list of alternating keys and values, i.e., (key value ...).

(putprop 'fred 'species 'snurd)
(putprop 'fred 'colors '(black white))
(property-list 'fred) <graphic> (colors (black white) species snurd)

procedure: (oblist)
returns: a list of interned symbols
libraries: (chezscheme)

The system maintains an internal symbol table used to insure that any two occurrences of the same symbol name resolve to the same symbol object. The oblist procedure returns a list of the symbols currently in this symbol table.

The list of interned symbols grows when a new symbol is introduced into the system or when the unique name of a gensym (see page 153) is requested. It shrinks when the garbage collector determines that it is safe to discard a symbol. It is safe to discard a symbol only if the symbol is not accessible except through the oblist, has no top-level binding, and has no properties on its property list.

(if (memq 'tiger (oblist)) 'yes 'no) <graphic> yes
(equal? (oblist) (oblist)) <graphic> #t
(= (length (oblist)) (length (oblist))) <graphic> #t or #f

The first example above follows from the property that all interned symbols are in the oblist from the time they are read, which happens prior to evaluation. The second example follows from the fact that no symbols can be removed from the oblist while references to those symbols exist, in this case, within the list returned by the first call to oblist (whichever call is performed first). The expression in the third example can return #f only if a garbage collection occurs sometime between the two calls to oblist, and only if one or more symbols are removed from the oblist by that collection.

Section 7.10. Void

Many Scheme operations return an unspecified result. Chez Scheme typically returns a special void object when the value returned by an operation is unspecified. The Chez Scheme void object is not meant to be used as a datum, and consequently does not have a reader syntax. As for other objects without a reader syntax, such as procedures and ports, Chez Scheme output procedures print the void object using a nonreadable representation, i.e., #<void>. Since the void object should be returned only by operations that do not have "interesting" values, the default waiter printer (see waiter-write) suppresses the printing of the void object. set!, set-car!, load, and write are examples of Chez Scheme operations that return the void object.

procedure: (void)
returns: the void object
libraries: (chezscheme)

void is a procedure of no arguments that returns the void object. It can be used to force expressions that are used for effect or whose values are otherwise unspecified to evaluate to a consistent, trivial value. Since most Chez Scheme operations that are used for effect return the void object, however, it is rarely necessary to explicitly invoke the void procedure.

Since the void object is used explicitly as an "unspecified" value, it is a bad idea to use it for any other purpose or to count on any given expression evaluating to the void object.

The default waiter printer suppresses the void object; that is, nothing is printed for expressions that evaluate to the void object.

(eq? (void) #f) <graphic> #f
(eq? (void) #t) <graphic> #f
(eq? (void) '()) <graphic> #f

Section 7.11. Sorting

procedure: (sort predicate list)
procedure: (sort! predicate list)
returns: a list containing the elements of list sorted according to predicate
libraries: (chezscheme)

sort is identical to the Revised6 Report list-sort, and sort! is a destructive version of sort, i.e., it reuses pairs from the input list to form the output list.

(sort < '(3 4 2 1 2 5)) <graphic> (1 2 2 3 4 5)
(sort! < '(3 4 2 1 2 5)) <graphic> (1 2 2 3 4 5)

procedure: (merge predicate list1 list2)
procedure: (merge! predicate list1 list2)
returns: list1 merged with list2 in the order specified by predicate
libraries: (chezscheme)

predicate should be a procedure that expects two arguments and returns #t if its first argument must precede its second in the merged list. It should not have any side effects. That is, if predicate is applied to two objects x and y, where x is taken from the second list and y is taken from the first list, it should return true only if x should appear before y in the output list. If this constraint is met, merge and merge! are stable, in that items from list1 are placed in front of equivalent items from list2 in the output list. Duplicate elements are included in the merged list.

merge! combines the lists destructively, using pairs from the input lists to form the output list.

(merge char<?
       '(#\a #\c)
       '(#\b #\c #\d)) <graphic> (#\a #\b #\c #\c #\d)
(merge <
       '(1/2 2/3 3/4)
       '(0.5 0.6 0.7)) <graphic> (1/2 0.5 0.6 2/3 0.7 3/4)

Section 7.12. Hashtables

Chez Scheme provides several extensions to the hashtable mechanism, including a mechanism for directly accessing a key, value pair in a hashtable, support for weak eq and eqv hashtables, and a set of procedures specialized to eq and symbol hashtables.

procedure: (hashtable-cell hashtable key default)
returns: a pair (see below)
libraries: (chezscheme)

hashtable must be a hashtable. key and default may be any Scheme values.

If no value is associated with key in hashtable, hashtable-cell modifies hashtable to associate key with default. It returns a pair whose car is key and whose cdr is the associated value. Changing the cdr of this pair effectively updates the table to associate key with a new value. The key in the car field should not be changed. The advantage of this procedure over the Revised6 Report procedures for manipulating hashtable entries is that the value associated with a key may be read or written many times with only a single hashtable lookup.

(define ht (make-eq-hashtable))
(define v (vector 'a 'b 'c))
(define cell (hashtable-cell ht v 3))
cell <graphic> (#(a b c) . 3)
(hashtable-ref ht v 0) <graphic> 3
(set-cdr! cell 4)
(hashtable-ref ht v 0) <graphic> 4

procedure: (hashtable-keys hashtable)
procedure: (hashtable-keys hashtable size)
returns: a vector containing the keys in hashtable
libraries: (chezscheme)

Identical to the Revised6 Report counterpart, but allowing an optional size argument. If size is specified, then it must be an exact, nonnegative integer, and the result vector contains no more than size elements. Different calls to hashtable-keys with a size less than (hashtable-size hashtable) may return different subsets of hashtable's keys.

(define ht (make-eq-hashtable))
(hashtable-set! ht 'a "one")
(hashtable-set! ht 'b "two")
(hashtable-set! ht 'c "three")
(hashtable-keys ht) <graphic> #(a b c) or any permutation
(hashtable-keys ht 1) <graphic> #(a) or #(b) or #(c)

procedure: (hashtable-values hashtable)
procedure: (hashtable-values hashtable size)
returns: a vector containing the values in hashtable
libraries: (chezscheme)

Each value is the value of one of the keys in hashtable. Duplicate values are not removed. The values may appear in any order in the returned vector. If size is specified, then it must be an exact, nonnegative integer, and the result vector contains no more than size elements. Different calls to hashtable-values with a size less than (hashtable-size hashtable) may return different subsets of hashtable's values.

(define ht (make-eq-hashtable))
(define p1 (cons 'a 'b))
(define p2 (cons 'a 'b))
(hashtable-set! ht p1 "one")
(hashtable-set! ht p2 "two")
(hashtable-set! ht 'q "two")
(hashtable-values ht) <graphic> #("one" "two" "two") or any permutation
(hashtable-values ht 1) <graphic> #("one") or #("two")

This procedure is equivalent to calling hashtable-entries and returning only the second result, but it is more efficient since the separate vector of keys need not be created.

procedure: (hashtable-entries hashtable)
procedure: (hashtable-entries hashtable size)
returns: two vectors containing the keys and values in hashtable
libraries: (chezscheme)

Identical to the Revised6 Report counterpart, but allowing an optional size argument. If size is specified, then it must be an exact, nonnegative integer, and the result vectors contain no more than size elements. Different calls to hashtable-entries with a size less than (hashtable-size hashtable) may return different subsets of hashtable's entries.

(define ht (make-eq-hashtable))
(hashtable-set! ht 'a "one")
(hashtable-set! ht 'b "two")
(hashtable-entries ht) <graphic> #(a b) #("one" "two") or the other permutation
(hashtable-entries ht 1) <graphic> #(a) #("one") or #(b) #("two") 

procedure: (hashtable-cells hashtable)
procedure: (hashtable-cells hashtable size)
returns: a vector of up to size elements containing the cells of hashtable
libraries: (chezscheme)

Each element of the result vector is the value of one of the cells in hashtable. The cells may appear in any order in the returned vector. If size is specified, then it must be an exact, nonnegative integer, and the result vector contains no more than size cells. If size is not specified, then the result vector has (hashtable-size hashtable) elements. Different calls to hashtable-cells with a size less than (hashtable-size hashtable) may return different subsets of hashtable's cells.

(define ht (make-eqv-hashtable))
(hashtable-set! ht 1 'one)
(hashtable-set! ht 2 'two)
(hashtable-cells ht) <graphic> #((1 . one) (2 . two)) or #((2 . two) (1 . one))
(hashtable-cells ht 1) <graphic> #((1 . one)) or #((2 . two))
(hashtable-cells ht 0) <graphic> #()

procedure: (make-weak-eq-hashtable)
procedure: (make-weak-eq-hashtable size)
procedure: (make-weak-eqv-hashtable)
procedure: (make-weak-eqv-hashtable size)
returns: a new weak eq hashtable
libraries: (chezscheme)

These procedures are like the Revised6 Report procedures make-eq-hashtable and make-eqv-hashtable except the keys of the hashtable are held weakly, i.e., they are not protected from the garbage collector. Keys reclaimed by the garbage collector are removed from the table, and their associated values are dropped the next time the table is modified, if not sooner.

Values in the hashtable are referenced normally as long as the key is not reclaimed, since keys are paired values using weak pairs. Consequently, if a value in the hashtable refers to its own key, then garbage collection is prevented from reclaiming the key. See make-ephemeron-eq-hashtable and make-ephemeron-eqv-hashtable.

A copy of a weak eq or eqv hashtable created by hashtable-copy is also weak. If the copy is immutable, inaccessible keys may still be dropped from the hashtable, even though the contents of the table is otherwise unchanging. The effect of this can be observed via hashtable-keys and hashtable-entries.

(define ht1 (make-weak-eq-hashtable))
(define ht2 (make-weak-eq-hashtable 32))

procedure: (make-ephemeron-eq-hashtable)
procedure: (make-ephemeron-eq-hashtable size)
procedure: (make-ephemeron-eqv-hashtable)
procedure: (make-ephemeron-eqv-hashtable size)
returns: a new ephemeron eq hashtable
libraries: (chezscheme)

These procedures are like make-weak-eq-hashtable and make-weak-eqv-hashtable, but a value in the hashtable can refer to a key in the hashtable (directly or indirectly) without preventing garbage collection from reclaiming the key, because keys are paired with values using ephemeron pairs.

A copy of an ephemeron eq or eqv hashtable created by hashtable-copy is also an ephemeron table, and an inaccessible key can be dropped from an immutable ephemeron hashtable in the same way as for an immutable weak hashtable.

(define ht1 (make-ephemeron-eq-hashtable))
(define ht2 (make-ephemeron-eq-hashtable 32))

procedure: (hashtable-weak? obj)
returns: #t if obj is a weak eq or eqv hashtable, #f otherwise
libraries: (chezscheme)

(define ht1 (make-weak-eq-hashtable))
(define ht2 (hashtable-copy ht1))
(hashtable-weak? ht2) <graphic> #t

procedure: (hashtable-ephemeron? obj)
returns: #t if obj is an ephemeron eq or eqv hashtable, #f otherwise
libraries: (chezscheme)

(define ht1 (make-ephemeron-eq-hashtable))
(define ht2 (hashtable-copy ht1))
(hashtable-ephemeron? ht2) <graphic> #t

procedure: (eq-hashtable? obj)
returns: #t if obj is an eq hashtable, #f otherwise
libraries: (chezscheme)

(eq-hashtable? (make-eq-hashtable)) <graphic> #t
(eq-hashtable? '(not a hash table)) <graphic> #f

procedure: (eq-hashtable-weak? hashtable)
returns: #t if hashtable is weak, #f otherwise
libraries: (chezscheme)

hashtable must be an eq hashtable.

(eq-hashtable-weak? (make-eq-hashtable)) <graphic> #f
(eq-hashtable-weak? (make-weak-eq-hashtable)) <graphic> #t

procedure: (eq-hashtable-ephemeron? hashtable)
returns: #t if hashtable uses ephemeron pairs, #f otherwise
libraries: (chezscheme)

hashtable must be an eq hashtable.

(eq-hashtable-ephemeron? (make-eq-hashtable)) <graphic> #f
(eq-hashtable-ephemeron? (make-ephemeron-eq-hashtable)) <graphic> #t

procedure: (eq-hashtable-set! hashtable key value)
returns: unspecified
libraries: (chezscheme)

hashtable must be a mutable eq hashtable. key and value may be any Scheme values.

eq-hashtable-set! associates the value value with the key key in hashtable.

(define ht (make-eq-hashtable))
(eq-hashtable-set! ht 'a 73)

procedure: (eq-hashtable-ref hashtable key default)
returns: see below
libraries: (chezscheme)

hashtable must be an eq hashtable. key and default may be any Scheme values.

eq-hashtable-ref returns the value associated with key in hashtable. If no value is associated with key in hashtable, eq-hashtable-ref returns default.

(define ht (make-eq-hashtable))
(define p1 (cons 'a 'b))
(define p2 (cons 'a 'b))
(eq-hashtable-set! ht p1 73)
(eq-hashtable-ref ht p1 55) <graphic> 73
(eq-hashtable-ref ht p2 55) <graphic> 55

procedure: (eq-hashtable-contains? hashtable key)
returns: #t if an association for key exists in hashtable, #f otherwise
libraries: (chezscheme)

hashtable must be an eq hashtable. key may be any Scheme value.

(define ht (make-eq-hashtable))
(define p1 (cons 'a 'b))
(define p2 (cons 'a 'b))
(eq-hashtable-set! ht p1 73)
(eq-hashtable-contains? ht p1) <graphic> #t
(eq-hashtable-contains? ht p2) <graphic> #f

procedure: (eq-hashtable-update! hashtable key procedure default)
returns: unspecified
libraries: (chezscheme)

hashtable must be a mutable eq hashtable. key and default may be any Scheme values. procedure should accept one argument, should return one value, and should not modify hashtable.

eq-hashtable-update! applies procedure to the value associated with key in hashtable, or to default if no value is associated with key in hashtable. If procedure returns, eq-hashtable-update! associates key with the value returned by procedure, replacing the old association, if any.

A version of eq-hashtable-update! that does not verify that it receives arguments of the proper type might be defined as follows.

(define eq-hashtable-update!
  (lambda (ht key proc value)
    (eq-hashtable-set! ht key
      (proc (eq-hashtable-ref ht key value)))))

An implementation may, however, be able to implement eq-hashtable-update! more efficiently by avoiding multiple hash computations and hashtable lookups.

(define ht (make-eq-hashtable))
(eq-hashtable-update! ht 'a
  (lambda (x) (* x 2))
  55)
(eq-hashtable-ref ht 'a 0) <graphic> 110
(eq-hashtable-update! ht 'a
  (lambda (x) (* x 2))
  0)
(eq-hashtable-ref ht 'a 0) <graphic> 220

procedure: (eq-hashtable-cell hashtable key default)
returns: a pair (see below)
libraries: (chezscheme)

hashtable must be an eq hashtable. key and default may be any Scheme values.

If no value is associated with key in hashtable, eq-hashtable-cell modifies hashtable to associate key with default. It returns a pair whose car is key and whose cdr is the associated value. Changing the cdr of this pair effectively updates the table to associate key with a new value. The key should not be changed.

(define ht (make-eq-hashtable))
(define v (vector 'a 'b 'c))
(define cell (eq-hashtable-cell ht v 3))
cell <graphic> (#(a b c) . 3)
(eq-hashtable-ref ht v 0) <graphic> 3
(set-cdr! cell 4)
(eq-hashtable-ref ht v 0) <graphic> 4

procedure: (eq-hashtable-delete! hashtable key)
returns: unspecified
libraries: (chezscheme)

hashtable must be a mutable eq hashtable. key may be any Scheme value.

eq-hashtable-delete! drops any association for key from hashtable.

(define ht (make-eq-hashtable))
(define p1 (cons 'a 'b))
(define p2 (cons 'a 'b))
(eq-hashtable-set! ht p1 73)
(eq-hashtable-contains? ht p1) <graphic> #t
(eq-hashtable-delete! ht p1)
(eq-hashtable-contains? ht p1) <graphic> #f
(eq-hashtable-contains? ht p2) <graphic> #f
(eq-hashtable-delete! ht p2)

procedure: (symbol-hashtable? obj)
returns: #t if obj is a symbol hashtable, #f otherwise
libraries: (chezscheme)

A symbol hashtable is a hashtable created with hash function symbol-hash and equivalence function eq?, eqv?, equal?, or symbol=?.

(symbol-hashtable? (make-hashtable symbol-hash eq?)) <graphic> #t
(symbol-hashtable? (make-eq-hashtable)) <graphic> #f

procedure: (symbol-hashtable-set! hashtable key value)
returns: unspecified
libraries: (chezscheme)

hashtable must be a mutable symbol hashtable. (A symbol hashtable is a hashtable created with hash function symbol-hash and equivalence function eq?, eqv?, equal?, or symbol=?.) key must be a symbol, and value may be any Scheme value.

symbol-hashtable-set! associates the value value with the key key in hashtable.

(define ht (make-hashtable symbol-hash eq?))
(symbol-hashtable-ref ht 'a #f) <graphic> #f
(symbol-hashtable-set! ht 'a 73)
(symbol-hashtable-ref ht 'a #f) <graphic> 73

procedure: (symbol-hashtable-ref hashtable key default)
returns: see below
libraries: (chezscheme)

hashtable must be a symbol hashtable. (A symbol hashtable is a hashtable created with hash function symbol-hash and equivalence function eq?, eqv?, equal?, or symbol=?.) key must be a symbol, and default may be any Scheme value.

symbol-hashtable-ref returns the value associated with key in hashtable. If no value is associated with key in hashtable, symbol-hashtable-ref returns default.

(define ht (make-hashtable symbol-hash eq?))
(define k1 'abcd)
(define k2 'not-abcd)
(symbol-hashtable-set! ht k1 "hi")
(symbol-hashtable-ref ht k1 "bye") <graphic> "hi"
(symbol-hashtable-ref ht k2 "bye") <graphic> "bye"

procedure: (symbol-hashtable-contains? hashtable key)
returns: #t if an association for key exists in hashtable, #f otherwise
libraries: (chezscheme)

hashtable must be a symbol hashtable. (A symbol hashtable is a hashtable created with hash function symbol-hash and equivalence function eq?, eqv?, equal?, or symbol=?.) key must be a symbol.

(define ht (make-hashtable symbol-hash eq?))
(define k1 'abcd)
(define k2 'not-abcd)
(symbol-hashtable-set! ht k1 "hi")
(symbol-hashtable-contains? ht k1) <graphic> #t
(symbol-hashtable-contains? ht k2 ) <graphic> #f

procedure: (symbol-hashtable-update! hashtable key procedure default)
returns: unspecified
libraries: (chezscheme)

hashtable must be a mutable symbol hashtable. (A symbol hashtable is a hashtable created with hash function symbol-hash and equivalence function eq?, eqv?, equal?, or symbol=?.) key must be a symbol, and default may be any Scheme value. procedure should accept one argument, should return one value, and should not modify hashtable.

symbol-hashtable-update! applies procedure to the value associated with key in hashtable, or to default if no value is associated with key in hashtable. If procedure returns, symbol-hashtable-update! associates key with the value returned by procedure, replacing the old association, if any.

A version of symbol-hashtable-update! that does not verify that it receives arguments of the proper type might be defined as follows.

(define symbol-hashtable-update!
  (lambda (ht key proc value)
    (symbol-hashtable-set! ht key
      (proc (symbol-hashtable-ref ht key value)))))

An implementation may, however, be able to implement symbol-hashtable-update! more efficiently by avoiding multiple hash computations and hashtable lookups.

(define ht (make-hashtable symbol-hash eq?))
(symbol-hashtable-update! ht 'a
  (lambda (x) (* x 2))
  55)
(symbol-hashtable-ref ht 'a 0) <graphic> 110
(symbol-hashtable-update! ht 'a
  (lambda (x) (* x 2))
  0)
(symbol-hashtable-ref ht 'a 0) <graphic> 220

procedure: (symbol-hashtable-cell hashtable key default)
returns: a pair (see below)
libraries: (chezscheme)

hashtable must be a mutable symbol hashtable. (A symbol hashtable is a hashtable created with hash function symbol-hash and equivalence function eq?, eqv?, equal?, or symbol=?.) key must be a symbol, and default may be any Scheme value.

If no value is associated with key in hashtable, symbol-hashtable-cell modifies hashtable to associate key with default. It returns a pair whose car is key and whose cdr is the associated value. Changing the cdr of this pair effectively updates the table to associate key with a new value. The key should not be changed.

(define ht (make-hashtable symbol-hash eq?))
(define k 'a-key)
(define cell (symbol-hashtable-cell ht k 3))
cell <graphic> (a-key . 3)
(symbol-hashtable-ref ht k 0) <graphic> 3
(set-cdr! cell 4)
(symbol-hashtable-ref ht k 0) <graphic> 4

procedure: (symbol-hashtable-delete! hashtable key)
returns: unspecified
libraries: (chezscheme)

hashtable must be a mutable symbol hashtable. (A symbol hashtable is a hashtable created with hash function symbol-hash and equivalence function eq?, eqv?, equal?, or symbol=?.) key must be a symbol.

symbol-hashtable-delete! drops any association for key from hashtable.

(define ht (make-hashtable symbol-hash eq?))
(define k1 (gensym))
(define k2 (gensym))
(symbol-hashtable-set! ht k1 73)
(symbol-hashtable-contains? ht k1) <graphic> #t
(symbol-hashtable-delete! ht k1)
(symbol-hashtable-contains? ht k1) <graphic> #f
(symbol-hashtable-contains? ht k2) <graphic> #f
(symbol-hashtable-delete! ht k2)

Section 7.13. Record Types

Chez Scheme extends the Revised6 Report's define-record-type syntax in one way, which is that it allows a generative record type to be declared explicitly as such (in a double-negative sort of way) by including a nongenerative clause with #f as the uid, i.e.:

(nongenerative #f)

This can be used in conjunction with the parameter require-nongenerative-clause to catch the accidental use of generative record types while avoiding spurious errors for record types that must be generative. Generative record types are rarely needed and are generally less efficient since a run-time representation of the type is created each time the define-record-clause is evaluated, rather than once at compile (expansion) time.

thread parameter: require-nongenerative-clause
libraries: (chezscheme)

This parameter holds a boolean value that determines whether define-record-type requires a nongenerative clause. The default value is #f. The lead-in above describes why one might want to set this to #t.

Section 7.14. Record Equality and Hashing

By default, the equal? primitive compares record instances using eq?, i.e., it distinguishes non-eq? instances even if they are of the same type and have equal contents. A program can override this behavior for instances of a record type (and its subtypes that do not have their own equality procedures) by using record-type-equal-procedure to associate an equality procedure with the record-type descriptor (rtd) that describes the record type.

When comparing two eq? instances, equal? always returns #t. When comparing two non-eq? instances that share an equality procedure equal-proc, equal? uses equal-proc to compare the instances. Two instances x and y share an equality procedure if they inherit an equality procedure from the same point in the inheritance chain, i.e., if (record-equal-procedure x y) returns a procedure (equal-proc) rather than #f. equal? passes equal-proc three arguments: the two instances plus a eql? procedure that should be used for recursive comparison of values within the two instances. Use of eql? for recursive comparison is necessary to allow comparison of potentially cyclic structure. When comparing two non-eq? instances that do not share an equality procedure, equal? returns #f.

A default equality procedure to be used for all record types (including opaque types) can be specified via the parameter default-record-equal-procedure. The default equality procedure is used only if neither instance's type has or inherits a type-specific record equality procedure.

Similarly, when the equal-hash primitive hashes a record instance, it defaults to a value that is independent of the record type and contents of the instance. A program can override this behavior for instances of a record type by using record-type-hash-procedure to associate a hash procedure with the record-type descriptor (rtd) that describes the record type. The procedure record-hash-procedure can be used to find the hash procedure for a given record instance, following the inheritance chain. equal-hash passes the hash procedure two arguments: the instance plus a hash procedure that should be used for recursive hashing of values within the instance. Use of hash for recursive hashing is necessary to allow hashing of potentially cyclic structure and to make the hashing of shared structure more efficient.

A default hash procedure to be used for all record types (including opaque types) can be specified via the parameter default-record-hash-procedure. The default hash procedure is used only if an instance's type does not have or inherit a type-specific hash procedure.

The following example illustrates the setting of equality and hash procedures.

(define-record-type marble
  (nongenerative)
  (fields color quality))

(record-type-equal-procedure (record-type-descriptor marble)) <graphic> #f
(equal? (make-marble 'blue 'medium) (make-marble 'blue 'medium)) <graphic> #f
(equal? (make-marble 'blue 'medium) (make-marble 'blue 'high)) <graphic> #f

; Treat marbles as equal when they have the same color
(record-type-equal-procedure (record-type-descriptor marble)
  (lambda (m1 m2 eql?)
    (eql? (marble-color m1) (marble-color m2))))
(record-type-hash-procedure (record-type-descriptor marble)
  (lambda (m hash)
    (hash (marble-color m))))

(equal? (make-marble 'blue 'medium) (make-marble 'blue 'high)) <graphic> #t
(equal? (make-marble 'red 'high) (make-marble 'blue 'high)) <graphic> #f

(define ht (make-hashtable equal-hash equal?))
(hashtable-set! ht (make-marble 'blue 'medium) "glass")
(hashtable-ref ht (make-marble 'blue 'high) #f) <graphic> "glass"

(define-record-type shooter
  (nongenerative)
  (parent marble)
  (fields size))

(equal? (make-marble 'blue 'medium) (make-shooter 'blue 'large 17)) <graphic> #t
(equal? (make-shooter 'blue 'large 17) (make-marble 'blue 'medium)) <graphic> #t
(hashtable-ref ht (make-shooter 'blue 'high 17) #f) <graphic> "glass"

This example illustrates the application of equality and hash procedures to cyclic record structures.

(define-record-type node
  (nongenerative)
  (fields (mutable left) (mutable right)))

(record-type-equal-procedure (record-type-descriptor node)
  (lambda (x y e?)
    (and
      (e? (node-left x) (node-left y))
      (e? (node-right x) (node-right y)))))
(record-type-hash-procedure (record-type-descriptor node)
  (lambda (x hash)
    (+ (hash (node-left x)) (hash (node-right x)) 23)))

(define graph1
  (let ([x (make-node "a" (make-node #f "b"))])
    (node-left-set! (node-right x) x)
    x))
(define graph2
  (let ([x (make-node "a" (make-node (make-node "a" #f) "b"))])
    (node-right-set! (node-left (node-right x)) (node-right x))
    x))
(define graph3
  (let ([x (make-node "a" (make-node #f "c"))])
    (node-left-set! (node-right x) x)
    x))

(equal? graph1 graph2) <graphic> #t
(equal? graph1 graph3) <graphic> #f
(equal? graph2 graph3) <graphic> #f

(define h (make-hashtable equal-hash equal?))
(hashtable-set! h graph1 #t)
(hashtable-ref h graph1 #f) <graphic> #t
(hashtable-ref h graph2 #f) <graphic> #t
(hashtable-ref h graph3 #f) <graphic> #f

procedure: (record-type-equal-procedure rtd equal-proc)
returns: unspecified
procedure: (record-type-equal-procedure rtd)
returns: equality procedure associated with rtd, if any, otherwise #f
libraries: (chezscheme)

In the first form, equal-proc must be a procedure or #f. If equal-proc is a procedure, a new association between rtd and equal-proc is established, replacing any existing such association. If equal-proc is #f, any existing association between rtd and an equality procedure is dropped.

In the second form, record-type-equal-procedure returns the equality procedure associated with rtd, if any, otherwise #f.

When changing a record type's equality procedure, the record type's hash procedure, if any, should be updated if necessary to maintain the property that it produces the same hash value for any two instances the equality procedure considers equal.

procedure: (record-equal-procedure record1 record2)
returns: the shared equality procedure for record1 and record2, if there is one, otherwise #f
libraries: (chezscheme)

record-equal-procedure traverses the inheritance chains for both record instances in an attempt to find the most specific type for each that is associated with an equality procedure, if any. If such type is found and is the same for both instances, the equality procedure associated with the type is returned. Otherwise, #f is returned.

procedure: (record-type-hash-procedure rtd hash-proc)
returns: unspecified
procedure: (record-type-hash-procedure rtd)
returns: hash procedure associated with rtd, if any, otherwise #f
libraries: (chezscheme)

In the first form, hash-proc must be a procedure or #f. If hash-proc is a procedure, a new association between rtd and hash-proc is established, replacing any existing such association. If hash-proc is #f, any existing association between rtd and a hash procedure is dropped.

In the second form, record-type-hash-procedure returns the hash procedure associated with rtd, if any, otherwise #f.

The procedure hash-proc should accept two arguments, the instance for which it should compute a hash value and a hash procedure to use to compute hash values for arbitrary fields of the instance, and it returns a nonnegative exact integer. A record type's hash procedure should produce the same hash value for any two instances the record type's equality procedure considers equal.

procedure: (record-hash-procedure record)
returns: the hash procedure for record, if there is one, otherwise #f
libraries: (chezscheme)

record-hash-procedure traverses the inheritance chain for the record instance in an attempt to find the most specific type that is associated with a hash procedure, if any. If such type is found, the hash procedure associated with the type is returned. Otherwise, #f is returned.

thread parameter: default-record-equal-procedure
libraries: (chezscheme)

This parameter determines how two record instances are compared by equal? if neither has a type-specific equality procedure. When the parameter has the value #f (the default), equal? compares the instances with eq?, i.e., there is no attempt at determining structural equivalence. Otherwise, the parameter's value must be a procedure, and equal? invokes that procedure to compare the instances, passing it three arguments: the two instances and a procedure that should be used to recursively compare arbitrary values within the instances.

thread parameter: default-record-hash-procedure
libraries: (chezscheme)

This parameter determines the hash procedure used when equal-hash is called on a record instance and the instance does not have a type-specific hash procedure. When the parameter has the value #f (the default), equal-hash returns a value that is independent of the record type and contents of the instance. Otherwise, the parameter's value must be a procedure, and equal-hash invokes the procedure to compute the instance's hash value, passing it the record instance and a procedure to invoke to recursively compute hash values for arbitrary values contained within the record. The procedure should return a nonnegative exact integer, and the return value should be the same for any two instances the default equal procedure considers equivalent.

Section 7.15. Legacy Record Types

In addition to the Revised6 Report record-type creation and definition mechanisms, which are described in Chapter 9 of The Scheme Programming Language, 4th Edition, Chez Scheme continues to support pre-R6RS mechanisms for creating new data types, or record types, with fixed sets of named fields. Many of the procedures described in this section are available only when imported from the (chezscheme csv7) library.

Code intended to be portable should use the R6RS mechanism instead.

Records may be defined via the define-record syntactic form or via the make-record-type procedure. The underlying representation of records and record-type descriptors is the same for the Revised6 Report mechanism and the alternative mechanism. Record types created by one can be used as parent record types for the other via the procedural mechanisms, though not via the syntactic mechanisms.

The syntactic (define-record) interface is the most commonly used interface. Each define-record form defines a constructor procedure for records of the new type, a type predicate that returns true only for records of the new type, an access procedure for each field, and an assignment procedure for each mutable field. For example,

(define-record point (x y))

creates a new point record type with two fields, x and y, and defines the following procedures:

(make-point x y) constructor
(point? obj) predicate
(point-x p) accessor for field x
(point-y p) accessor for field y
(set-point-x! p obj) mutator for field x
(set-point-y! p obj) mutator for field y

The names of these procedures follow a regular naming convention by default, but the programmer can override the defaults if desired. define-record allows the programmer to control which fields are arguments to the generated constructor procedure and which are explicitly initialized by the constructor procedure. Fields are mutable by default, but may be declared immutable. Fields can generally contain any Scheme value, but the internal representation of each field may be specified, which places implicit constraints on the type of value that may be stored there. These customization options are covered in the formal description of define-record later in this section.

The procedural (make-record-type) interface may be used to implement interpreters that must handle define-record forms. Each call to make-record-type returns a record-type descriptor representing the record type. Using this record-type descriptor, programs may generate constructors, type predicates, field accessors, and field mutators dynamically. The following code demonstrates how the procedural interface might be used to create a similar point record type and associated definitions.

(define point (make-record-type "point" '(x y)))
(define make-point (record-constructor point))
(define point? (record-predicate point))
(define point-x (record-field-accessor point 'x))
(define point-y (record-field-accessor point 'y))
(define set-point-x! (record-field-mutator point 'x))
(define set-point-y! (record-field-mutator point 'y))

The procedural interface is more flexible than the syntactic interface, but this flexibility can lead to less readable programs and compromises the compiler's ability to generate efficient code. Programmers should use the syntactic interface whenever it suffices.

A record-type descriptor may also be extracted from an instance of a record type, whether the record type was produced by define-record or make-record-type, and the extracted descriptor may also be used to produce constructors, predicates, accessors, and mutators, with a few limitations noted in the description of record-type-descriptor below. This is a powerful feature that permits the coding of portable printers and object inspectors. For example, the printer employs this feature in its default record printer, and the inspector uses it to allow inspection and mutation of system- and user-defined records during debugging.

A parent record may be specified in the define-record syntax or as an optional argument to make-record-type. A new record inherits the parent record's fields, and each instance of the new record type is considered to be an instance of the parent type as well, so that accessors and mutators for the parent type may be used on instances of the new type.

Record type definitions may be classified as either generative or nongenerative. A new type results for each generative record definition, while only one type results for all occurrences of a given nongenerative record definition. This distinction is important semantically since record accessors and setters are applicable only to objects with the same type.

Syntactic (define-record) record definitions are expand-time generative by default, which means that a new record is created when the code is expanded. Expansion happens once for each form prior to compilation or interpretation, as when it is entered interactively, loaded from source, or compiled by compile-file. As a result, multiple evaluations of a single define-record form, e.g., in the body of a procedure called multiple times, always produce the same record type.

Separate define-record forms usually produce different types, even if the forms are textually identical. The only exception occurs when the name of a record is specified as a generated symbol, or gensym (page 153). Multiple copies of a record definition whose name is given by a gensym always produce the same record type; i.e., such definitions are nongenerative. Each copy of the record definition must contain the same fields and field modifiers in the same order; an exception is raised with condition-type &assertion when two differing record types with the same generated name are loaded into the same Scheme process.

Procedural (make-record-type) record definitions are run-time generative by default. That is, each call to make-record-type usually produces a new record type. As with the syntactic interface, the only exception occurs when the name of the record is specified as a gensym, in which case the record type is fully nongenerative.

By default, a record is printed with the syntax

#[type-name field ...]

where field ... are the printed representations of the contents of the fields of the record, and type-name is a generated symbol, or gensym (page 153), that uniquely identifies the record type. For nongenerative records, type-name is the gensym provided by the program. Otherwise, it is a gensym whose "pretty" name (page 153) is the name given to the record by define-record or make-record-type.

The default printing of records of a given type may be overridden with record-writer.

The default syntax may be used as input to the reader as well, as long as the corresponding record type has already been defined in the Scheme session in which the read occurs. The parameter record-reader may be used to specify a different name to be recognized by the reader in place of the generated name. Specifying a different name in this manner also changes the name used when the record is printed. This reader extension is disabled in an input stream after #!r6rs has been seen by the reader, unless #!chezscheme has been seen more recently.

The mark (#n=) and reference (#n#) syntaxes may be used within the record syntax, with the result of creating shared or cyclic structure as desired. All cycles must be resolvable, however, without mutation of an immutable record field. That is, any cycle must contain at least one pointer through a mutable field, whether it is a mutable record field or a mutable field of a built-in object type such as a pair or vector.

When the parameter print-record is set to #f, records are printed using the simpler syntax

#<record of type name>

where name is the "pretty" name of the record (not the full gensym) or the reader name first assigned to the record type.

syntax: (define-record name (fld1 ...) ((fld2 init) ...) (opt ...))
syntax: (define-record name parent (fld1 ...) ((fld2 init) ...) (opt ...))
returns: unspecified
libraries: (chezscheme)

A define-record form is a definition and may appear anywhere and only where other definitions may appear.

define-record creates a new record type containing a specified set of named fields and defines a set of procedures for creating and manipulating instances of the record type.

name must be an identifier. If name is a generated symbol (gensym), the record definition is nongenerative, otherwise it is expand-time generative. (See the discussion of generativity earlier in this section.)

Each fld must be an identifier field-name, or it must take the form

(class type field-name)

where class and type are optional and field-name is an identifier. class, if present, must be the keyword immutable or the keyword mutable. If the immutable class specifier is present, the field is immutable; otherwise, the field is mutable. type, if present, specifies how the field is represented, as described below.

ptr any Scheme object
scheme-object same as ptr
int a C int
unsigned a C unsigned int
short a C short
unsigned-short a C unsigned short
long a C long
unsigned-long a C unsigned long
iptr a signed integer the size of a ptr
uptr an unsigned integer the size of a ptr
float a C float
double a C double
integer-8 an eight-bit signed integer
unsigned-8 an eight-bit unsigned integer
integer-16 a 16-bit signed integer
unsigned-16 a 16-bit unsigned integer
integer-32 a 32-bit signed integer
unsigned-32 a 32-bit unsigned integer
integer-64 a 64-bit signed integer
unsigned-64 a 64-bit unsigned integer
single-float a 32-bit single floating point number
double-float a 64-bit double floating point number

If a type is specified, the field can contain objects only of the specified type. If no type is specified, the field is of type ptr, meaning that it can contain any Scheme object.

The field identifiers name the fields of the record. The values of the n fields described by fld1 ... are specified by the n arguments to the generated constructor procedure. The values of the remaining fields, fld2 ..., are given by the corresponding expressions, init .... Each init is evaluated within the scope of the set of field names given by fld1 ... and each field in fld2 ... that precedes it, as if within a let* expression. Each of these field names is bound to the value of the corresponding field during initialization.

If parent is present, the record type named by parent is the parent of the record. The new record type inherits each of the parent record's fields, and records of the new type are considered records of the parent type. If parent is not present, the parent record type is a base record type with no fields.

The following procedures are defined by define-record:

If no parent record type is specified, the constructor behaves as if defined as

(define make-name
  (lambda (id1 ...)
    (let* ([id2 init] ...)
      body)))

where id1 ... are the names of the fields defined by fld1 ..., id2 ... are the names of the fields defined by fld2 ..., and body builds the record from the values of the identifiers id1 ... and id2 ....

If a parent record type is specified, the parent arguments appear first, and the parent fields are inserted into the record before the child fields.

The options opt ... control the selection of names of the generated constructor, predicate, accessors, and mutators.

(constructor id)
(predicate id)
(prefix string)

The option (constructor id) causes the generated constructor's name to be id rather than make-name. The option (predicate id) likewise causes the generated predicate's name to be id rather than name?. The option (prefix string) determines the prefix to be used in the generated accessor and mutator names in place of name-.

If no options are needed, the third subexpression, (opt ...), may be omitted. If no options and no fields other than those initialized by the arguments to the constructor procedure are needed, both the second and third subexpressions may be omitted. If options are specified, the second subexpression must be present, even if it contains no field specifiers.

Here is a simple example with no inheritance and no options.

(define-record marble (color quality))
(define x (make-marble 'blue 'medium))
(marble? x) <graphic> #t
(pair? x) <graphic> #f
(vector? x) <graphic> #f
(marble-color x) <graphic> blue
(marble-quality x) <graphic> medium
(set-marble-quality! x 'low)
(marble-quality x) <graphic> low

(define-record marble ((immutable color) (mutable quality))
  (((mutable shape) (if (eq? quality 'high) 'round 'unknown))))
(marble-shape (make-marble 'blue 'high)) <graphic> round
(marble-shape (make-marble 'blue 'low)) <graphic> unknown
(define x (make-marble 'blue 'high))
(set-marble-quality! x 'low)
(marble-shape x) <graphic> round
(set-marble-shape! x 'half-round)
(marble-shape x) <graphic> half-round

The following example illustrates inheritance.

(define-record shape (x y))
(define-record point shape ())
(define-record circle shape (radius))

(define a (make-point 7 -3))
(shape? a) <graphic> #t
(point? a) <graphic> #t
(circle? a) <graphic> #f

(shape-x a) <graphic> 7
(set-shape-y! a (- (shape-y a) 1))
(shape-y a) <graphic> -4

(define b (make-circle 7 -3 1))
(shape? b) <graphic> #t
(point? b) <graphic> #f
(circle? b) <graphic> #t

(circle-radius b) <graphic> 1
(circle-radius a) <graphic> exception: not of type circle

(define c (make-shape 0 0))
(shape? c) <graphic> #t
(point? c) <graphic> #f
(circle? c) <graphic> #f

This example demonstrates the use of options:

(define-record pair (car cdr)
  ()
  ((constructor cons)
   (prefix "")))

(define x (cons 'a 'b))
(car x) <graphic> a
(cdr x) <graphic> b
(pair? x) <graphic> #t

(pair? '(a b c)) <graphic> #f
<graphic> #[#{pair bdhavk1bwafxyss1-a} a b]

This example illustrates the use a specified reader name, immutable fields, and the graph mark and reference syntax.

(define-record triple ((immutable x1) (mutable x2) (immutable x3)))
(record-reader 'triple (type-descriptor triple))

(let ([t '#[triple #1=(1 2) (3 4) #1#]])
  (eq? (triple-x1 t) (triple-x3 t))) <graphic> #t
(let ([x '(#1=(1 2) . #[triple #1# b c])])
  (eq? (car x) (triple-x1 (cdr x)))) <graphic> #t
(let ([t #[triple #1# (3 4) #1=(1 2)]])
  (eq? (triple-x1 t) (triple-x3 t))) <graphic> #t
(let ([t '#1=#[triple a #1# c]])
  (eq? t (triple-x2 t))) <graphic> #t
(let ([t '#1=(#[triple #1# b #1#])])
  (and (eq? t (triple-x1 (car t)))
       (eq? t (triple-x1 (car t))))) <graphic> #t

Cycles established with the mark and reference syntax can be resolved only if a mutable record field or mutable location of some other object is involved the cycle, as in the last two examples above. An exception is raised with condition type &lexical if only immutable fields are involved.

'#1=#[triple #1# (3 4) #1#] <graphic> exception

The following example demonstrates the use of nongenerative record definitions.

(module A (point-disp)
  (define-record #{point bdhavk1bwafxyss1-b} (x y))
  (define square (lambda (x) (* x x)))
  (define point-disp
    (lambda (p1 p2)
      (sqrt (+ (square (- (point-x p1) (point-x p2)))
               (square (- (point-y p1) (point-y p2))))))))

(module B (base-disp)
  (define-record #{point bdhavk1bwafxyss1-b} (x y))
  (import A)
  (define base-disp
    (lambda (p)
      (point-disp (make-point 0 0) p))))

(let ()
  (import B)
  (define-record #{point bdhavk1bwafxyss1-b} (x y))
  (base-disp (make-point 3 4))) <graphic> 5

This works even if the different program components are loaded from different source files or are compiled separately and loaded from different object files.

syntax: predicate
syntax: prefix
syntax: constructor
libraries: (chezscheme)

These identifiers are auxiliary keywords for define-record. It is a syntax violation to reference these identifiers except in contexts where they are recognized as auxiliary keywords. mutable and immutable are also auxiliary keywords for define-record, shared with the Revised6 Report define-record-type.

syntax: (type-descriptor name)
returns: the record-type descriptor associated with name
libraries: (chezscheme)

name must name a record type defined by define-record or define-record-type.

This form is equivalent to the Revised6 Report record-type-descriptor form.

The record-type descriptor is useful for overriding the default read and write syntax using record-reader and record-writer and may also be used with the procedural interface routines described later in this section.

(define-record frob ())
(type-descriptor frob) <graphic> #<record type frob>

procedure: (record-reader name)
returns: the record-type descriptor associated with name
procedure: (record-reader rtd)
returns: the first name associated with rtd
procedure: (record-reader name rtd)
returns: unspecified
procedure: (record-reader name #f)
returns: unspecified
procedure: (record-reader rtd #f)
returns: unspecified
libraries: (chezscheme)

name must be a symbol, and rtd must be a record-type descriptor.

With one argument, record-reader is used to retrieve the record type associated with a name or name associated with a record type. If no association has been created, record-reader returns #f

With arguments name and rtd, record-reader registers rtd as the record-type descriptor to be used whenever the read procedure encounters a record named by name and printed in the default record syntax.

With arguments name and #f, record-reader removes any association for name to a record-type descriptor. Similarly, with arguments rtd and #f, record-reader removes any association for rtd to a name.

(define-record marble (color quality))
(define m (make-marble 'blue 'perfect))
<graphic> #[#{marble bdhavk1bwafxyss1-c} blue perfect]

(record-reader (type-descriptor marble)) <graphic> #f
(record-reader 'marble) <graphic> #f

(record-reader 'marble (type-descriptor marble))
(marble-color '#[marble red miserable]) <graphic> red

(record-reader (type-descriptor marble)) <graphic> marble
(record-reader 'marble) <graphic> #<record type marble>

(record-reader (type-descriptor marble) #f)
(record-reader (type-descriptor marble)) <graphic> #f
(record-reader 'marble) <graphic> #f

(record-reader 'marble (type-descriptor marble))
(record-reader 'marble #f)
(record-reader (type-descriptor marble)) <graphic> #f
(record-reader 'marble) <graphic> #f

The introduction of a record reader also changes the default printing of records. The printer always chooses the reader name first assigned to the record, if any, in place of the unique record name, as this continuation of the example above demonstrates.

(record-reader 'marble (type-descriptor marble))
(make-marble 'pink 'splendid) <graphic> #[marble pink splendid]

procedure: (record-writer rtd)
returns: the record writer associated with rtd
procedure: (record-writer rtd procedure)
returns: unspecified
libraries: (chezscheme)

rtd must be a record-type descriptor, and procedure should accept three arguments, as described below.

When passed only one argument, record-writer returns the record writer associated with rtd, which is initially the default record writer for all records. The default print method prints all records in a uniform syntax that includes the generated name for the record and the values of each of the fields, as described in the introduction to this section.

When passed two arguments, record-writer establishes a new association between rtd and procedure so that procedure will be used by the printer in place of the default printer for records of the given type. The printer passes procedure three arguments: the record r, a port p, and a procedure wr that should be used to write out the values of arbitrary Scheme objects that the print method chooses to include in the printed representation of the record, e.g., values of the record's fields.

(define-record marble (color quality))
(define m (make-marble 'blue 'medium))

<graphic> #[#{marble bdhavk1bwafxyss1-d} blue medium]

(record-writer (type-descriptor marble)
  (lambda (r p wr)
    (display "#<" p)
    (wr (marble-quality r) p)
    (display " quality " p)
    (wr (marble-color r) p)
    (display " marble>" p)))

<graphic> #<medium quality blue marble>

The record writer is used only when print-record is true (the default). When the parameter print-record is set to #f, records are printed using a compressed syntax that identifies only the type of record.

(parameterize ([print-record #f])
  (format "~s" m)) <graphic> "#<record of type marble>"

A print method may be called more than once during the printing of a single record to support cycle detection and graph printing (see print-graph), so print methods that perform side effects other than printing to the given port are discouraged. Whenever a print method is called more than once during the printing of a single record, in all but one call, a generic "bit sink" port is used to suppress output automatically so that only one copy of the object appears on the actual port. In order to avoid confusing the cycle detection and graph printing algorithms, a print method should always produce the same printed representation for each object. Furthermore, a print method should normally use the supplied procedure wr to print subobjects, though atomic values, such as strings or numbers, may be printed by direct calls to display or write or by other means.

(let ()
  (define-record ref () ((contents 'nothing)))
  (record-writer (type-descriptor ref)
    (lambda (r p wr)
      (display "<" p)
      (wr (ref-contents r) p)
      (display ">" p)))
  (let ([ref-lexive (make-ref)])
    (set-ref-contents! ref-lexive ref-lexive)
    ref-lexive)) <graphic> #0=<#0#>

Print methods need not be concerned with handling nonfalse values of the parameters print-level. The printer handles print-level automatically even when user-defined print procedures are used. Since records typically contain a small, fixed number of fields, it is usually possible to ignore nonfalse values of print-length as well.

(print-level 3)
(let ()
  (define-record ref () ((contents 'nothing)))
  (record-writer (type-descriptor ref)
    (lambda (r p wr)
      (display "<" p)
      (wr (ref-contents r) p)
      (display ">" p)))
  (let ([ref-lexive (make-ref)])
    (set-ref-contents! ref-lexive ref-lexive)
    ref-lexive)) <graphic> <<<<#[...]>>>>

thread parameter: print-record
libraries: (chezscheme)

This parameter controls the printing of records. If set to true (the default) the record writer associated with a record type is used to print records of that type. If set to false, all records are printed with the syntax #<record of type name>, where name is the name of the record type as returned by record-type-name.

procedure: (make-record-type type-name fields)
procedure: (make-record-type parent-rtd type-name fields)
returns: a record-type descriptor for a new record type
libraries: (chezscheme)

make-record-type creates a new data type and returns a record-type descriptor, a value representing the new data type. The new type is disjoint from all others.

If present, parent-rtd must be a record-type descriptor.

type-name must be a string or gensym. If type-name is a string, a new record type is generated. If type-name is a gensym, a new record type is generated only if one with the same gensym has not already been defined. If one has already been defined, the parent and fields must be identical to those of the existing record type, and the existing record type is used. If the parent and fields are not identical, an exception is raised with condition-type &assertion.

fields must be a list of field descriptors, each of which describes one field of instances of the new record type. A field descriptor is either a symbol or a list in the following form:

(class type field-name)

where class and type are optional. field-name must be a symbol. class, if present, must be the symbol immutable or the symbol mutable. If the immutable class-specifier is present, the field is immutable; otherwise, the field is mutable. type, if present, specifies how the field is represented. The types are the same as those given in the description of define-record on page 175.

If a type is specified, the field can contain objects only of the specified type. If no type is specified, the field is of type ptr, meaning that it can contain any Scheme object.

The behavior of a program that modifies the string type-name or the list fields or any of its sublists is unspecified.

The record-type descriptor may be passed as an argument to any of the Revised6 Report procedures

or to the Chez Scheme variants

to obtain procedures for creating and manipulating records of the new type.

(define marble
  (make-record-type "marble"
    '(color quality)
    (lambda (r p wr)
      (display "#<" p)
      (wr (marble-quality r) p)
      (display " quality " p)
      (wr (marble-color r) p)
      (display " marble>" p))))
(define make-marble
  (record-constructor marble))
(define marble?
  (record-predicate marble))
(define marble-color
  (record-field-accessor marble 'color))
(define marble-quality
  (record-field-accessor marble 'quality))
(define set-marble-quality!
  (record-field-mutator marble 'quality))
(define x (make-marble 'blue 'high))
(marble? x) <graphic> #t
(marble-quality x) <graphic> high
(set-marble-quality! x 'low)
(marble-quality x) <graphic> low
<graphic> #<low quality blue marble>

The order in which the fields appear in fields is important. While field names are generally distinct, it is permissible for one field name to be the same as another in the list of fields or the same as an inherited name. In this case, field ordinals must be used to select fields in calls to record-field-accessor and record-field-mutator. Ordinals range from zero through one less than the number of fields. Parent fields come first, if any, followed by the fields in fields, in the order given.

(define r1 (make-record-type "r1" '(t t)))
(define r2 (make-record-type r1 "r2" '(t)))
(define r3 (make-record-type r2 "r3" '(t t t)))

(define x ((record-constructor r3) 'a 'b 'c 'd 'e 'f))
((record-field-accessor r3 0) x) <graphic> a
((record-field-accessor r3 2) x) <graphic> c
((record-field-accessor r3 4) x) <graphic> e
((record-field-accessor r3 't) x) <graphic> unspecified

procedure: (record-constructor rcd)
procedure: (record-constructor rtd)
returns: a constructor for records of the type represented by rtd
libraries: (chezscheme)

Like the Revised6 Report version of this procedure, this procedure may be passed a record-constructor descriptor, rcd, which determines the behavior of the constructor. It may also be passed a record-type descriptor, rtd, in which case the constructor accepts as many arguments as there are fields in the record; these arguments are the initial values of the fields in the order given when the record-type descriptor was created.

procedure: (record-field-accessor rtd field-id)
returns: an accessor for the identified field
libraries: (chezscheme csv7)

rtd must be a record-type descriptor, field-id must be a symbol or field ordinal, i.e., a nonnegative exact integer less than the number of fields of the given record type. The specified field must be accessible.

The generated accessor expects one argument, which must be a record of the type represented by rtd. It returns the contents of the specified field of the record.

procedure: (record-field-accessible? rtd field-id)
returns: #t if the specified field is accessible, otherwise #f
libraries: (chezscheme csv7)

rtd must be a record-type descriptor, field-id must be a symbol or field ordinal, i.e., a nonnegative exact integer less than the number of fields of the given record type.

The compiler is free to eliminate a record field if it can prove that the field is not accessed. In making this determination, the compiler is free to ignore the possibility that an accessor might be created from a record-type descriptor obtained by calling record-type-descriptor on an instance of the record type.

procedure: (record-field-mutator rtd field-id)
returns: a mutator for the identified field
libraries: (chezscheme csv7)

rtd must be a record-type descriptor, field-id must be a symbol or field ordinal, i.e., a nonnegative exact integer less than the number of fields of the given record type. The specified field must be mutable.

The mutator expects two arguments, r and obj. r must be a record of the type represented by rtd. obj must be a value that is compatible with the type declared for the specified field when the record-type descriptor was created. obj is stored in the specified field of the record.

procedure: (record-field-mutable? rtd field-id)
returns: #t if the specified field is mutable, otherwise #f
libraries: (chezscheme csv7)

rtd must be a record-type descriptor, field-id must be a symbol or field ordinal, i.e., a nonnegative exact integer less than the number of fields of the given record type.

Any field declared immutable is immutable. In addition, the compiler is free to treat a field as immutable if it can prove that the field is never assigned. In making this determination, the compiler is free to ignore the possibility that a mutator might be created from a record-type descriptor obtained by calling record-type-descriptor on an instance of the record type.

procedure: (record-type-name rtd)
returns: the name of the record-type represented by rtd
libraries: (chezscheme csv7)

rtd must be a record-type descriptor.

The name is a always a string. If a gensym is provided as the record-type name in a define-record form or make-record-type call, the result is the "pretty" name of the gensym (see 7.9).

(record-type-name (make-record-type "empty" '())) <graphic> "empty"

(define-record #{point bdhavk1bwafxyss1-b} (x y))
(define p (type-descriptor #{point bdhavk1bwafxyss1-b}))
(record-type-name p) <graphic> "point"

procedure: (record-type-symbol rtd)
returns: the generated symbol associated with rtd
libraries: (chezscheme csv7)

rtd must be a record-type descriptor.

(define e (make-record-type "empty" '()))
(record-type-symbol e) <graphic> #{empty bdhavk1bwafxyss1-e}

(define-record #{point bdhavk1bwafxyss1-b} (x y))
(define p (type-descriptor #{point bdhavk1bwafxyss1-b}))
(record-type-symbol p) <graphic> #{point bdhavk1bwafxyss1-b}

procedure: (record-type-field-names rtd)
returns: a list of field names of the type represented by rtd
libraries: (chezscheme csv7)

rtd must be a record-type descriptor. The field names are symbols.

(define-record triple ((immutable x1) (mutable x2) (immutable x3)))
(record-type-field-names (type-descriptor triple)) <graphic> (x1 x2 x3)

procedure: (record-type-field-decls rtd)
returns: a list of field declarations of the type represented by rtd
libraries: (chezscheme csv7)

rtd must be a record-type descriptor. Each field declaration has the following form:

(class type field-name)

where class, type, and field-name are as described under make-record-type.

(define-record shape (x y))
(define-record circle shape (radius))

(record-type-field-decls
  (type-descriptor circle)) <graphic> ((mutable ptr x)
                              (mutable ptr y)
                              (mutable ptr radius))

procedure: (record? obj)
returns: #t if obj is a record, otherwise #f
procedure: (record? obj rtd)
returns: #t if obj is a record of the given type, otherwise #f
libraries: (chezscheme)

If present, rtd must be a record-type descriptor.

A record is "of the given type" if it is an instance of the record type or one of its ancestors. The predicate generated by record-predicate for a record-type descriptor rtd is equivalent to the following.

(lambda (x) (record? x rtd))

procedure: (record-type-descriptor rec)
returns: the record-type descriptor of rec
libraries: (chezscheme csv7)

rec must be a record. This procedure is intended for use in the definition of portable printers and debuggers. For records created with make-record-type, it may not be the same as the descriptor returned by make-record-type. See the comments about field accessibility and mutability under record-field-accessible? and record-field-mutable? above.

This procedure is equivalent to the Revised6 Report record-rtd procedure.

(define rtd (make-record-type "frob" '(blit blat)))
rtd <graphic> #<record type frob>
(define x ((record-constructor rtd) 1 2))
(record-type-descriptor x) <graphic> #<record type frob>
(eq? (record-type-descriptor x) rtd) <graphic> unspecified

Section 7.16. Procedures

procedure: (procedure-arity-mask proc)
returns: an exact integer bitmask identifying the accepted argument counts of proc
libraries: (chezscheme)

The bitmask is represented as two's complement number with the bit at each index n set if and only if proc accepts n arguments.

The two's complement encoding implies that if proc accepts n or more arguments, the encoding is a negative number, since all the bits from n and up are set. For example, if proc accepts any number of arguments, the two's complement encoding of all bits set is -1.

(procedure-arity-mask (lambda () 'none)) <graphic> 1
(procedure-arity-mask car) <graphic> 2
(procedure-arity-mask (case-lambda [() 'none] [(x) x])) <graphic> 3
(procedure-arity-mask (lambda x x)) <graphic> -1
(procedure-arity-mask (case-lambda [() 'none] [(x y . z) x])) <graphic> -3
(procedure-arity-mask (case-lambda)) <graphic> 0
(logbit? 1 (procedure-arity-mask pair?)) <graphic> #t
(logbit? 2 (procedure-arity-mask pair?)) <graphic> #f
(logbit? 2 (procedure-arity-mask cons)) <graphic> #t

Chez Scheme Version 9 User's Guide
Copyright © 2023 Cisco Systems, Inc.
Licensed under the Apache License Version 2.0 (full copyright notice.).
Revised October 2023 for Chez Scheme Version 9.6.4
about this book