Many Voderberg tile spirals, warped in euclidean space, rendered as if convex.

Chapter 6. Operations on Objects

This chapter describes the operations on objects, including lists, numbers, characters, strings, vectors, and symbols. The first section covers constant objects and quotation. The second section describes generic equivalence predicates for comparing two objects and predicates for determining the type of an object. Later sections describe procedures that deal primarily with one of the object types mentioned above. There is no section treating operations on procedures, since the only operation defined specifically for procedures is application, and this is described in Chapter 5. Operations on ports are covered in the more general discussion of input and output in Chapter 7.

Section 6.1. Constants and Quotation


syntax: constant
returns: constant

constant is any self-evaluating constant, i.e., a number, boolean, character, or string. Constants are immutable; see the note in the description of quote below.

3.2 <graphic> 3.2
#f <graphic> #f
#\c <graphic> #\c
"hi" <graphic> "hi"


syntax: (quote obj)
syntax: 'obj
returns: obj

'obj is equivalent to (quote obj). The abbreviated form is converted into the longer form by the Scheme reader (see read).

quote inhibits the normal evaluation rule for obj, allowing obj to be employed as data. Although any Scheme object may be quoted, quotation is not necessary for self-evaluating constants, i.e., numbers, booleans, characters, and strings.

Quoted and self-evaluating constants are immutable. That is, it is an error to alter a constant via set-car!, string-set!, etc. An implementation may choose to share storage among different constants to save space.

(+ 2 3) <graphic> 5
'(+ 2 3) <graphic> (+ 2 3)
(quote (+ 2 3)) <graphic> (+ 2 3)
'a <graphic> a
'cons <graphic> cons
'() <graphic> ()
'7 <graphic> 7


syntax: (quasiquote obj)
syntax: `obj
syntax: (unquote obj)
syntax: ,obj
syntax: (unquote-splicing obj)
syntax: ,@obj
returns: see explanation

`obj is equivalent to (quasiquote obj), ,obj is equivalent to (unquote obj), and ,@obj is equivalent to (unquote-splicing obj). The abbreviated forms are converted into the longer forms by the Scheme reader (see read).

quasiquote is similar to quote, but it allows parts of the quoted text to be "unquoted." Within a quasiquote expression, subforms of unquote and unquote-splicing forms are evaluated, and everything else is quoted, i.e., left unevaluated. The value of each unquote subform is inserted into the output in place of the unquote form, while the value of each unquote-splicing subform is spliced into the surrounding list or vector structure. unquote and unquote-splicing are valid only within quasiquote expressions.

quasiquote expressions may be nested, with each quasiquote introducing a new level of quotation and each unquote or unquote-splicing taking away a level of quotation. An expression nested within n quasiquote expressions must be within n unquote or unquote-splicing expressions to be evaluated.

`(+ 2 3) <graphic> (+ 2 3)

`(+ 2 ,(* 3 4)) <graphic> (+ 2 12)
`(a b (,(+ 2 3) c) d) <graphic> (a b (5 c) d)
`(a b ,(reverse '(c d e)) f g) <graphic> (a b (e d c) f g)
(let ((a 1) (b 2))
  `(,a . ,b)) <graphic> (1 . 2)

`(+ ,@(cdr '(* 2 3))) <graphic> (+ 2 3)
`(a b ,@(reverse '(c d e)) f g) <graphic> (a b e d c f g)
(let ((a 1) (b 2))
  `(,a ,@b)) <graphic> (1 . 2)
`#(,@(list 1 2 3)) <graphic> #(1 2 3)

'`,(cons 'a 'b) <graphic> `,(cons 'a 'b)
`',(cons 'a 'b) <graphic> '(a . b)

Section 6.2. Generic Equivalence and Type Predicates

This section describes the basic Scheme predicates (procedures returning one of the boolean values #t or #f) for determining the type of an object or the equivalence of two objects. The equivalence predicates eq?, eqv?, and equal? are discussed first, followed by the type predicates.


procedure: (eq? obj1 obj2)
returns: #t if obj1 and obj2 are identical, #f otherwise

In most Scheme systems, two objects are considered identical if they are represented internally by the same pointer value and distinct (not identical) if they are represented internally by different pointer values, although other criteria, such as time-stamping, are possible.

Although the particular rules for object identity vary somewhat from system to system, the following rules always hold.

eq? cannot be used to compare numbers and characters reliably. Although every inexact number is distinct from every exact number, two exact numbers, two inexact numbers, or two characters with the same value may or may not be identical.

Since constant objects are immutable, i.e., it is an error to modify one, all or portions of different quoted constants or self-evaluating literals may be represented internally by the same object. Thus, eq? may return #t when applied to equal parts of different immutable constants.

(eq? 'a 3) <graphic> #f
(eq? #t 't) <graphic> #f
(eq? "abc" 'abc) <graphic> #f
(eq? "hi" '(hi)) <graphic> #f
(eq? #f '()) <graphic> #f

(eq? 9/2 7/2) <graphic> #f
(eq? 3.4 53344) <graphic> #f
(eq? 3 3.0) <graphic> #f
(eq? 1/3 #i1/3) <graphic> #f

(eq? 9/2 9/2) <graphic> unspecified
(eq? 3.4 (+ 3.0 .4)) <graphic> unspecified
(let ((x (* 12345678987654321 2)))
  (eq? x x)) <graphic> unspecified

(eq? #\a #\b) <graphic> #f
(eq? #\a #\a) <graphic> unspecified
(let ((x (string-ref "hi" 0)))
  (eq? x x)) <graphic> unspecified

(eq? #t #t) <graphic> #t
(eq? #f #f) <graphic> #t
(eq? #t #f) <graphic> #f
(eq? (null? '()) #t) <graphic> #t
(eq? (null? '(a)) #f) <graphic> #t

(eq? (cdr '(a)) '()) <graphic> #t

(eq? 'a 'a) <graphic> #t
(eq? 'a 'b) <graphic> #f
(eq? 'a (string->symbol "a")) <graphic> #t

(eq? '(a) '(b)) <graphic> #f
(eq? '(a) '(a)) <graphic> unspecified
(let ((x '(a . b))) (eq? x x)) <graphic> #t
(let ((x (cons 'a 'b)))
  (eq? x x)) <graphic> #t
(eq? (cons 'a 'b) (cons 'a 'b)) <graphic> #f

(eq? "abc" "cba") <graphic> #f
(eq? "abc" "abc") <graphic> unspecified
(let ((x "hi")) (eq? x x)) <graphic> #t
(let ((x (string #\h #\i))) (eq? x x)) <graphic> #t
(eq? (string #\h #\i)
     (string #\h #\i)) <graphic> #f

(eq? '#(a) '#(b)) <graphic> #f
(eq? '#(a) '#(a)) <graphic> unspecified
(let ((x '#(a))) (eq? x x)) <graphic> #t
(let ((x (vector 'a)))
  (eq? x x)) <graphic> #t
(eq? (vector 'a) (vector 'a)) <graphic> #f

(eq? car car) <graphic> #t
(eq? car cdr) <graphic> #f
(let ((f (lambda (x) x)))
  (eq? f f)) <graphic> #t
(let ((f (lambda () (lambda (x) x))))
  (eq? (f) (f))) <graphic> unspecified
(eq? (lambda (x) x) (lambda (y) y)) <graphic> unspecified

(let ((f (lambda (x)
           (lambda ()
             (set! x (+ x 1))
             x))))
  (eq? (f 0) (f 0))) <graphic> #f


procedure: (eqv? obj1 obj2)
returns: #t if obj1 and obj2 are equivalent, #f otherwise

eqv? is similar to eq? except that eqv? is guaranteed to return #t for two exact numbers, two inexact numbers, or two characters with the same value (by = or char=?). eqv? is less implementation-dependent but generally more expensive than eq?. eqv? might be defined as follows.

(define eqv?
  (lambda (x y)
    (cond
      ((eq? x y))
      ((number? x)
       (and (number? y)
            (if (exact? x)
                (and (exact? y) (= x y))
                (and (inexact? y) (= x y)))))
      ((char? x) (and (char? y) (char=? x y)))
      (else #f))))

(eqv? 'a 3) <graphic> #f
(eqv? #t 't) <graphic> #f
(eqv? "abc" 'abc) <graphic> #f
(eqv? "hi" '(hi)) <graphic> #f
(eqv? #f '()) <graphic> #f

(eqv? 9/2 7/2) <graphic> #f
(eqv? 3.4 53344) <graphic> #f
(eqv? 3 3.0) <graphic> #f
(eqv? 1/3 #i1/3) <graphic> #f

(eqv? 9/2 9/2) <graphic> #t
(eqv? 3.4 (+ 3.0 .4)) <graphic> #t
(let ((x (* 12345678987654321 2)))
  (eqv? x x)) <graphic> #t

(eqv? #\a #\b) <graphic> #f
(eqv? #\a #\a) <graphic> #t
(let ((x (string-ref "hi" 0)))
  (eqv? x x)) <graphic> #t

(eqv? #t #t) <graphic> #t
(eqv? #f #f) <graphic> #t
(eqv? #t #f) <graphic> #f
(eqv? (null? '()) #t) <graphic> #t
(eqv? (null? '(a)) #f) <graphic> #t

(eqv? (cdr '(a)) '()) <graphic> #t

(eqv? 'a 'a) <graphic> #t
(eqv? 'a 'b) <graphic> #f
(eqv? 'a (string->symbol "a")) <graphic> #t

(eqv? '(a) '(b)) <graphic> #f
(eqv? '(a) '(a)) <graphic> unspecified
(let ((x '(a . b))) (eqv? x x)) <graphic> #t
(let ((x (cons 'a 'b)))
  (eqv? x x)) <graphic> #t
(eqv? (cons 'a 'b) (cons 'a 'b)) <graphic> #f

(eqv? "abc" "cba") <graphic> #f
(eqv? "abc" "abc") <graphic> unspecified
(let ((x "hi")) (eqv? x x)) <graphic> #t
(let ((x (string #\h #\i))) (eqv? x x)) <graphic> #t
(eqv? (string #\h #\i)
      (string #\h #\i)) <graphic> #f

(eqv? '#(a) '#(b)) <graphic> #f
(eqv? '#(a) '#(a)) <graphic> unspecified
(let ((x '#(a))) (eqv? x x)) <graphic> #t
(let ((x (vector 'a)))
  (eqv? x x)) <graphic> #t
(eqv? (vector 'a) (vector 'a)) <graphic> #f

(eqv? car car) <graphic> #t
(eqv? car cdr) <graphic> #f
(let ((f (lambda (x) x)))
  (eqv? f f)) <graphic> #t
(let ((f (lambda () (lambda (x) x))))
  (eqv? (f) (f))) <graphic> unspecified
(eqv? (lambda (x) x) (lambda (y) y)) <graphic> unspecified

(let ((f (lambda (x)
           (lambda ()
             (set! x (+ x 1))
             x))))
  (eqv? (f 0) (f 0))) <graphic> #f


procedure: (equal? obj1 obj2)
returns: #t if obj1 and obj2 have the same structure and contents, #f otherwise

Two objects are equal if they are equivalent according to eqv? or if they are strings that are string=?, pairs whose cars and cdrs are equal, or vectors of the same length whose corresponding elements are equal.

equal? is recursively defined and must compare not only numbers and characters for equivalence but also pairs, strings, and vectors. The result is that equal? is less discriminating than either eq? or eqv?. It is also likely to be more expensive.

equal? might be defined as follows.

(define equal?
  (lambda (x y)
    (cond
      ((eqv? x y))
      ((pair? x)
       (and (pair? y)
            (equal? (car x) (car y))
            (equal? (cdr x) (cdr y))))
      ((string? x) (and (string? y) (string=? x y)))
      ((vector? x)
       (and (vector? y)
            (let ((n (vector-length x)))
              (and (= (vector-length y) n)
                   (let loop ((i 0))
                     (or (= i n)
                         (and (equal? (vector-ref x i) (vector-ref y i))
                              (loop (+ i 1)))))))))
      (else #f))))

(equal? 'a 3) <graphic> #f
(equal? #t 't) <graphic> #f
(equal? "abc" 'abc) <graphic> #f
(equal? "hi" '(hi)) <graphic> #f
(equal? #f '()) <graphic> #f

(equal? 9/2 7/2) <graphic> #f
(equal? 3.4 53344) <graphic> #f
(equal? 3 3.0) <graphic> #f
(equal? 1/3 #i1/3) <graphic> #f

(equal? 9/2 9/2) <graphic> #t
(equal? 3.4 (+ 3.0 .4)) <graphic> #t
(let ((x (* 12345678987654321 2)))
  (equal? x x)) <graphic> #t

(equal? #\a #\b) <graphic> #f
(equal? #\a #\a) <graphic> #t
(let ((x (string-ref "hi" 0)))
  (equal? x x)) <graphic> #t

(equal? #t #t) <graphic> #t
(equal? #f #f) <graphic> #t
(equal? #t #f) <graphic> #f
(equal? (null? '()) #t) <graphic> #t
(equal? (null? '(a)) #f) <graphic> #t

(equal? (cdr '(a)) '()) <graphic> #t

(equal? 'a 'a) <graphic> #t
(equal? 'a 'b) <graphic> #f
(equal? 'a (string->symbol "a")) <graphic> #t

(equal? '(a) '(b)) <graphic> #f
(equal? '(a) '(a)) <graphic> #t
(let ((x '(a . b))) (equal? x x)) <graphic> #t
(let ((x (cons 'a 'b)))
  (equal? x x)) <graphic> #t
(equal? (cons 'a 'b) (cons 'a 'b)) <graphic> #t

(equal? "abc" "cba") <graphic> #f
(equal? "abc" "abc") <graphic> #t
(let ((x "hi")) (equal? x x)) <graphic> #t
(let ((x (string #\h #\i))) (equal? x x)) <graphic> #t
(equal? (string #\h #\i)
        (string #\h #\i)) <graphic> #t

(equal? '#(a) '#(b)) <graphic> #f
(equal? '#(a) '#(a)) <graphic> #t
(let ((x '#(a))) (equal? x x)) <graphic> #t
(let ((x (vector 'a)))
  (equal? x x)) <graphic> #t
(equal? (vector 'a) (vector 'a)) <graphic> #t

(equal? car car) <graphic> #t
(equal? car cdr) <graphic> #f
(let ((f (lambda (x) x)))
  (equal? f f)) <graphic> #t
(let ((f (lambda () (lambda (x) x))))
  (equal? (f) (f))) <graphic> unspecified
(equal? (lambda (x) x) (lambda (y) y)) <graphic> unspecified

(let ((f (lambda (x)
           (lambda ()
             (set! x (+ x 1))
             x))))
  (equal? (f 0) (f 0))) <graphic> #f


procedure: (boolean? obj)
returns: #t if obj is either #t or #f, #f otherwise

boolean? is equivalent to (lambda (x) (or (eq? x #t) (eq? x #f))).

(boolean? #t) <graphic> #t
(boolean? #f) <graphic> #t
(boolean? 't) <graphic> #f
(boolean? '()) <graphic> #f


procedure: (null? obj)
returns: #t if obj is the empty list, #f otherwise

null? is equivalent to (lambda (x) (eq? x '())).

(null? '()) <graphic> #t
(null? '(a)) <graphic> #f
(null? (cdr '(a))) <graphic> #t
(null? 3) <graphic> #f
(null? #f) <graphic> #f


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

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


procedure: (number? obj)
returns: #t if obj is a number, #f otherwise
procedure: (complex? obj)
returns: #t if obj is a complex number, #f otherwise
procedure: (real? obj)
returns: #t if obj is a real number, #f otherwise
procedure: (rational? obj)
returns: #t if obj is a rational number, #f otherwise
procedure: (integer? obj)
returns: #t if obj is an integer, #f otherwise

These predicates form a hierarchy: any integer is rational, any rational is real, any real is complex, and any complex is numeric. Most implementations do not provide internal representations for irrational numbers, so all real numbers are typically rational as well.

(integer? 1901) <graphic> #t
(rational? 1901) <graphic> #t
(real? 1901) <graphic> #t
(complex? 1901) <graphic> #t
(number? 1901) <graphic> #t

(integer? -3.0) <graphic> #t
(rational? -3.0) <graphic> #t
(real? -3.0) <graphic> #t
(complex? -3.0) <graphic> #t
(number? -3.0) <graphic> #t

(integer? 7.0+0.0i) <graphic> #t
(rational? 7.0+0.0i) <graphic> #t
(real? 7.0+0.0i) <graphic> #t
(complex? 7.0+0.0i) <graphic> #t
(number? 7.0+0.0i) <graphic> #t

(integer? -2/3) <graphic> #f
(rational? -2/3) <graphic> #t
(real? -2/3) <graphic> #t
(complex? -2/3) <graphic> #t
(number? -2/3) <graphic> #t

(integer? -2.345) <graphic> #f
(rational? -2.345) <graphic> #t
(real? -2.345) <graphic> #t
(complex? -2.345) <graphic> #t
(number? -2.345) <graphic> #t

(integer? 3.2-2.01i) <graphic> #f
(rational? 3.2-2.01i) <graphic> #f
(real? 3.2-2.01i) <graphic> #f
(complex? 3.2-2.01i) <graphic> #t
(number? 3.2-2.01i) <graphic> #t

(integer? 'a) <graphic> #f
(rational? '(a b c)) <graphic> #f
(real? "3") <graphic> #f
(complex? #(1 2)) <graphic> #f
(number? #\a) <graphic> #f


procedure: (char? obj)
returns: #t if obj is a character, #f otherwise

(char? 'a) <graphic> #f
(char? 97) <graphic> #f
(char? #\a) <graphic> #t
(char? "a") <graphic> #f
(char? (string-ref (make-string 1) 0)) <graphic> #t


procedure: (string? obj)
returns: #t if obj is a string, #f otherwise

(string? "hi") <graphic> #t
(string? 'hi) <graphic> #f
(string? #\h) <graphic> #f


procedure: (vector? obj)
returns: #t if obj is a vector, #f otherwise

(vector? '#()) <graphic> #t
(vector? '#(a b c)) <graphic> #t
(vector? (vector 'a 'b 'c)) <graphic> #t
(vector? '()) <graphic> #f
(vector? '(a b c)) <graphic> #f
(vector? "abc") <graphic> #f


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

(symbol? 't) <graphic> #t
(symbol? "t") <graphic> #f
(symbol? '(t)) <graphic> #f
(symbol? #\t) <graphic> #f
(symbol? 3) <graphic> #f
(symbol? #t) <graphic> #f


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

(procedure? car) <graphic> #t
(procedure? 'car) <graphic> #f
(procedure? (lambda (x) x)) <graphic> #t
(procedure? '(lambda (x) x)) <graphic> #f
(call/cc procedure?) <graphic> #t

Section 6.3. Lists and Pairs

The pair, or cons cell, is the most fundamental of Scheme's structured object types. The most common use for pairs is to build lists, which are ordered sequences of pairs linked one to the next by the cdr field. The elements of the list occupy the car fields of the pairs. The cdr of the last pair in a proper list is the empty list, (); the cdr of the last pair in an improper list can be anything other than ().

Pairs may be used to construct binary trees. Each pair in the tree structure is an internal node of the binary tree; its car and cdr are the children of the node.

Proper lists are printed as sequences of objects separated by whitespace (that is, blanks, tabs, and newlines) and enclosed in parentheses. Brackets ( [ ] ) may also be used in some Scheme systems. For example, (1 2 3) and (a (nested list)) are proper lists. The empty list is written as ().

Improper lists and trees require a slightly more complex syntax. A single pair is written as two objects separated by whitespace and a dot, e.g., (a . b). This is referred to as dotted-pair notation. Improper lists and trees are also written in dotted-pair notation; the dot appears wherever necessary, e.g., (1 2 3 . 4) or ((1 . 2) . 3). Proper lists may be written in dotted-pair notation as well. For example, (1 2 3) may be written as (1 . (2 . (3 . ()))).

Unless otherwise stated, it is an error to pass an improper list to a procedure requiring a list argument.

It is possible to create a circular list or a cyclic graph by destructively altering the car or cdr field of a pair, using set-car! or set-cdr!. Some of the procedures listed in this section may loop indefinitely when handed a cyclic structure.


procedure: (cons obj1 obj2)
returns: a new pair whose car and cdr are obj1 and obj2

cons is the pair constructor procedure. obj1 becomes the car and obj2 becomes the cdr of the new pair.

(cons 'a '()) <graphic> (a)
(cons 'a '(b c)) <graphic> (a b c)
(cons 3 4) <graphic> (3 . 4)


procedure: (car pair)
returns: the car of pair

It is an error to ask for the car of the empty list.

(car '(a)) <graphic> a
(car '(a b c)) <graphic> a
(car (cons 3 4)) <graphic> 3


procedure: (cdr pair)
returns: the cdr of pair

It is an error to ask for the cdr of the empty list.

(cdr '(a)) <graphic> ()
(cdr '(a b c)) <graphic> (b c)
(cdr (cons 3 4)) <graphic> 4


procedure: (set-car! pair obj)
returns: unspecified

set-car! changes the car of pair to obj.

(let ((x '(a b c)))
  (set-car! x 1)
  x) <graphic> (1 b c)


procedure: (set-cdr! pair obj)
returns: unspecified

set-cdr! changes the cdr of pair to obj.

(let ((x '(a b c)))
  (set-cdr! x 1)
  x) <graphic> (a . 1)


procedure: (caar pair)
procedure: (cadr pair)
<graphic>
procedure: (cddddr pair)
returns: the caar, cadr, ..., or cddddr of pair

These procedures are defined as the composition of up to four cars and cdrs. The a's and d's between the c and r represent the application of car or cdr in order from right to left. For example, the procedure cadr applied to a pair yields the car of the cdr of the pair and is equivalent to (lambda (x) (car (cdr x))).

(caar '((a))) <graphic> a
(cadr '(a b c)) <graphic> b
(cdddr '(a b c d)) <graphic> (d)
(cadadr '(a (b c))) <graphic> c


procedure: (list obj ...)
returns: a list of obj ...

list is equivalent to (lambda x x).

(list) <graphic> ()
(list 1 2 3) <graphic> (1 2 3)
(list 3 2 1) <graphic> (3 2 1)


procedure: (list? obj)
returns: #t if obj is a proper list, #f otherwise

list? must return #f for all improper lists, including cyclic lists. A definition of list? is shown on page 64.

(list? '()) <graphic> #t
(list? '(a b c)) <graphic> #t
(list? 'a) <graphic> #f
(list? '(3 . 4)) <graphic> #f
(list? 3) <graphic> #f
(let ((x (list 'a 'b 'c)))
  (set-cdr! (cddr x) x)
  (list? x)) <graphic> #f


procedure: (length list)
returns: the number of elements in list

length may be defined as follows.

(define length
  (lambda (ls)
    (let loop ((ls ls) (n 0))
      (if (null? ls)
          n
          (loop (cdr ls) (+ n 1))))))

(length '()) <graphic> 0
(length '(a b c)) <graphic> 3


procedure: (list-ref list n)
returns: the nth element (zero-based) of list

n must be an exact nonnegative integer strictly less than the length of list. list-ref may be defined as follows.

(define list-ref
  (lambda (ls n)
    (if (= n 0)
        (car ls)
        (list-ref (cdr ls) (- n 1)))))

(list-ref '(a b c) 0) <graphic> a
(list-ref '(a b c) 1) <graphic> b
(list-ref '(a b c) 2) <graphic> c


procedure: (list-tail list n)
returns: the nth tail (zero-based) of list

n must be an exact nonnegative integer less than or equal to the length of list. The result is not a copy; the tail is eq? to the nth cdr of list (or to list itself, if n is zero).

list-tail appears in the Revised5 Report but not in the ANSI/IEEE standard. It may be defined as follows.

(define list-tail
  (lambda (ls n)
    (if (= n 0)
        ls
        (list-tail (cdr ls) (- n 1)))))

(list-tail '(a b c) 0) <graphic> (a b c)
(list-tail '(a b c) 2) <graphic> (c)
(list-tail '(a b c) 3) <graphic> ()
(list-tail '(a b c . d) 2) <graphic> (c . d)
(list-tail '(a b c . d) 3) <graphic> d
(let ((x (list 1 2 3)))
  (eq? (list-tail x 2)
       (cddr x))) <graphic> #t


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

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. The new list is made from new pairs for all arguments but the last; the last (which need not actually be a list) is merely placed at the end of the new structure. append may be defined as follows.

(define append
  (lambda args
    (let f ((ls '()) (args args))
      (if (null? args)
          ls
          (let g ((ls ls))
            (if (null? ls)
                (f (car args) (cdr args))
                (cons (car ls) (g (cdr ls)))))))))

(append '(a b c) '()) <graphic> (a b c)
(append '() '(a b c)) <graphic> (a b c)
(append '(a b) '(c d)) <graphic> (a b c d)
(append '(a b) 'c) <graphic> (a b . c)
(let ((x (list 'b)))
  (eq? x (cdr (append '(a) x)))) <graphic> #t


procedure: (reverse list)
returns: a new list containing the elements of list in reverse order

reverse may be defined as follows.

(define reverse
  (lambda (ls)
    (let rev ((ls ls) (new '()))
      (if (null? ls)
          new
          (rev (cdr ls) (cons (car ls) new))))))

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


procedure: (memq obj list)
procedure: (memv obj list)
procedure: (member obj list)
returns: the first tail of list whose car is equivalent to obj, or #f

These procedures traverse the argument list in order, comparing the elements of list against obj. If an object equivalent to obj is found, the tail of the list whose first element is that object is returned. If the list contains more than one object equivalent to obj, the first tail whose first element is equivalent to obj is returned. If no object equivalent to obj is found, #f is returned. The equivalence test for memq is eq?, for memv is eqv?, and for member is equal?.

These procedures are most often used as predicates, but their names do not end with a question mark because they return a useful true value in place of #t. memq may be defined as follows.

(define memq
  (lambda (x ls)
    (cond
      ((null? ls) #f)
      ((eq? (car ls) x) ls)
      (else (memq x (cdr ls))))))

memv and member may be defined similarly, with eqv? and equal? in place of eq?.

(memq 'a '(b c a d e)) <graphic> (a d e)
(memq 'a '(b c d e g)) <graphic> #f
(memq 'a '(b a c a d a)) <graphic> (a c a d a)

(memv 3.4 '(1.2 2.3 3.4 4.5)) <graphic> (3.4 4.5)
(memv 3.4 '(1.3 2.5 3.7 4.9)) <graphic> #f
(let ((ls (list 'a 'b 'c)))
  (set-car! (memv 'b ls) 'z)
  ls) <graphic> (a z c)

(member '(b) '((a) (b) (c))) <graphic> ((b) (c))
(member '(d) '((a) (b) (c))) <graphic> #f
(member "b" '("a" "b" "c")) <graphic> ("b" "c")

(define count-occurrences
  (lambda (x ls)
    (cond
      ((memq x ls) =>
       (lambda (ls)
         (+ (count-occurrences x (cdr ls)) 1)))
      (else 0))))

(count-occurrences 'a '(a b c d a)) <graphic> 2


procedure: (assq obj alist)
procedure: (assv obj alist)
procedure: (assoc obj alist)
returns: first element of alist whose car is equivalent to obj, or #f

The argument alist must be an association list. An association list is a proper list whose elements are key-value pairs of the form (key . value). Associations are useful for storing information (values) associated with certain objects (keys).

These procedures traverse the association list, testing each key for equivalence with obj. If an equivalent key is found, the key-value pair is returned. Otherwise, #f is returned.

The equivalence test for assq is eq?, for assv is eqv?, and for assoc is equal?. assq may be defined as follows.

(define assq
  (lambda (x ls)
    (cond
      ((null? ls) #f)
      ((eq? (caar ls) x) (car ls))
      (else (assq x (cdr ls))))))

assv and assoc may be defined similarly, with eqv? and equal? in place of eq?.

(assq 'b '((a . 1) (b . 2))) <graphic> (b . 2)
(cdr (assq 'b '((a . 1) (b . 2)))) <graphic> 2
(assq 'c '((a . 1) (b . 2))) <graphic> #f

(assv 2/3 '((1/3 . 1) (2/3 . 2))) <graphic> (2/3 . 2)
(assv 2/3 '((1/3 . a) (3/4 . b))) <graphic> #f

(assoc '(a) '(((a) . a) (-1 . b))) <graphic> ((a) . a)
(assoc '(a) '(((b) . b) (a . c))) <graphic> #f

(let ((alist '((2 . a) (3 . b))))
  (set-cdr! (assv 3 alist) 'c)
  alist) <graphic> ((2 . a) (3 . c))

The interpreter given in Section 9.7 represents environments as association lists and uses assq for both variable lookup and assignment.

Section 6.4. Numbers

Scheme numbers may be classified as integers, rational numbers, real numbers, or complex numbers, although an implementation may support only a subset of these numeric classes. This classification is hierarchical, in that all integers are rational, all rational numbers are real, and all real numbers are complex. The predicates integer?, rational?, real?, and complex? described in Section 6.2 are used to determine into which of these classes a number falls.

A Scheme number may also be classified as exact or inexact, depending upon the quality of operations used to derive the number and the inputs to these operations. The predicates exact? and inexact? may be used to determine the exactness of a number. Most operations on numbers in Scheme are exactness preserving: if given exact operands they return exact values, and if given inexact operands or a combination of exact and inexact operands they return inexact values.

Exact integer and rational arithmetic is typically supported to arbitrary precision; the size of an integer or of the denominator or numerator of a ratio is limited only by system storage constraints. Although other representations are possible, inexact numbers are typically represented by floating-point numbers supported by the host computer's hardware or by system software. Complex numbers are typically represented as ordered pairs (real-part, imag-part), where real-part and imag-part are exact integers, exact rationals, or floating-point numbers.

Scheme numbers are written in a straightforward manner not much different from ordinary conventions for writing numbers. An exact integer is normally written as a sequence of numerals preceded by an optional sign. For example, 3, +19, -100000, and 208423089237489374 all represent exact integers.

An exact rational number is normally written as two sequences of numerals separated by a slash (/) and preceded by an optional sign. For example, 3/4, -6/5, and 1/1208203823 are all exact rational numbers. A ratio is reduced immediately when it is read and may in fact reduce to an exact integer.

Inexact integers and rational numbers are normally written in either floating-point or scientific notation. Floating-point notation consists of a sequence of numerals followed by a decimal point and another sequence of numerals, all preceded by an optional sign. Scientific notation consists of an optional sign, a sequence of numerals, an optional decimal point followed by a second string of numerals, and an exponent; an exponent is written as the letter e followed by an optional sign and a sequence of numerals. For example, 1.0 and -200.0 are valid inexact integers, and 1.5, 0.034, -10e-10 and 1.5e-5 are valid inexact rational numbers. The exponent is the power of ten by which the number preceding the exponent should be scaled, so that 2e3 is equivalent to 2000.0.

The special digit # (hash) may be used in place of a normal digit in certain contexts to signify that the value of the digit is unknown. Numbers that include hash digits are naturally inexact, even if they are written in the style of exact integers or rational numbers. Hash digits may appear after one or more nonhash digits to signify an inexact integer; after one or more nonhash digits in the first or second part of a ratio to specify an inexact rational number; or after one or more nonhash digits before or after the decimal point of an inexact number written in floating-point or scientific notation. No significant (known) digit may follow a hash digit. For example, 1####, -1#/2#, .123### and 1#.### all specify inexact quantities.

Exact and inexact real numbers are written as exact or inexact integers or rational numbers; no provision is made in the syntax of Scheme numbers for nonrational real numbers, i.e., irrational numbers.

Complex numbers may be written in either rectangular or polar form. In rectangular form, a complex number is written as x+yi or x-yi, where x is an integer, rational, or real number and y is an unsigned integer, rational, or real number. The real part, x, may be omitted, in which case it is assumed to be zero. For example, 3+4i, 3.2-3/4i, +i, and -3e-5i are complex numbers written in rectangular form. In polar form, a complex number is written as x@y, where x and y are integer, rational, or real numbers. For example, 1.1@1.764 and -1@-1/2 are complex numbers written in polar form.

The exactness of a numeric representation may be overridden by preceding the representation by either #e or #i. #e forces the number to be exact, and #i forces it to be inexact. For example, 1, #e1, 1/1, #e1/1, #e1.0, #e1e0, and #e1.## all represent the exact integer 1, and #i3/10, 3#/100, 0.3, #i0.3, and 3e-1 all represent the inexact rational 0.3.

Numbers are written by default in base 10, although the special prefixes #b (binary), #o (octal), #d (decimal), and #x (hexadecimal) can be used to specify base 2, base 8, base 10, or base 16. For radix 16, the letters a through f or A through F serve as the additional numerals required to express digit values 10 through 15. For example, #b10101 is the binary equivalent of 2110, #o72 is the octal equivalent of 5810, and #xC7 is the hexadecimal equivalent of 19910. Numbers written in floating-point and scientific notations are always written in base 10.

If both are present, radix and exactness prefixes may appear in either order.

A Scheme implementation may support more than one size of internal representation for inexact quantities. The exponent markers s (short), f (single), d (double), and l (long) may appear in place of the default exponent marker e to override the default size for numbers written in scientific notation. In implementations that support multiple representations, the default size has at least as much precision as double.

A precise grammar for Scheme numbers is included in the description of the formal syntax of Scheme starting on page 277.

Any number can be written in a variety of different ways, but the system printer (see write and display) and number->string express numbers in a compact form, using the fewest number of digits possible while retaining the property that, when read, the printed number is identical to the original number.

The remainder of this section describes procedures that operate on numbers. The type of numeric arguments accepted by these procedures is implied by the name given to the arguments: num for complex numbers (that is, all numbers), real for real numbers, rat for rational numbers, and int for integers.


procedure: (exact? num)
returns: #t if num is exact, #f otherwise

(exact? 1) <graphic> #t
(exact? -15/16) <graphic> #t
(exact? 2.01) <graphic> #f
(exact? #i77) <graphic> #f
(exact? #i2/3) <graphic> #f
(exact? 1.0-2i) <graphic> #f
(exact? -1#i) <graphic> #f


procedure: (inexact? num)
returns: #t if num is inexact, #f otherwise

(inexact? -123) <graphic> #f
(inexact? #i123) <graphic> #t
(inexact? 1e23) <graphic> #t
(inexact? 1###) <graphic> #t
(inexact? 1#/2#) <graphic> #t
(inexact? #e1#/2#) <graphic> #f
(inexact? +i) <graphic> #f


procedure: (= num1 num2 num3 ...)
procedure: (< real1 real2 real3 ...)
procedure: (> real1 real2 real3 ...)
procedure: (<= real1 real2 real3 ...)
procedure: (>= real1 real2 real3 ...)
returns: #t if the relation holds, #f otherwise

The predicate = returns #t if its arguments are equal. The predicate < returns #t if its arguments are monotonically increasing, i.e., each argument is greater than the preceding ones, while > returns #t if its arguments are monotonically decreasing. The predicate <= returns #t if its arguments are monotonically nondecreasing, i.e., each argument is not less than the preceding ones, while >= returns #t if its arguments are monotonically nonincreasing.

As implied by the names of the arguments, = is defined for complex arguments while the other relational predicates are defined only for real arguments. Two complex numbers are considered equal if their real and imaginary parts are equal.

(= 7 7) <graphic> #t
(= 7 9) <graphic> #f

(< 2e3 3e2) <graphic> #f
(<= 1 2 3 3 4 5) <graphic> #t
(<= 1 2 3 4 5) <graphic> #t

(> 1 2 2 3 3 4) <graphic> #f
(>= 1 2 2 3 3 4) <graphic> #f

(= -1/2 -0.5) <graphic> #t
(= 2/3 .667) <graphic> #f
(= 7.2+0i 7.2) <graphic> #t
(= 7.2-3i 7) <graphic> #f

(< 1/2 2/3 3/4) <graphic> #t
(> 8 4.102 2/3 -5) <graphic> #t

(let ((x 0.218723452))
  (< 0.210 x 0.220)) <graphic> #t

(let ((i 1) (v (vector 'a 'b 'c)))
  (< -1 i (vector-length v))) <graphic> #t

(apply < '(1 2 3 4)) <graphic> #t
(apply > '(4 3 3 2)) <graphic> #f


procedure: (+ num ...)
returns: the sum of the arguments num ...

When called with no arguments, + returns 0.

(+) <graphic> 0
(+ 1 2) <graphic> 3
(+ 1/2 2/3) <graphic> 7/6
(+ 3 4 5) <graphic> 12
(+ 3.0 4) <graphic> 7.0
(+ 3+4i 4+3i) <graphic> 7+7i
(apply + '(1 2 3 4 5)) <graphic> 15


procedure: (- num1)
procedure: (- num1 num2 num3 ...)
returns: see explanation

When called with one argument, - returns the negative of num1. Thus, (- num1) is an idiom for (- 0 num1).

When called with two or more arguments, - returns the result of subtracting the sum of the numbers num2 ... from num1.

The ANSI/IEEE standard includes only one- and two-argument variants. The more general form is included in the Revised5 Report.

(- 3) <graphic> -3
(- -2/3) <graphic> 2/3
(- 4 3.0) <graphic> 1.0
(- 3.25+4.25i 1/4+1/4i) <graphic> 3.0+4.0i
(- 4 3 2 1) <graphic> -2


procedure: (* num ...)
returns: the product of the arguments num ...

When called with no arguments, * returns 1.

(*) <graphic> 1
(* 3.4) <graphic> 3.4
(* 1 1/2) <graphic> 1/2
(* 3 4 5.5) <graphic> 66.0
(* 1+2i 3+4i) <graphic> -5+10i
(apply * '(1 2 3 4 5)) <graphic> 120


procedure: (/ num1)
procedure: (/ num1 num2 num3 ...)
returns: see explanation

When called with one argument, / returns the reciprocal of num1. That is, (/ num1) is an idiom for (/ 1 num1).

When called with two or more arguments, / returns the result of dividing num1 by the product of the remaining arguments num2 ....

The ANSI/IEEE standard includes only one- and two-argument variants. The more general form is included in the Revised5 Report.

(/ -17) <graphic> -1/17
(/ 1/2) <graphic> 2
(/ .5) <graphic> 2.0
(/ 3 4) <graphic> 3/4
(/ 3.0 4) <graphic> .75
(/ -5+10i 3+4i) <graphic> 1+2i
(/ 60 5 4 3 2) <graphic> 1/2


procedure: (zero? num)
returns: #t if num is zero, #f otherwise

zero? is equivalent to (lambda (x) (= x 0)).

(zero? 0) <graphic> #t
(zero? 1) <graphic> #f
(zero? (- 3.0 3.0)) <graphic> #t
(zero? (+ 1/2 1/2)) <graphic> #f
(zero? 0+0i) <graphic> #t
(zero? 0.0-0.0i) <graphic> #t


procedure: (positive? real)
returns: #t if real is greater than zero, #f otherwise

positive? is equivalent to (lambda (x) (> x 0)).

(positive? 128) <graphic> #t
(positive? 0.0) <graphic> #f
(positive? 1.8e-15) <graphic> #t
(positive? -2/3) <graphic> #f
(positive? .001-0.0i) <graphic> #t


procedure: (negative? real)
returns: #t if real is less than zero, #f otherwise

negative? is equivalent to (lambda (x) (< x 0)).

(negative? -65) <graphic> #t
(negative? 0) <graphic> #f
(negative? -0.0121) <graphic> #t
(negative? 15/16) <graphic> #f
(negative? -7.0+0.0i) <graphic> #t


procedure: (even? int)
returns: #t if int is even, #f otherwise

(even? 0) <graphic> #t
(even? 1) <graphic> #f
(even? 2.0) <graphic> #t
(even? -120762398465) <graphic> #f
(even? 2.0+0.0i) <graphic> #t


procedure: (odd? int)
returns: #t if int is odd, #f otherwise

(odd? 0) <graphic> #f
(odd? 1) <graphic> #t
(odd? 2.0) <graphic> #f
(odd? -120762398465) <graphic> #t
(odd? 2.0+0.0i) <graphic> #f


procedure: (quotient int1 int2)
returns: the integer quotient of int1 and int2

(quotient 45 6) <graphic> 7
(quotient 6.0 2.0) <graphic> 3.0
(quotient 3.0 -2) <graphic> -1.0


procedure: (remainder int1 int2)
returns: the integer remainder of int1 and int2

The result of remainder has the same sign as int1.

(remainder 16 4) <graphic> 0
(remainder 5 2) <graphic> 1
(remainder -45.0 7) <graphic> -3.0
(remainder 10.0 -3.0) <graphic> 1.0
(remainder -17 -9) <graphic> -8


procedure: (modulo int1 int2)
returns: the integer modulus of int1 and int2

The result of modulo has the same sign as int2.

(modulo 16 4) <graphic> 0
(modulo 5 2) <graphic> 1
(modulo -45.0 7) <graphic> 4.0
(modulo 10.0 -3.0) <graphic> -2.0
(modulo -17 -9) <graphic> -8


procedure: (truncate real)
returns: the integer closest to real toward zero

(truncate 19) <graphic> 19
(truncate 2/3) <graphic> 0
(truncate -2/3) <graphic> 0
(truncate 17.3) <graphic> 17.0
(truncate -17/2) <graphic> -8


procedure: (floor real)
returns: the integer closest to real toward <graphic>

(floor 19) <graphic> 19
(floor 2/3) <graphic> 0
(floor -2/3) <graphic> -1
(floor 17.3) <graphic> 17.0
(floor -17/2) <graphic> -9


procedure: (ceiling real)
returns: the integer closest to real toward <graphic>

(ceiling 19) <graphic> 19
(ceiling 2/3) <graphic> 1
(ceiling -2/3) <graphic> 0
(ceiling 17.3) <graphic> 18.0
(ceiling -17/2) <graphic> -8


procedure: (round real)
returns: the integer closest to real

If real is exactly between two integers, the closest even integer is returned.

(round 19) <graphic> 19
(round 2/3) <graphic> 1
(round -2/3) <graphic> -1
(round 17.3) <graphic> 17.0
(round -17/2) <graphic> -8
(round 2.5) <graphic> 2.0
(round 3.5) <graphic> 4.0


procedure: (abs real)
returns: the absolute value of real

abs is equivalent to (lambda (x) (if (< x 0) (- x) x)). abs and magnitude (see page 150) are identical for real inputs.

(abs 1) <graphic> 1
(abs -3/4) <graphic> 3/4
(abs 1.83) <graphic> 1.83
(abs -0.093) <graphic> 0.093


procedure: (max real1 real2 ...)
returns: the maximum of real1 real2 ...

(max 4 -7 2 0 -6) <graphic> 4
(max 1/2 3/4 4/5 5/6 6/7) <graphic> 6/7
(max 1.5 1.3 -0.3 0.4 2.0 1.8) <graphic> 2.0
(max 5 2.0) <graphic> 5.0
(max -5 -2.0) <graphic> -2.0
(let ((ls '(7 3 5 2 9 8)))
  (apply max ls)) <graphic> 9


procedure: (min real1 real2 ...)
returns: the minimum of real1 real2 ...

(min 4 -7 2 0 -6) <graphic> -7
(min 1/2 3/4 4/5 5/6 6/7) <graphic> 1/2
(min 1.5 1.3 -0.3 0.4 2.0 1.8) <graphic> -0.3
(min 5 2.0) <graphic> 2.0
(min -5 -2.0) <graphic> -5.0
(let ((ls '(7 3 5 2 9 8)))
  (apply min ls)) <graphic> 2


procedure: (gcd int ...)
returns: the greatest common divisor of its arguments int ...

The result is always nonnegative, i.e., factors of -1 are ignored. When called with no arguments, gcd returns 0.

(gcd) <graphic> 0
(gcd 34) <graphic> 34
(gcd 33.0 15.0) <graphic> 3.0
(gcd 70 -42 28) <graphic> 14


procedure: (lcm int ...)
returns: the least common multiple of its arguments int ...

The result is always nonnegative, i.e., common multiples of -1 are ignored. Although lcm should probably return <graphic> when called with no arguments, it is defined to return 1. If one or more of the arguments is 0, lcm returns 0.

(lcm) <graphic> 1
(lcm 34) <graphic> 34
(lcm 33.0 15.0) <graphic> 165.0
(lcm 70 -42 28) <graphic> 420
(lcm 17.0 0) <graphic> 0


procedure: (expt num1 num2)
returns: num1 raised to the num2 power

If both arguments are 0, expt returns 1.

(expt 2 10) <graphic> 1024
(expt 2 -10) <graphic> 1/1024
(expt 2 -10.0) <graphic> 9.765625e-4
(expt -1/2 5) <graphic> -1/32
(expt 3.0 3) <graphic> 27.0
(expt +i 2) <graphic> -1


procedure: (exact->inexact num)
returns: an inexact representation for num

If num is already inexact, it is returned unchanged. If no inexact representation for num is supported by the implementation, an error may be signaled.

(exact->inexact 3) <graphic> 3.0
(exact->inexact 3.0) <graphic> 3.0
(exact->inexact -1/4) <graphic> -.25
(exact->inexact 3+4i) <graphic> 3.0+4.0i
(exact->inexact (expt 10 20)) <graphic> 1e20


procedure: (inexact->exact num)
returns: an exact representation for num

If num is already exact, it is returned unchanged. If no exact representation for num is supported by the implementation, an error may be signaled.

(inexact->exact 3.0) <graphic> 3
(inexact->exact 3) <graphic> 3
(inexact->exact -.25) <graphic> -1/4
(inexact->exact 3.0+4.0i) <graphic> 3+4i
(inexact->exact 1e20) <graphic> 100000000000000000000


procedure: (rationalize real1 real2)
returns: see below

rationalize returns the simplest rational number that differs from real1 by no more than real2. A rational number q1 = n1/m1 is simpler than another rational number q2 = n2/m2 if |n1| ≤ |n2| and |m1| ≤ |m2| and either |n1| < |n2| or |m1| < |m2|.

(rationalize 3/10 1/10) <graphic> 1/3
(rationalize .3 1/10) <graphic> 0.3333333333333333
(eqv? (rationalize .3 1/10) #i1/3) <graphic> #t


procedure: (numerator rat)
returns: the numerator of rat

If rat is an integer, the numerator is rat.

(numerator 9) <graphic> 9
(numerator 9.0) <graphic> 9.0
(numerator 2/3) <graphic> 2
(numerator -9/4) <graphic> -9
(numerator -2.25) <graphic> -9.0


procedure: (denominator rat)
returns: the denominator of rat

If rat is an integer, the denominator is 1.

(denominator 9) <graphic> 1
(denominator 9.0) <graphic> 1.0
(denominator 2/3) <graphic> 3
(denominator -9/4) <graphic> 4
(denominator -2.25) <graphic> 4.0


procedure: (real-part num)
returns: the real component of num

If num is real, real-part returns num.

(real-part 3+4i) <graphic> 3
(real-part -2.3+0.7i) <graphic> -2.3
(real-part -i) <graphic> 0
(real-part 17.2) <graphic> 17.2
(real-part -17/100) <graphic> -17/100


procedure: (imag-part num)
returns: the imaginary component of num

If num is real, imag-part returns zero.

(imag-part 3+4i) <graphic> 4
(imag-part -2.3+0.7i) <graphic> 0.7
(imag-part -i) <graphic> -1
(imag-part 17.2) <graphic> 0.0
(imag-part -17/100) <graphic> 0


procedure: (make-rectangular real1 real2)
returns: a complex number with real component real1 and imaginary component real2

(make-rectangular -2 7) <graphic> -2+7i
(make-rectangular 2/3 -1/2) <graphic> 2/3-1/2i
(make-rectangular 3.2 5.3) <graphic> 3.2+5.3i


procedure: (make-polar real1 real2)
returns: a complex number with magnitude real1 and angle real2

(make-polar 2 0) <graphic> 2
(make-polar 2.0 0.0) <graphic> 2.0+0.0i
(make-polar 1.0 (asin -1.0)) <graphic> 0.0-1.0i
(eqv? (make-polar 7.2 -0.588) 7.2@-0.588) <graphic> #t


procedure: (angle num)
returns: the angle part of the polar representation of num

The range of the result is <graphic> (exclusive) to <graphic> (inclusive).

(angle 7.3@1.5708) <graphic> 1.5708
(angle 5.2) <graphic> 0.0


procedure: (magnitude num)
returns: the magnitude of num

magnitude and abs (see page 146) are identical for real arguments. The magnitude of a complex number x + yi is <graphic>.

(magnitude 1) <graphic> 1
(magnitude -3/4) <graphic> 3/4
(magnitude 1.83) <graphic> 1.83
(magnitude -0.093) <graphic> 0.093
(magnitude 3+4i) <graphic> 5
(magnitude 7.25@1.5708) <graphic> 7.25


procedure: (sqrt num)
returns: the principal square root of num

Implementations are encouraged, but not required, to return exact results for exact inputs to sqrt whenever feasible.

(sqrt 16) <graphic> 4
(sqrt 1/4) <graphic> 1/2
(sqrt 4.84) <graphic> 2.2
(sqrt -4.84) <graphic> 0.0+2.2i
(sqrt 3+4i) <graphic> 2+1i
(sqrt -3.0-4.0i) <graphic> 1.0-2.0i


procedure: (exp num)
returns: e to the num power

(exp 0.0) <graphic> 1.0
(exp 1.0) <graphic> 2.7182818284590455
(exp -.5) <graphic> 0.6065306597126334


procedure: (log num)
returns: the natural log of num

The log of a complex number z is defined as follows.

<graphic>

(log 1.0) <graphic> 0.0
(log (exp 1.0)) <graphic> 1.0
(/ (log 100) (log 10)) <graphic> 2.0
(log (make-polar (exp 2.0) 1.0)) <graphic> 2.0+1.0i


procedure: (sin num)
procedure: (cos num)
procedure: (tan num)
returns: the sine, cosine, or tangent of num

The argument is specified in radians.


procedure: (asin num)
procedure: (acos num)
returns: the arc sine or the arc cosine of num

The result is in radians. The arc sine and arc cosine of a complex number z are defined as follows.

<graphic>

<graphic>


procedure: (atan num)
procedure: (atan real1 real2)
returns: see explanation

When passed a single complex argument num (the first form), atan returns the arc tangent of num. The arc tangent of a complex number z is defined as follows.

<graphic>

When passed two real arguments (the second form), atan is equivalent to (lambda (y x) (angle (make-rectangular x y))).


procedure: (string->number string)
procedure: (string->number string radix)
returns: the number represented by string, or #f

If string is a valid representation of a number, that number is returned, otherwise #f is returned. The number is interpreted in radix radix, which must be an exact integer in the set {2,8,10,16}. If not specified, radix defaults to 10. Any radix specifier within string, e.g., #x, overrides the radix argument.

(string->number "0") <graphic> 0
(string->number "3.4e3") <graphic> 3400.0
(string->number "#x#e-2e2") <graphic> -738
(string->number "#e-2e2" 16) <graphic> -738
(string->number "#i15/16") <graphic> 0.9375
(string->number "10" 16) <graphic> 16


procedure: (number->string num)
procedure: (number->string num radix)
returns: an external representation of num as a string

The num is expressed in radix radix, which must be an exact integer in the set {2,8,10,16}. If not specified, radix defaults to 10. In any case, no radix specifier appears in the resulting string.

The external representation is such that, when converted back into a number using string->number, the resulting numeric value is equivalent to num. That is, for all inputs:

(eqv? (string->number
        (number->string num radix)
        radix)
      num)

returns #t. Inexact results are expressed using the fewest number of significant digits possible without violating the above restriction.

(number->string 3.4) <graphic> "3.4"
(number->string 1e2) <graphic> "100.0"
(number->string 1e23) <graphic> "1e23"
(number->string -7/2) <graphic> "-7/2"
(number->string 220/9 16) <graphic> "DC/9"

Section 6.5. Characters

Characters are atomic objects representing letters, digits, special symbols such as $ or -, and certain nongraphic control characters such as space and newline. Characters are written with a #\ prefix. For most characters, the prefix is followed by the character itself. The written character representation of the letter A, for example, is #\A. The characters newline and space may be written in this manner as well, but they can also be written as #\newline or #\space.

This section describes the operations that deal primarily with characters. See also the following section on strings and Chapter 7 on input and output for other operations relating to characters.


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

These predicates behave in a similar manner to the numeric predicates =, <, >, <=, and >=. For example, char=? returns #t when its arguments are equivalent characters, and char<? returns #t when its arguments are monotonically increasing character values.

Independent of the particular representation employed, the following relationships are guaranteed to hold.

The tests performed by char=?, char<?, char>?, char<=?, and char>=? are case-sensitive. That is, the character #\A is not equivalent to the character #\a according to these predicates.

The ANSI/IEEE standard includes only two-argument versions of these procedures. The more general versions are mentioned in the Revised5 Report.

(char>? #\a #\b) <graphic> #f
(char<? #\a #\b) <graphic> #t
(char<? #\a #\b #\c) <graphic> #t
(let ((c #\r))
  (char<=? #\a c #\z)) <graphic> #t
(char<=? #\Z #\W) <graphic> #f
(char=? #\+ #\+) <graphic> #t
(or (char<? #\a #\0)
    (char<? #\0 #\a)) <graphic> #t


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

These predicates are identical to the predicates char=?, char<?, char>?, char<=?, and char>=? except that they are case-insensitive. This means that when two letters are compared, case is unimportant. For example, char=? considers #\a and #\A to be distinct values; char-ci=? does not.

The ANSI/IEEE standard includes only two-argument versions of these procedures. The more general versions are mentioned in the Revised5 Report.

(char-ci<? #\a #\B) <graphic> #t
(char-ci=? #\W #\w) <graphic> #t
(char-ci=? #\= #\+) <graphic> #f
(let ((c #\R))
  (list (char<=? #\a c #\z)
        (char-ci<=? #\a c #\z))) <graphic> (#f #t)


procedure: (char-alphabetic? char)
returns: #t if char is a letter, #f otherwise

(char-alphabetic? #\a) <graphic> #t
(char-alphabetic? #\T) <graphic> #t
(char-alphabetic? #\8) <graphic> #f
(char-alphabetic? #\$) <graphic> #f


procedure: (char-numeric? char)
returns: #t if char is a digit, #f otherwise

(char-numeric? #\7) <graphic> #t
(char-numeric? #\2) <graphic> #t
(char-numeric? #\X) <graphic> #f
(char-numeric? #\space) <graphic> #f


procedure: (char-lower-case? letter)
returns: #t if letter is lower-case, #f otherwise

If letter is not alphabetic, the result is unspecified.

(char-lower-case? #\r) <graphic> #t
(char-lower-case? #\R) <graphic> #f
(char-lower-case? #\8) <graphic> unspecified


procedure: (char-upper-case? letter)
returns: #t if letter is upper-case, #f otherwise

If letter is not alphabetic, the result is unspecified.

(char-upper-case? #\r) <graphic> #f
(char-upper-case? #\R) <graphic> #t
(char-upper-case? #\8) <graphic> unspecified


procedure: (char-whitespace? char)
returns: #t if char is whitespace, #f otherwise

Whitespace consists of spaces and newlines and possibly other nongraphic characters, depending upon the Scheme implementation and the underlying operating system.

(char-whitespace? #\space) <graphic> #t
(char-whitespace? #\newline) <graphic> #t
(char-whitespace? #\Z) <graphic> #f


procedure: (char-upcase char)
returns: the upper-case character equivalent to char

If char is a lower-case character, char-upcase returns the upper-case equivalent. If char is not a lower-case character, char-upcase returns char.

(char-upcase #\g) <graphic> #\G
(char-upcase #\Y) <graphic> #\Y
(char-upcase #\7) <graphic> #\7


procedure: (char-downcase char)
returns: the lower-case character equivalent to char

If char is an upper-case character, char-downcase returns the lower-case equivalent. If char is not an upper-case character, char-downcase returns char.

(char-downcase #\g) <graphic> #\g
(char-downcase #\Y) <graphic> #\y
(char-downcase #\7) <graphic> #\7


procedure: (char->integer char)
returns: an exact integer representation for char

char->integer is useful for performing table lookups, with the integer representation of char employed as an index into a table. The integer representation of a character is typically the integer code supported by the operating system for character input and output.

Although the particular representation employed depends on the Scheme implementation and the underlying operating system, the rules regarding the relationship between character objects stated above under the description of char=? and its relatives holds for the integer representations of characters as well.

The following examples assume that the integer representation is the ASCII code for the character.

(char->integer #\h) <graphic> 104
(char->integer #\newline) <graphic> 10

The definition of make-dispatch-table below shows how the integer codes returned by char->integer may be used portably to associate values with characters in vector-based dispatch tables, even though the exact correspondence between characters and their integer codes is unspecified.

make-dispatch-table accepts two arguments: an association list (see assv in Section 6.3) associating characters with values and a default value for characters without associations. It returns a lookup procedure that accepts a character and returns the associated (or default) value. make-dispatch-table builds a vector that is used by the lookup procedure. This vector is indexed by the integer codes for the characters and contains the associated values. Slots in the vector between indices for characters with defined values are filled with the default value. The code works even if char->integer returns negative values or both negative and nonnegative values, although the table can get large if the character codes are not tightly packed.

(define make-dispatch-table
  (lambda (alist default)
    (let ((codes (map char->integer (map car alist))))
      (let ((first-index (apply min codes))
            (last-index (apply max codes)))
        (let ((n (+ (- last-index first-index) 1)))
          (let ((v (make-vector n default)))
            (for-each
              (lambda (i x) (vector-set! v (- i first-index) x))
              codes
              (map cdr alist))
            ;; table is built; return the table lookup procedure
            (lambda (c)
              (let ((i (char->integer c)))
                (if (<= first-index i last-index)
                    (vector-ref v (- i first-index))
                    default)))))))))

(define-syntax define-dispatch-table
  ;; define-dispatch-table associates sets of characters in strings
  ;; with values in a call to make-dispatch-table.
  (syntax-rules ()
    ((_ default (str val) ...)
     (make-dispatch-table
       (append (map (lambda (c) (cons c 'val))
                    (string->list str))
               ...)
       'default))))

(define t
  (define-dispatch-table
    unknown
    ("abcdefghijklmnopqrstuvwxyz" letter)
    ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" letter)
    ("0123456789" digit)))

(t #\m) <graphic> letter
(t #\0) <graphic> digit
(t #\*) <graphic> unknown


procedure: (integer->char int)
returns: the character object corresponding to the exact integer int

This procedure is the functional inverse of char->integer. It is an error for int to be outside the range of valid integer character codes.

The following examples assume that the integer representation is the ASCII code for the character.

(integer->char 48) <graphic> #\0
(integer->char 101) <graphic> #\e

Section 6.6. Strings

Strings are sequences of characters and are typically used as messages or character buffers. Scheme provides operations for creating strings, extracting characters from strings, obtaining substrings, concatenating strings, and altering the contents of strings.

A string is written as a sequence of characters enclosed in double quotes, e.g., "hi there". A double quote may be introduced into a string by preceding it by a backward slash, e.g., "two \"quotes\" within". A backward slash may also be included by preceding it with a backward slash, e.g., "a \\slash".

Strings are indexed by exact nonnegative integers, and the index of the first element of any string is 0. The highest valid index for a given string is one less than its length.


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 ...)
returns: #t if the relation holds, #f otherwise

As with =, <, >, <=, and >=, these predicates express relationships among all of the arguments. For example, string>? determines if the lexicographic ordering of its arguments is monotonically decreasing.

The comparisons are based on the character predicates char=?, char<?, char>?, char<=?, and char>=?. Two strings are lexicographically equivalent if they are the same length and consist of the same sequence of characters according to char=?. If two strings differ only in length, the shorter string is considered to be lexicographically less than the longer string. Otherwise, the first character position at which the strings differ determines which string is lexicographically less than the other, according to char<?.

The ANSI/IEEE standard includes only two-argument versions of these procedures. The more general versions are mentioned in the Revised5 Report.

Two-argument string=? may be defined as follows.

(define string=?
  (lambda (s1 s2)
    (let ((n (string-length s1)))
      (and (= (string-length s2) n)
           (let loop ((i 0))
             (or (= i n)
                 (and (char=? (string-ref s1 i) (string-ref s2 i))
                      (loop (+ i 1)))))))))

Two-argument string<? may be defined as follows.

(define string<?
  (lambda (s1 s2)
    (let ((n1 (string-length s1)) (n2 (string-length s2)))
      (let loop ((i 0))
        (and (not (= i n2))
             (or (= i n1)
                 (let ((c1 (string-ref s1 i)) (c2 (string-ref s2 i)))
                   (or (char<? c1 c2)
                       (and (char=? c1 c2)
                            (loop (+ i 1)))))))))))

These definitions may be extended straightforwardly to support three or more arguments. string<=?, string>?, and string>=? may be defined similarly.

(string=? "mom" "mom") <graphic> #t
(string<? "mom" "mommy") <graphic> #t
(string>? "Dad" "Dad") <graphic> #f
(string=? "Mom and Dad" "mom and dad") <graphic> #f
(string<? "a" "b" "c") <graphic> #t


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

These predicates are case-insensitive versions of string=?, string<?, string>?, string<=?, and string>=?. That is, the comparisons are based on the character predicates char-ci=?, char-ci<?, char-ci>?, char-ci<=?, and char-ci>=?.

The ANSI/IEEE standard includes only two-argument versions of these procedures. The more general versions are mentioned in the Revised5 Report.

Two-argument versions of these procedures may be defined in a manner similar to string=? and string<? above.

(string-ci=? "Mom and Dad" "mom and dad") <graphic> #t
(string-ci<=? "say what" "Say What!?") <graphic> #t
(string-ci>? "N" "m" "L" "k") <graphic> #t


procedure: (string char ...)
returns: a string containing the characters char ...

(string) <graphic> ""
(string #\a #\b #\c) <graphic> "abc"
(string #\H #\E #\Y #\!) <graphic> "HEY!"


procedure: (make-string n)
procedure: (make-string n char)
returns: a string of length n

n must be an exact nonnegative integer. If char is supplied, the string is filled with char, otherwise the characters contained in the string are unspecified.

(make-string 0) <graphic> ""
(make-string 0 #\x) <graphic> ""
(make-string 5 #\x) <graphic> "xxxxx"


procedure: (string-length string)
returns: the number of characters in string

The length of a string is always an exact nonnegative integer.

(string-length "abc") <graphic> 3
(string-length "") <graphic> 0
(string-length "hi there") <graphic> 8
(string-length (make-string 1000000)) <graphic> 1000000


procedure: (string-ref string n)
returns: the nth character (zero-based) of string

n must be an exact nonnegative integer strictly less than the length of string.

(string-ref "hi there" 0) <graphic> #\h
(string-ref "hi there" 5) <graphic> #\e


procedure: (string-set! string n char)
returns: unspecified

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

(let ((str "hi three"))
  (string-set! str 5 #\e)
  (string-set! str 6 #\r)
  str) <graphic> "hi there"


procedure: (string-copy string)
returns: a new copy of string

string-copy is equivalent to (lambda (s) (string-append s)). string-copy appears in the Revised5 Report but not in the ANSI/IEEE standard.

(string-copy "abc") <graphic> "abc"
(let ((str "abc"))
  (eq? str (string-copy str))) <graphic> #f


procedure: (string-append string ...)
returns: a new string formed by concatenating the strings string ...

(string-append) <graphic> ""
(string-append "abc" "def") <graphic> "abcdef"
(string-append "Hey " "you " "there!") <graphic> "Hey you there!"

The following implementation of string-append recurs down the list of strings to compute the total length, then allocates the new string and fills it up as it unwinds the recursion.

(define string-append
  (lambda args
    (let f ((ls args) (n 0))
      (if (null? ls)
          (make-string n)
          (let* ((s1 (car ls))
                 (m (string-length s1))
                 (s2 (f (cdr ls) (+ n m))))
            (do ((i 0 (+ i 1)) (j n (+ j 1)))
                ((= i m) s2)
                (string-set! s2 j (string-ref s1 i))))))))


procedure: (substring string start end)
returns: a copy of string from start (inclusive) to end (exclusive)

start and end must be exact 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 end ≤ start, a string of length zero is returned. substring may be defined as follows.

(define substring
  (lambda (s1 m n)
    (let ((s2 (make-string (- n m))))
      (do ((j 0 (+ j 1)) (i m (+ i 1)))
          ((= i n) s2)
          (string-set! s2 j (string-ref s1 i))))))

(substring "hi there" 0 1) <graphic> "h"
(substring "hi there" 3 6) <graphic> "the"
(substring "hi there" 5 5) <graphic> ""

(let ((str "hi there"))
  (let ((end (string-length str)))
    (substring str 0 end))) <graphic> "hi there"


procedure: (string-fill! string char)
returns: unspecified

string-fill! sets every character in string to char. string-fill! appears in the Revised5 Report but not in the ANSI/IEEE standard. It may be defined as follows.

(define string-fill!
  (lambda (s c)
    (let ((n (string-length s)))
      (do ((i 0 (+ i 1)))
          ((= i n))
          (string-set! s i c)))))

(let ((str (string-copy "sleepy")))
  (string-fill! str #\Z)
  str) <graphic> "ZZZZZZ"


procedure: (string->list string)
returns: a list of the characters in string

string->list allows a string to be converted into a list, so that Scheme's list-processing operations may be applied to the processing of strings. string->list appears in the Revised5 Report but not in the ANSI/IEEE standard. It may be defined as follows.

(define string->list
  (lambda (s)
    (do ((i (- (string-length s) 1) (- i 1))
         (ls '() (cons (string-ref s i) ls)))
        ((< i 0) ls))))

(string->list "") <graphic> ()
(string->list "abc") <graphic> (#\a #\b #\c)
(apply char<? (string->list "abc")) <graphic> #t
(map char-upcase (string->list "abc")) <graphic> (#\A #\B #\C)


procedure: (list->string list)
returns: a string of the characters in list

list must consist entirely of characters.

list->string is the functional inverse of string->list. A program might use both procedures together, first converting a string into a list, then operating on this list to produce a new list, and finally converting the new list back into a string.

list->string appears in the Revised5 Report but not in the ANSI/IEEE standard. It may be defined as follows.

(define list->string
  (lambda (ls)
    (let ((s (make-string (length ls))))
      (do ((ls ls (cdr ls)) (i 0 (+ i 1)))
          ((null? ls) s)
          (string-set! s i (car ls))))))

(list->string '()) <graphic> ""
(list->string '(#\a #\b #\c)) <graphic> "abc"
(list->string
  (map char-upcase
       (string->list "abc"))) <graphic> "ABC"

Section 6.7. Vectors

Vectors are more convenient and efficient than lists for some applications. Whereas accessing an arbitrary element in a list requires a linear traversal of the list up to the selected element, arbitrary vector elements are accessed in constant time. The length of a vector in Scheme is the number of elements it contains. Vectors are indexed by exact nonnegative integers, and the index of the first element of any vector is 0. The highest valid index for a given vector is one less than its length.

As with lists, the elements of a vector may be of any type; a single vector may even hold more than one type of object.

A vector is written as a sequence of objects separated by whitespace, preceded by the prefix #( and followed by ). For example, a vector consisting of the elements a, b, and c would be written #(a b c).


procedure: (vector obj ...)
returns: a vector of the objects obj ...

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


procedure: (make-vector n)
procedure: (make-vector n obj)
returns: a vector of length n

n must be an exact nonnegative integer. If obj is supplied, each element of the vector is filled with obj; otherwise, the elements are unspecified.

(make-vector 0) <graphic> #()
(make-vector 0 'a) <graphic> #()
(make-vector 5 'a) <graphic> #(a a a a a)


procedure: (vector-length vector)
returns: the number of elements in vector

The length of a vector is always an exact nonnegative integer.

(vector-length '#()) <graphic> 0
(vector-length '#(a b c)) <graphic> 3
(vector-length (vector 1 2 3 4)) <graphic> 4
(vector-length (make-vector 300)) <graphic> 300


procedure: (vector-ref vector n)
returns: the nth element (zero-based) of vector

n must be an exact nonnegative integer strictly less than the length of vector.

(vector-ref '#(a b c) 0) <graphic> a
(vector-ref '#(a b c) 1) <graphic> b
(vector-ref '#(x y z w) 3) <graphic> w


procedure: (vector-set! vector n obj)
returns: unspecified

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

(let ((v (vector 'a 'b 'c 'd 'e)))
  (vector-set! v 2 'x)
  v) <graphic> #(a b x d e)


procedure: (vector-fill! vector obj)
returns: unspecified

vector-fill! replaces each element of vector with obj. vector-fill! appears in the Revised5 Report but not in the ANSI/IEEE standard. It may be defined as follows.

(define vector-fill!
  (lambda (v x)
    (let ((n (vector-length v)))
      (do ((i 0 (+ i 1)))
          ((= i n))
          (vector-set! v i x)))))

(let ((v (vector 1 2 3)))
  (vector-fill! v 0)
  v) <graphic> #(0 0 0)


procedure: (vector->list vector)
returns: a list of the elements of vector

vector->list provides a convenient method for applying list-processing operations to vectors. vector->list appears in the Revised5 Report but not in the ANSI/IEEE standard. It may be defined as follows.

(define vector->list
  (lambda (s)
    (do ((i (- (vector-length s) 1) (- i 1))
         (ls '() (cons (vector-ref s i) ls)))
        ((< i 0) ls))))

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

(let ((v '#(1 2 3 4 5)))
  (apply * (vector->list v))) <graphic> 120


procedure: (list->vector list)
returns: a vector of the elements of list

list->vector is the functional inverse of vector->list. The two procedures are often used in combination to take advantage of a list-processing operation. A vector may be converted to a list with vector->list, this list processed in some manner to produce a new list, and the new list converted back into a vector with list->vector.

list->vector appears in the Revised5 Report but not in the ANSI/IEEE standard. It may be defined as follows.

(define list->vector
  (lambda (ls)
    (let ((s (make-vector (length ls))))
      (do ((ls ls (cdr ls)) (i 0 (+ i 1)))
          ((null? ls) s)
          (vector-set! s i (car ls))))))

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

(let ((v '#(1 2 3 4 5)))
  (let ((ls (vector->list v)))
    (list->vector (map * ls ls)))) <graphic> #(1 4 9 16 25)

Section 6.8. Symbols

Symbols are used for a variety of purposes as symbolic names in Scheme programs. Strings could be used for most of the same purposes, but an important characteristic of symbols makes comparisons between symbols much more efficient. This characteristic is that two symbols with the same name are identical in the sense of eq?. The reason is that the Scheme reader (see read in Section 7.1) and the procedure string->symbol catalog symbols in an internal symbol table and always return the same symbol whenever the same name is encountered. Thus, no character-by-character comparison is needed, as would be needed to compare two strings.

The property that two symbols may be compared quickly for equivalence makes them ideally suited for use as identifiers in the representation of programs, allowing fast comparison of identifiers. This property also makes symbols useful for a variety of other purposes. For example, symbols might be used as messages passed between procedures, labels for list-structured records, or names for objects stored in an association list (see assq in Section 6.3).

Symbols are written without double quotes or other bracketing characters. Parentheses, double quotes, spaces, and most other characters with a special meaning to the Scheme reader are not allowed within the printed representation of a symbol. Some implementations, however, support the use of backward slashes to escape special characters occurring in symbols, in a manner similar to the use of backward slashes in strings.

Refer to Section 1.1 or the formal syntax of Scheme at the back of this book for a precise description of the syntax of symbols.


procedure: (string->symbol string)
returns: a symbol whose name is string

string->symbol records all symbols it creates in an internal table that it shares with the system reader, read. If a symbol whose name is equivalent to string (according to the predicate string=?) already exists in the table, this symbol is returned. Otherwise, a new symbol is created with string as its name; this symbol is entered into the table and returned.

The system reader arranges to convert all symbols to a single case (lower case is assumed in this book), before entering them into the internal table. string->symbol does not. Thus, it is possible to produce symbols in lower case, upper case, or even mixed case, using string->symbol. It is also possible to create symbols with names that contain special characters, such as spaces or parentheses.

(string->symbol "x") <graphic> x

(eq? (string->symbol "x") 'x) <graphic> #t
(eq? (string->symbol "X") 'x) <graphic> #f

(eq? (string->symbol "x")
     (string->symbol "x")) <graphic> #t


procedure: (symbol->string symbol)
returns: a string, the name of symbol

The string returned by symbol->string for a symbol created by an earlier call to string->symbol may or may not be the same string (by eq?) as the string passed to string->symbol. That is, an implementation is free to copy or not to copy a string it uses as the name of a symbol. Unpredictable behavior can result if a string passed to string->symbol is altered with string-set! or by any other means.

(symbol->string 'xyz) <graphic> "xyz"
(symbol->string (string->symbol "Hi")) <graphic> "Hi"
(symbol->string (string->symbol "()")) <graphic> "()"

R. Kent Dybvig / The Scheme Programming Language, Third Edition
Copyright © 2003 The MIT Press. Electronically reproduced by permission.
Illustrations © 2003 Jean-Pierre Hébert
ISBN 0-262-54148-3 / LOC QA76.73.S34D93
to order this book / about this book

http://www.scheme.com