This is r5rs.info, produced by makeinfo version 4.2 from r5rs.texi.

INFO-DIR-SECTION The Algorithmic Language Scheme
START-INFO-DIR-ENTRY
* R5RS: (r5rs).                 The Revised(5) Report on Scheme.
END-INFO-DIR-ENTRY

20 February 1998


File: r5rs.info,  Node: Implementation restrictions,  Next: Syntax of numerical constants,  Prev: Exactness,  Up: Numbers

Implementation restrictions
---------------------------

Implementations of Scheme are not required to implement the whole tower
of subtypes given in section *Note Numerical types::, but they must
implement a coherent subset consistent with both the purposes of the
implementation and the spirit of the Scheme language.  For example, an
implementation in which all numbers are real may still be quite useful.

Implementations may also support only a limited range of numbers of any
type, subject to the requirements of this section.  The supported range
for exact numbers of any type may be different from the supported range
for inexact numbers of that type.  For example, an implementation that
uses flonums to represent all its inexact real numbers may support a
practically unbounded range of exact integers and rationals while
limiting the range of inexact reals (and therefore the range of inexact
integers and rationals) to the dynamic range of the flonum format.
Furthermore the gaps between the representable inexact integers and
rationals are likely to be very large in such an implementation as the
limits of this range are approached.

An implementation of Scheme must support exact integers throughout the
range of numbers that may be used for indexes of lists, vectors, and
strings or that may result from computing the length of a list, vector,
or string.  The `length', `vector-length', and `string-length'
procedures must return an exact integer, and it is an error to use
anything but an exact integer as an index.  Furthermore any integer
constant within the index range, if expressed by an exact integer
syntax, will indeed be read as an exact integer, regardless of any
implementation restrictions that may apply outside this range.
Finally, the procedures listed below will always return an exact
integer result provided all their arguments are exact integers and the
mathematically expected result is representable as an exact integer
within the implementation:


     +            -             *
     quotient     remainder     modulo
     max          min           abs
     numerator    denominator   gcd
     lcm          floor         ceiling
     truncate     round         rationalize
     expt

Implementations are encouraged, but not required, to support exact
integers and exact rationals of practically unlimited size and
precision, and to implement the above procedures and the `/' procedure
in such a way that they always return exact results when given exact
arguments.  If one of these procedures is unable to deliver an exact
result when given exact arguments, then it may either report a
violation of an implementation restriction or it may silently coerce
its result to an inexact number.  Such a coercion may cause an error
later.

An implementation may use floating point and other approximate
representation strategies for inexact numbers.

This report recommends, but does not require, that the IEEE 32-bit and
64-bit floating point standards be followed by implementations that use
flonum representations, and that implementations using other
representations should match or exceed the precision achievable using
these floating point standards [IEEE].

In particular, implementations that use flonum representations must
follow these rules: A flonum result must be represented with at least
as much precision as is used to express any of the inexact arguments to
that operation.  It is desirable (but not required) for potentially
inexact operations such as `sqrt', when applied to exact arguments, to
produce exact answers whenever possible (for example the square root of
an exact 4 ought to be an exact 2).  If, however, an exact number is
operated upon so as to produce an inexact result (as by `sqrt'), and if
the result is represented as a flonum, then the most precise flonum
format available must be used; but if the result is represented in some
other way then the representation must have at least as much precision
as the most precise flonum format available.

Although Scheme allows a variety of written notations for numbers, any
particular implementation may support only some of them.  For example,
an implementation in which all numbers are real need not support the
rectangular and polar notations for complex numbers.  If an
implementation encounters an exact numerical constant that it cannot
represent as an exact number, then it may either report a violation of
an implementation restriction or it may silently represent the constant
by an inexact number.


File: r5rs.info,  Node: Syntax of numerical constants,  Next: Numerical operations,  Prev: Implementation restrictions,  Up: Numbers

Syntax of numerical constants
-----------------------------

The syntax of the written representations for numbers is described
formally in section *Note Lexical structure::.  Note that case is not
significant in numerical constants.

A number may be written in binary, octal, decimal, or hexadecimal by
the use of a radix prefix.  The radix prefixes are `#b' (binary), `#o'
(octal), `#d' (decimal), and `#x' (hexadecimal).  With no radix prefix,
a number is assumed to be expressed in decimal.

A numerical constant may be specified to be either exact or inexact by
a prefix.  The prefixes are `#e' for exact, and `#i' for inexact.  An
exactness prefix may appear before or after any radix prefix that is
used.  If the written representation of a number has no exactness
prefix, the constant may be either inexact or exact.  It is inexact if
it contains a decimal point, an exponent, or a "#" character in the
place of a digit, otherwise it is exact.

In systems with inexact numbers of varying precisions it may be useful
to specify the precision of a constant.  For this purpose, numerical
constants may be written with an exponent marker that indicates the
desired precision of the inexact representation.  The letters `s', `f',
`d', and `l' specify the use of SHORT, SINGLE, DOUBLE, and LONG
precision, respectively.  (When fewer than four internal inexact
representations exist, the four size specifications are mapped onto
those available.  For example, an implementation with two internal
representations may map short and single together and long and double
together.)  In addition, the exponent marker `e' specifies the default
precision for the implementation.  The default precision has at least
as much precision as DOUBLE, but implementations may wish to allow this
default to be set by the user.


     3.14159265358979F0
            Round to single -- 3.141593
     0.6L0
            Extend to long -- .600000000000000


File: r5rs.info,  Node: Numerical operations,  Next: Numerical input and output,  Prev: Syntax of numerical constants,  Up: Numbers

Numerical operations
--------------------

The reader is referred to section *Note Entry format:: for a summary of
the naming conventions used to specify restrictions on the types of
arguments to numerical routines.

The examples used in this section assume that any numerical constant
written using an exact notation is indeed represented as an exact
number.  Some examples also assume that certain numerical constants
written using an inexact notation can be represented without loss of
accuracy; the inexact constants were chosen so that this is likely to
be true in implementations that use flonums to represent inexact
numbers.

 - procedure: number? obj
 - procedure: complex? obj
 - procedure: real? obj
 - procedure: rational? obj
 - procedure: integer? obj
     These numerical type predicates can be applied to any kind of
     argument, including non-numbers.  They return #t if the object is
     of the named type, and otherwise they return #f.  In general, if a
     type predicate is true of a number then all higher type predicates
     are also true of that number.  Consequently, if a type predicate
     is false of a number, then all lower type predicates are also
     false of that number.

     If Z is an inexact complex number, then `(real? Z)' is true if and
     only if `(zero? (imag-part Z))' is true.  If X is an inexact real
     number, then `(integer? X)' is true if and only if `(= X (round
     X))'.

     (complex? 3+4i)                        ==>  #t
     (complex? 3)                           ==>  #t
     (real? 3)                              ==>  #t
     (real? -2.5+0.0i)                      ==>  #t
     (real? #e1e10)                         ==>  #t
     (rational? 6/10)                       ==>  #t
     (rational? 6/3)                        ==>  #t
     (integer? 3+0i)                        ==>  #t
     (integer? 3.0)                         ==>  #t
     (integer? 8/4)                         ==>  #t

          _Note:_ The behavior of these type predicates on inexact
          numbers is unreliable, since any inaccuracy may affect the
          result.

          _Note:_ In many implementations the `rational?' procedure
          will be the same as `real?', and the `complex?' procedure
          will be the same as `number?', but unusual implementations
          may be able to represent some irrational numbers exactly or
          may extend the number system to support some kind of
          non-complex numbers.


 - procedure: exact? Z
 - procedure: inexact? Z
     These numerical predicates provide tests for the exactness of a
     quantity.  For any Scheme number, precisely one of these predicates
     is true.


 - procedure: = z1 z2 z3 ...,
 - procedure: < x1 x2 x3 ...,
 - procedure: > x1 x2 x3 ...,
 - procedure: <= x1 x2 x3 ...,
 - procedure: >= x1 x2 x3 ...,
     These procedures return #t if their arguments are (respectively):
     equal, monotonically increasing, monotonically decreasing,
     monotonically nondecreasing, or monotonically nonincreasing.

     These predicates are required to be transitive.

          _Note:_ The traditional implementations of these predicates
          in Lisp-like languages are not transitive.

          _Note:_ While it is not an error to compare inexact numbers
          using these predicates, the results may be unreliable because
          a small inaccuracy may affect the result; this is especially
          true of `=' and `zero?'.  When in doubt, consult a numerical
          analyst.


 - library procedure: zero? Z
 - library procedure: positive? X
 - library procedure: negative? X
 - library procedure: odd? N
 - library procedure: even? N
     These numerical predicates test a number for a particular property,
     returning #t or #f.  See note above.


 - library procedure: max x1 x2 ...,
 - library procedure: min x1 x2 ...,
     These procedures return the maximum or minimum of their arguments.

     (max 3 4)                              ==>  4    ; exact
     (max 3.9 4)                            ==>  4.0  ; inexact

          _Note:_ If any argument is inexact, then the result will also
          be inexact (unless the procedure can prove that the
          inaccuracy is not large enough to affect the result, which is
          possible only in unusual implementations).  If `min' or `max'
          is used to compare numbers of mixed exactness, and the
          numerical value of the result cannot be represented as an
          inexact number without loss of accuracy, then the procedure
          may report a violation of an implementation restriction.


 - procedure: + z1 ...,
 - procedure: * z1 ...,
     These procedures return the sum or product of their arguments.

     (+ 3 4)                                ==>  7
     (+ 3)                                  ==>  3
     (+)                                    ==>  0
     (* 4)                                  ==>  4
     (*)                                    ==>  1


 - procedure: - z1 z2
 - procedure: - Z
 - optional procedure: - z1 z2 ...,
 - procedure: / z1 z2
 - procedure: / Z
 - optional procedure: / z1 z2 ...,
     With two or more arguments, these procedures return the difference
     or quotient of their arguments, associating to the left.  With one
     argument, however, they return the additive or multiplicative
     inverse of their argument.

     (- 3 4)                                ==>  -1
     (- 3 4 5)                              ==>  -6
     (- 3)                                  ==>  -3
     (/ 3 4 5)                              ==>  3/20
     (/ 3)                                  ==>  1/3


 - library procedure: abs x
     `Abs' returns the absolute value of its argument.

     (abs -7)                               ==>  7


 - procedure: quotient n1 n2
 - procedure: remainder n1 n2
 - procedure: modulo n1 n2
     These procedures implement number-theoretic (integer) division.
     N2 should be non-zero.  All three procedures return integers.  If
     N1/N2 is an integer:

         (quotient N1 N2)                   ==> N1/N2
         (remainder N1 N2)                  ==> 0
         (modulo N1 N2)                     ==> 0

     If N1/N2 is not an integer:

         (quotient N1 N2)                   ==> N_Q
         (remainder N1 N2)                  ==> N_R
         (modulo N1 N2)                     ==> N_M

     where N_Q is N1/N2 rounded towards zero, 0 < |N_R| < |N2|, 0 <
     |N_M| < |N2|, N_R and N_M differ from N1 by a multiple of N2, N_R
     has the same sign as N1, and N_M has the same sign as N2.

     From this we can conclude that for integers N1 and N2 with N2 not
     equal to 0,

          (= N1 (+ (* N2 (quotient N1 N2))
                (remainder N1 N2)))
                                            ==>  #t

     provided all numbers involved in that computation are exact.

     (modulo 13 4)                          ==>  1
     (remainder 13 4)                       ==>  1
     
     (modulo -13 4)                         ==>  3
     (remainder -13 4)                      ==>  -1
     
     (modulo 13 -4)                         ==>  -3
     (remainder 13 -4)                      ==>  1
     
     (modulo -13 -4)                        ==>  -1
     (remainder -13 -4)                     ==>  -1
     
     (remainder -13 -4.0)                   ==>  -1.0  ; inexact


 - library procedure: gcd n1 ...,
 - library procedure: lcm n1 ...,
     These procedures return the greatest common divisor or least common
     multiple of their arguments.  The result is always non-negative.

     (gcd 32 -36)                           ==>  4
     (gcd)                                  ==>  0
     (lcm 32 -36)                           ==>  288
     (lcm 32.0 -36)                         ==>  288.0  ; inexact
     (lcm)                                  ==>  1


 - procedure: numerator Q
 - procedure: denominator Q
     These procedures return the numerator or denominator of their
     argument; the result is computed as if the argument was
     represented as a fraction in lowest terms.  The denominator is
     always positive.  The denominator of 0 is defined to be 1.

     (numerator (/ 6 4))                    ==>  3
     (denominator (/ 6 4))                  ==>  2
     (denominator
       (exact->inexact (/ 6 4)))            ==> 2.0


 - procedure: floor x
 - procedure: ceiling x
 - procedure: truncate x
 - procedure: round x
     These procedures return integers.  `Floor' returns the largest
     integer not larger than X.  `Ceiling' returns the smallest integer
     not smaller than X.  `Truncate' returns the integer closest to X
     whose absolute value is not larger than the absolute value of X.
     `Round' returns the closest integer to X, rounding to even when X
     is halfway between two integers.

          _Rationale:_ `Round' rounds to even for consistency with the
          default rounding mode specified by the IEEE floating point
          standard.

          _Note:_ If the argument to one of these procedures is
          inexact, then the result will also be inexact.  If an exact
          value is needed, the result should be passed to the
          `inexact->exact' procedure.

     (floor -4.3)                           ==>  -5.0
     (ceiling -4.3)                         ==>  -4.0
     (truncate -4.3)                        ==>  -4.0
     (round -4.3)                           ==>  -4.0
     
     (floor 3.5)                            ==>  3.0
     (ceiling 3.5)                          ==>  4.0
     (truncate 3.5)                         ==>  3.0
     (round 3.5)                            ==>  4.0  ; inexact
     
     (round 7/2)                            ==>  4    ; exact
     (round 7)                              ==>  7


 - library procedure: rationalize x y
     `Rationalize' returns the _simplest_ rational number differing
     from X by no more than Y.  A rational number r_1 is _simpler_
     than another rational number r_2 if r_1 = p_1/q_1 and r_2 =
     p_2/q_2 (in lowest terms) and |p_1|<= |p_2| and |q_1| <= |q_2|.
     Thus 3/5 is simpler than 4/7.  Although not all rationals are
     comparable in this ordering (consider 2/7 and 3/5) any interval
     contains a rational number that is simpler than every other
     rational number in that interval (the simpler 2/5 lies between 2/7
     and 3/5).  Note that 0 = 0/1 is the simplest rational of all.

     (rationalize
       (inexact->exact .3) 1/10)            ==> 1/3    ; exact
     (rationalize .3 1/10)                  ==> #i1/3  ; inexact


 - procedure: exp Z
 - procedure: log Z
 - procedure: sin Z
 - procedure: cos Z
 - procedure: tan Z
 - procedure: asin Z
 - procedure: acos Z
 - procedure: atan Z
 - procedure: atan Y X
     These procedures are part of every implementation that supports
     general real numbers; they compute the usual transcendental
     functions.  `Log' computes the natural logarithm of Z (not the
     base ten logarithm).  `Asin', `acos', and `atan' compute arcsine
     (sin^-1), arccosine (cos^-1), and arctangent (tan^-1),
     respectively.  The two-argument variant of `atan' computes (angle
     (make-rectangular X Y)) (see below), even in implementations that
     don't support general complex numbers.

     In general, the mathematical functions log, arcsine, arccosine, and
     arctangent are multiply defined.  The value of log z is defined to
     be the one whose imaginary part lies in the range from -pi
     (exclusive) to pi (inclusive).  log 0 is undefined.  With log
     defined this way, the values of sin^-1 z, cos^-1 z, and tan^-1 z
     are according to the following formulae:

                   sin^-1 z = -i log (i z + sqrt1 - z^2)

                        cos^-1 z = pi / 2 - sin^-1 z

             tan^-1 z = (log (1 + i z) - log (1 - i z)) / (2 i)

     The above specification follows [CLtL], which in turn cites
     [Penfield81]; refer to these sources for more detailed discussion
     of branch cuts, boundary conditions, and implementation of these
     functions.  When it is possible these procedures produce a real
     result from a real argument.


 - procedure: sqrt Z
     Returns the principal square root of Z.  The result will have
     either positive real part, or zero real part and non-negative
     imaginary part.

 - procedure: expt z1 z2
     Returns Z1 raised to the power Z2.  For z_1 ~= 0

                          z_1^z_2 = e^z_2 log z_1
 0^z is 1 if z = 0 and 0 otherwise.

 - procedure: make-rectangular x1 x2
 - procedure: make-polar x3 x4
 - procedure: real-part Z
 - procedure: imag-part Z
 - procedure: magnitude Z
 - procedure: angle Z
     These procedures are part of every implementation that supports
     general complex numbers.  Suppose X1, X2, X3, and X4 are real
     numbers and Z is a complex number such that

                         Z = X1 + X2i = X3 . e^i X4
 Then

     (make-rectangular X1 X2)               ==> Z
     (make-polar X3 X4)                     ==> Z
     (real-part Z)                          ==> X1
     (imag-part Z)                          ==> X2
     (magnitude Z)                          ==> |X3|
     (angle Z)                              ==> x_angle

     where -pi < x_angle <= pi with x_angle = X4 + 2pi n for some
     integer n.

          _Rationale:_ `Magnitude' is the same as `abs' for a real
          argument, but `abs' must be present in all implementations,
          whereas `magnitude' need only be present in implementations
          that support general complex numbers.


 - procedure: exact->inexact Z
 - procedure: inexact->exact Z
     `Exact->inexact' returns an inexact representation of Z.  The
     value returned is the inexact number that is numerically closest
     to the argument.  If an exact argument has no reasonably close
     inexact equivalent, then a violation of an implementation
     restriction may be reported.

     `Inexact->exact' returns an exact representation of Z.  The value
     returned is the exact number that is numerically closest to the
     argument.  If an inexact argument has no reasonably close exact
     equivalent, then a violation of an implementation restriction may
     be reported.

     These procedures implement the natural one-to-one correspondence
     between exact and inexact integers throughout an
     implementation-dependent range.  See section *Note Implementation
     restrictions::.






File: r5rs.info,  Node: Numerical input and output,  Prev: Numerical operations,  Up: Numbers

Numerical input and output
--------------------------

 - procedure: number->string z
 - procedure: number->string z radix
     RADIX must be an exact integer, either 2, 8, 10, or 16.  If
     omitted, RADIX defaults to 10.  The procedure `number->string'
     takes a number and a radix and returns as a string an external
     representation of the given number in the given radix such that

     (let ((number NUMBER)
           (radix RADIX))
       (eqv? number
             (string->number (number->string number
                                             radix)
                             radix)))

     is true.  It is an error if no possible result makes this
     expression true.

     If Z is inexact, the radix is 10, and the above expression can be
     satisfied by a result that contains a decimal point, then the
     result contains a decimal point and is expressed using the minimum
     number of digits (exclusive of exponent and trailing zeroes)
     needed to make the above expression true [howtoprint], [howtoread];
     otherwise the format of the result is unspecified.

     The result returned by `number->string' never contains an explicit
     radix prefix.

          _Note:_ The error case can occur only when Z is not a complex
          number or is a complex number with a non-rational real or
          imaginary part.

          _Rationale:_ If Z is an inexact number represented using
          flonums, and the radix is 10, then the above expression is
          normally satisfied by a result containing a decimal point.
          The unspecified case allows for infinities, NaNs, and
          non-flonum representations.


 - procedure: string->number string
 - procedure: string->number string radix
     Returns a number of the maximally precise representation expressed
     by the given STRING.  RADIX must be an exact integer, either 2, 8,
     10, or 16.  If supplied, RADIX is a default radix that may be
     overridden by an explicit radix prefix in STRING (e.g. "#o177").
     If RADIX is not supplied, then the default radix is 10.  If STRING
     is not a syntactically valid notation for a number, then
     `string->number' returns #f.

     (string->number "100")                 ==>  100
     (string->number "100" 16)              ==>  256
     (string->number "1e2")                 ==>  100.0
     (string->number "15##")                ==>  1500.0

          _Note:_ The domain of `string->number' may be restricted by
          implementations in the following ways.  `String->number' is
          permitted to return #f whenever STRING contains an explicit
          radix prefix.  If all numbers supported by an implementation
          are real, then `string->number' is permitted to return #f
          whenever STRING uses the polar or rectangular notations for
          complex numbers.  If all numbers are integers, then
          `string->number' may return #f whenever the fractional
          notation is used.  If all numbers are exact, then
          `string->number' may return #f whenever an exponent marker or
          explicit exactness prefix is used, or if a # appears in place
          of a digit.  If all inexact numbers are integers, then
          `string->number' may return #f whenever a decimal point is
          used.



File: r5rs.info,  Node: Other data types,  Next: Control features,  Prev: Numbers,  Up: Standard procedures

Other data types
================

* Menu:

* Booleans::
* Pairs and lists::
* Symbols::
* Characters::
* Strings::
* Vectors::

This section describes operations on some of Scheme's non-numeric data
types: booleans, pairs, lists, symbols, characters, strings and vectors.


File: r5rs.info,  Node: Booleans,  Next: Pairs and lists,  Prev: Other data types,  Up: Other data types

Booleans
--------

The standard boolean objects for true and false are written as #t and
#f.  What really matters, though, are the objects that the Scheme
conditional expressions (`if', `cond', `and', `or', `do') treat as true
or false.  The phrase "a true value" (or sometimes just "true") means
any object treated as true by the conditional expressions, and the
phrase "a false value" (or "false") means any object treated as false
by the conditional expressions.

Of all the standard Scheme values, only #f counts as false in
conditional expressions.  Except for #f, all standard Scheme values,
including #t, pairs, the empty list, symbols, numbers, strings,
vectors, and procedures, count as true.

     _Note:_ Programmers accustomed to other dialects of Lisp should be
     aware that Scheme distinguishes both #f and the empty list from
     the symbol `nil'.

Boolean constants evaluate to themselves, so they do not need to be
quoted in programs.


     #t                                     ==>  #t
     #f                                     ==>  #f
     '#f                                    ==>  #f

 - library procedure: not obj
     `Not' returns #t if OBJ is false, and returns #f otherwise.

     (not #t)                               ==>  #f
     (not 3)                                ==>  #f
     (not (list 3))                         ==>  #f
     (not #f)                               ==>  #t
     (not '())                              ==>  #f
     (not (list))                           ==>  #f
     (not 'nil)                             ==>  #f


 - library procedure: boolean? obj
     `Boolean?' returns #t if OBJ is either #t or #f and returns #f
     otherwise.

     (boolean? #f)                          ==>  #t
     (boolean? 0)                           ==>  #f
     (boolean? '())                         ==>  #f



File: r5rs.info,  Node: Pairs and lists,  Next: Symbols,  Prev: Booleans,  Up: Other data types

Pairs and lists
---------------

A "pair" (sometimes called a "dotted pair") is a record structure with
two fields called the car and cdr fields (for historical reasons).
Pairs are created by the procedure `cons'.  The car and cdr fields are
accessed by the procedures `car' and `cdr'.  The car and cdr fields are
assigned by the procedures `set-car!' and `set-cdr!'.

Pairs are used primarily to represent lists.  A list can be defined
recursively as either the empty list or a pair whose cdr is a list.
More precisely, the set of lists is defined as the smallest set X such
that

   * The empty list is in X.

   * If LIST is in X, then any pair whose cdr field contains LIST is
     also in X.


The objects in the car fields of successive pairs of a list are the
elements of the list.  For example, a two-element list is a pair whose
car is the first element and whose cdr is a pair whose car is the
second element and whose cdr is the empty list.  The length of a list
is the number of elements, which is the same as the number of pairs.

The empty list is a special object of its own type (it is not a pair);
it has no elements and its length is zero.

     _Note:_ The above definitions imply that all lists have finite
     length and are terminated by the empty list.

The most general notation (external representation) for Scheme pairs is
the "dotted" notation `(C1 . C2)' where C1 is the value of the car
field and C2 is the value of the cdr field.  For example `(4 . 5)' is a
pair whose car is 4 and whose cdr is 5.  Note that `(4 . 5)' is the
external representation of a pair, not an expression that evaluates to
a pair.

A more streamlined notation can be used for lists: the elements of the
list are simply enclosed in parentheses and separated by spaces.  The
empty list is written () .  For example,


     (a b c d e)

and


     (a . (b . (c . (d . (e . ())))))

are equivalent notations for a list of symbols.

A chain of pairs not ending in the empty list is called an "improper
list".  Note that an improper list is not a list.  The list and dotted
notations can be combined to represent improper lists:


     (a b c . d)

is equivalent to


     (a . (b . (c . d)))

Whether a given pair is a list depends upon what is stored in the cdr
field.  When the `set-cdr!' procedure is used, an object can be a list
one moment and not the next:


     (define x (list 'a 'b 'c))
     (define y x)
     y                                      ==>  (a b c)
     (list? y)                              ==>  #t
     (set-cdr! x 4)                         ==>  _unspecified_
     x                                      ==>  (a . 4)
     (eqv? x y)                             ==>  #t
     y                                      ==>  (a . 4)
     (list? y)                              ==>  #f
     (set-cdr! x x)                         ==>  _unspecified_
     (list? x)                              ==>  #f

Within literal expressions and representations of objects read by the
`read' procedure, the forms '<datum>, `<datum>, ,<datum>, and ,@<datum>
denote two-element lists whose first elements are the symbols `quote',
`quasiquote', `unquote', and `unquote-splicing', respectively.  The
second element in each case is <datum>.  This convention is supported
so that arbitrary Scheme programs may be represented as lists.   That
is, according to Scheme's grammar, every <expression> is also a <datum>
(see section *note External representation::).  Among other things,
this permits the use of the `read' procedure to parse Scheme programs.
See section *Note External representations::.

 - procedure: pair? obj
     `Pair?' returns #t if OBJ is a pair, and otherwise returns #f.

     (pair? '(a . b))                       ==>  #t
     (pair? '(a b c))                       ==>  #t
     (pair? '())                            ==>  #f
     (pair? '#(a b))                        ==>  #f


 - procedure: cons obj1 obj2
     Returns a newly allocated pair whose car is OBJ1 and whose cdr is
     OBJ2.  The pair is guaranteed to be different (in the sense of
     `eqv?') from every existing object.

     (cons 'a '())                          ==>  (a)
     (cons '(a) '(b c d))                   ==>  ((a) b c d)
     (cons "a" '(b c))                      ==>  ("a" b c)
     (cons 'a 3)                            ==>  (a . 3)
     (cons '(a b) 'c)                       ==>  ((a b) . c)


 - procedure: car pair
     Returns the contents of the car field of PAIR.  Note that it is an
     error to take the car of the empty list.

     (car '(a b c))                         ==>  a
     (car '((a) b c d))                     ==>  (a)
     (car '(1 . 2))                         ==>  1
     (car '())                              ==>  _error_


 - procedure: cdr pair
     Returns the contents of the cdr field of PAIR.  Note that it is an
     error to take the cdr of the empty list.

     (cdr '((a) b c d))                     ==>  (b c d)
     (cdr '(1 . 2))                         ==>  2
     (cdr '())                              ==>  _error_


 - procedure: set-car! pair obj
     Stores OBJ in the car field of PAIR.  The value returned by
     `set-car!' is unspecified.

     (define (f) (list 'not-a-constant-list))
     (define (g) '(constant-list))
     (set-car! (f) 3)                       ==>  _unspecified_
     (set-car! (g) 3)                       ==>  _error_


 - procedure: set-cdr! pair obj
     Stores OBJ in the cdr field of PAIR.  The value returned by
     `set-cdr!' is unspecified.


 - library procedure: caar pair
 - library procedure: cadr pair
 -  :          ...
 - library procedure: cdddar pair
 - library procedure: cddddr pair
     These procedures are compositions of `car' and `cdr', where for
     example `caddr' could be defined by

     (define caddr (lambda (x) (car (cdr (cdr x))))).

     Arbitrary compositions, up to four deep, are provided.  There are
     twenty-eight of these procedures in all.


 - library procedure: null? obj
     Returns #t if OBJ is the empty list, otherwise returns #f.


 - library procedure: list? obj
     Returns #t if OBJ is a list, otherwise returns #f.  By definition,
     all lists have finite length and are terminated by the empty list.

             (list? '(a b c))               ==>  #t
             (list? '())                    ==>  #t
             (list? '(a . b))               ==>  #f
             (let ((x (list 'a)))
               (set-cdr! x x)
               (list? x))                   ==>  #f


 - library procedure: list OBJ ...,
     Returns a newly allocated list of its arguments.

     (list 'a (+ 3 4) 'c)                   ==>  (a 7 c)
     (list)                                 ==>  ()


 - library procedure: length list
     Returns the length of LIST.

     (length '(a b c))                      ==>  3
     (length '(a (b) (c d e)))              ==>  3
     (length '())                           ==>  0


 - library procedure: append list ...,
     Returns a list consisting of the elements of the first LIST
     followed by the elements of the other LISTs.

     (append '(x) '(y))                     ==>  (x y)
     (append '(a) '(b c d))                 ==>  (a b c d)
     (append '(a (b)) '((c)))               ==>  (a (b) (c))

     The resulting list is always newly allocated, except that it shares
     structure with the last LIST argument.  The last argument may
     actually be any object; an improper list results if the last
     argument is not a proper list.

     (append '(a b) '(c . d))               ==>  (a b c . d)
     (append '() 'a)                        ==>  a


 - library procedure: reverse list
     Returns a newly allocated list consisting of the elements of LIST
     in reverse order.

     (reverse '(a b c))                     ==>  (c b a)
     (reverse '(a (b c) d (e (f))))
               ==>  ((e (f)) d (b c) a)


 - library procedure: list-tail list K
     Returns the sublist of LIST obtained by omitting the first K
     elements.  It is an error if LIST has fewer than K elements.
     `List-tail' could be defined by

     (define list-tail
       (lambda (x k)
         (if (zero? k)
             x
             (list-tail (cdr x) (- k 1)))))


 - library procedure: list-ref list K
     Returns the Kth element of LIST.  (This is the same as the car of
     (list-tail LIST K).)  It is an error if LIST has fewer than K
     elements.

     (list-ref '(a b c d) 2)                 ==>  c
     (list-ref '(a b c d)
               (inexact->exact (round 1.8)))
               ==>  c


 - library procedure: memq obj list
 - library procedure: memv obj list
 - library procedure: member obj list
     These procedures return the first sublist of LIST whose car is
     OBJ, where the sublists of LIST are the non-empty lists returned
     by (list-tail LIST K) for K less than the length of LIST.  If OBJ
     does not occur in LIST, then #f (not the empty list) is returned.
     `Memq' uses `eq?' to compare OBJ with the elements of LIST, while
     `memv' uses `eqv?' and `member' uses `equal?'.

     (memq 'a '(a b c))                     ==>  (a b c)
     (memq 'b '(a b c))                     ==>  (b c)
     (memq 'a '(b c d))                     ==>  #f
     (memq (list 'a) '(b (a) c))            ==>  #f
     (member (list 'a)
             '(b (a) c))                    ==>  ((a) c)
     (memq 101 '(100 101 102))              ==>  _unspecified_
     (memv 101 '(100 101 102))              ==>  (101 102)


 - library procedure: assq obj alist
 - library procedure: assv obj alist
 - library procedure: assoc obj alist
     ALIST (for "association list") must be a list of pairs.  These
     procedures find the first pair in ALIST whose car field is OBJ,
     and returns that pair.  If no pair in ALIST has OBJ as its car,
     then #f (not the empty list) is returned.  `Assq' uses `eq?' to
     compare OBJ with the car fields of the pairs in ALIST, while
     `assv' uses `eqv?' and `assoc' uses `equal?'.

     (define e '((a 1) (b 2) (c 3)))
     (assq 'a e)                            ==>  (a 1)
     (assq 'b e)                            ==>  (b 2)
     (assq 'd e)                            ==>  #f
     (assq (list 'a) '(((a)) ((b)) ((c))))
                                            ==>  #f
     (assoc (list 'a) '(((a)) ((b)) ((c))))
                                            ==>  ((a))
     (assq 5 '((2 3) (5 7) (11 13)))
                                            ==>  _unspecified_
     (assv 5 '((2 3) (5 7) (11 13)))
                                            ==>  (5 7)

          _Rationale:_ Although they are ordinarily used as predicates,
          `memq', `memv', `member', `assq', `assv', and `assoc' do not
          have question marks in their names because they return useful
          values rather than just #t or #f.



File: r5rs.info,  Node: Symbols,  Next: Characters,  Prev: Pairs and lists,  Up: Other data types

Symbols
-------

Symbols are objects whose usefulness rests on the fact that two symbols
are identical (in the sense of `eqv?') if and only if their names are
spelled the same way.  This is exactly the property needed to represent
identifiers in programs, and so most implementations of Scheme use them
internally for that purpose.  Symbols are useful for many other
applications; for instance, they may be used the way enumerated values
are used in Pascal.

The rules for writing a symbol are exactly the same as the rules for
writing an identifier; see sections *Note Identifiers:: and *Note
Lexical structure::.

It is guaranteed that any symbol that has been returned as part of a
literal expression, or read using the `read' procedure, and
subsequently written out using the `write' procedure, will read back in
as the identical symbol (in the sense of `eqv?').  The `string->symbol'
procedure, however, can create symbols for which this write/read
invariance may not hold because their names contain special characters
or letters in the non-standard case.

     _Note:_ Some implementations of Scheme have a feature known as
     "slashification" in order to guarantee write/read invariance for
     all symbols, but historically the most important use of this
     feature has been to compensate for the lack of a string data type.

     Some implementations also have "uninterned symbols", which defeat
     write/read invariance even in implementations with slashification,
     and also generate exceptions to the rule that two symbols are the
     same if and only if their names are spelled the same.

 - procedure: symbol? obj
     Returns #t if OBJ is a symbol, otherwise returns #f.

     (symbol? 'foo)                         ==>  #t
     (symbol? (car '(a b)))                 ==>  #t
     (symbol? "bar")                        ==>  #f
     (symbol? 'nil)                         ==>  #t
     (symbol? '())                          ==>  #f
     (symbol? #f)                           ==>  #f


 - procedure: symbol->string symbol
     Returns the name of SYMBOL as a string.  If the symbol was part of
     an object returned as the value of a literal expression (section
     *note Literal expressions::) or by a call to the `read' procedure,
     and its name contains alphabetic characters, then the string
     returned will contain characters in the implementation's preferred
     standard case--some implementations will prefer upper case, others
     lower case.  If the symbol was returned by `string->symbol', the
     case of characters in the string returned will be the same as the
     case in the string that was passed to `string->symbol'.  It is an
     error to apply mutation procedures like `string-set!' to strings
     returned by this procedure.

     The following examples assume that the implementation's standard
     case is lower case:

     (symbol->string 'flying-fish)
                                            ==>  "flying-fish"
     (symbol->string 'Martin)               ==>  "martin"
     (symbol->string
        (string->symbol "Malvina"))
                                            ==>  "Malvina"


 - procedure: string->symbol string
     Returns the symbol whose name is STRING.  This procedure can
     create symbols with names containing special characters or letters
     in the non-standard case, but it is usually a bad idea to create
     such symbols because in some implementations of Scheme they cannot
     be read as themselves.  See `symbol->string'.

     The following examples assume that the implementation's standard
     case is lower case:

     (eq? 'mISSISSIppi 'mississippi)
               ==>  #t
     (string->symbol "mISSISSIppi")
               ==>
       the symbol with name "mISSISSIppi"
     (eq? 'bitBlt (string->symbol "bitBlt"))
               ==>  #f
     (eq? 'JollyWog
          (string->symbol
            (symbol->string 'JollyWog)))
               ==>  #t
     (string=? "K. Harper, M.D."
               (symbol->string
                 (string->symbol "K. Harper, M.D.")))
               ==>  #t



File: r5rs.info,  Node: Characters,  Next: Strings,  Prev: Symbols,  Up: Other data types

Characters
----------

Characters are objects that represent printed characters such as
letters and digits.  Characters are written using the notation
#\<character> or #\<character name>.  For example:

                                   
    #\a
          ; lower case letter

    #\A
          ; upper case letter

    #\(
          ; left parenthesis

    #\
          ; the space character

    #\space
          ; the preferred way to write a space

    #\newline
          ; the newline character



Case is significant in #\<character>, but not in #\<character name>.

If <character> in #\<character> is alphabetic, then the character
following <character> must be a delimiter character such as a space or
parenthesis.  This rule resolves the ambiguous case where, for example,
the sequence of characters "#\ space" could be taken to be either a
representation of the space character or a representation of the
character "#\ s" followed by a representation of the symbol "pace."

Characters written in the #\ notation are self-evaluating.  That is,
they do not have to be quoted in programs.

Some of the procedures that operate on characters ignore the difference
between upper case and lower case.  The procedures that ignore case
have "-ci" (for "case insensitive") embedded in their names.

 - procedure: char? obj
     Returns #t if OBJ is a character, otherwise returns #f.


 - procedure: char=? char1 char2
 - procedure: char<? char1 char2
 - procedure: char>? char1 char2
 - procedure: char<=? char1 char2
 - procedure: char>=? char1 char2
     These procedures impose a total ordering on the set of characters.
     It is guaranteed that under this ordering:

        * The upper case characters are in order.  For example,
          `(char<? #\A #\B)' returns #t.

        * The lower case characters are in order.  For example,
          `(char<? #\a #\b)' returns #t.

        * The digits are in order.  For example, `(char<? #\0 #\9)'
          returns #t.

        * Either all the digits precede all the upper case letters, or
          vice versa.

        * Either all the digits precede all the lower case letters, or
          vice versa.


     Some implementations may generalize these procedures to take more
     than two arguments, as with the corresponding numerical predicates.


 - library procedure: char-ci=? char1 char2
 - library procedure: char-ci<? char1 char2
 - library procedure: char-ci>? char1 char2
 - library procedure: char-ci<=? char1 char2
 - library procedure: char-ci>=? char1 char2
     These procedures are similar to `char=?' et cetera, but they treat
     upper case and lower case letters as the same.  For example,
     `(char-ci=? #\A #\a)' returns #t.  Some implementations may
     generalize these procedures to take more than two arguments, as
     with the corresponding numerical predicates.


 - library procedure: char-alphabetic? char
 - library procedure: char-numeric? char
 - library procedure: char-whitespace? char
 - library procedure: char-upper-case? letter
 - library procedure: char-lower-case? letter
     These procedures return #t if their arguments are alphabetic,
     numeric, whitespace, upper case, or lower case characters,
     respectively, otherwise they return #f.  The following remarks,
     which are specific to the ASCII character set, are intended only
     as a guide:  The alphabetic characters are the 52 upper and lower
     case letters.  The numeric characters are the ten decimal digits.
     The whitespace characters are space, tab, line feed, form feed,
     and carriage return.

 - procedure: char->integer char
 - procedure: integer->char N
     Given a character, `char->integer' returns an exact integer
     representation of the character.  Given an exact integer that is
     the image of a character under `char->integer', `integer->char'
     returns that character.  These procedures implement
     order-preserving isomorphisms between the set of characters under
     the `char<=?' ordering and some subset of the integers under the
     `<=' ordering.  That is, if

     (char<=? A B) => #t  and  (<= X Y) => #t

     and X and Y are in the domain of `integer->char', then

     (<= (char->integer A)
         (char->integer B))                 ==>  #t
     
     (char<=? (integer->char X)
              (integer->char Y))            ==>  #t


 - library procedure: char-upcase char
 - library procedure: char-downcase char
     These procedures return a character CHAR2 such that `(char-ci=?
     CHAR CHAR2)'.  In addition, if CHAR is alphabetic, then the result
     of `char-upcase' is upper case and the result of `char-downcase'
     is lower case.


