Common Lisp the Language, 2nd Edition
The following function may be used to convert an object to an equivalent object of another type.
[Function]
coerce object result-type
The result-type must be a type specifier; the object is converted to an ``equivalent'' object of the specified type. If the coercion cannot be performed, then an error is signaled. In particular, (coerce x 'nil) always signals an error. If object is already of the specified type, as determined by typep, then it is simply returned. It is not generally possible to convert any object to be of any type whatsoever; only certain conversions are permitted:
(coerce '(a b c) 'vector) => #(a b c)
X3J13 voted in June 1989 (SEQUENCE-TYPE-LENGTH) to specify that
coerce should signal an error if the new sequence type specifies the
number of elements and the old sequence has a different length.
X3J13 voted in March 1989 (CHARACTER-PROPOSAL)
to specify that if the result-type is string
then it is understood to mean (vector character),
and simple-string is understood to mean (simple-array character (*)).
(coerce "a" 'character) => #\a
X3J13 voted in March 1989 (CHARACTER-PROPOSAL)
to eliminate int-char from Common Lisp.
Presumably this eliminates the possibility of coercing an
integer to a character, although the vote did not address
this question directly.
(coerce 0 'short-float) => 0.0S0 (coerce 3.5L0 'float) => 3.5L0 (coerce 7/2 'float) => 3.5
(coerce 4.5s0 'complex) => #C(4.5S0 0.0S0) (coerce 7/2 'complex) => 7/2 (coerce #C(7/2 0) '(complex double-float)) => #C(3.5D0 0.0D0)
(coerce x 't) == (identity x) == x
X3J13 voted in June 1988 (FUNCTION-TYPE)
to allow coercion of certain objects to the type function:
Coercions from floating-point numbers to rationals and from ratios to integers are purposely not provided because of rounding problems. The functions rational, rationalize, floor, ceiling, truncate, and round may be used for such purposes. Similarly, coercions from characters to integers are purposely not provided; char-code or char-int may be used explicitly to perform such conversions.