4.8 Type Conversion Function

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:

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.