Integers are whole numbers, that is numbers with no fractional part, such as 2, 83, and -3789.
Integers in Guile can be arbitrarily big, as shown by the following example.
(define (factorial n) (let loop ((n n) (product 1)) (if (= n 0) product (loop (- n 1) (* product n))))) (factorial 3) ⇒ 6 (factorial 20) ⇒ 2432902008176640000 (- (factorial 45)) ⇒ -119622220865480194561963161495657715064383733760000000000
Readers whose background is in programming languages where integers are limited by the need to fit into just 4 or 8 bytes of memory may find this surprising, or suspect that Guile’s representation of integers is inefficient. In fact, Guile achieves a near optimal balance of convenience and efficiency by using the host computer’s native representation of integers where possible, and a more general representation where the required number does not fit in the native form. Conversion between these two representations is automatic and completely invisible to the Scheme level programmer.
C has a host of different integer types, and Guile offers a host of
functions to convert between them and the
For example, a C
int can be handled with
scm_from_int. Guile also defines a few C integer types of its
own, to help with differences between systems.
C integer types that are not covered can be handled with the generic
signed types, or with
scm_from_unsigned_integer for unsigned types.
Scheme integers can be exact and inexact. For example, a number
3.0 with an explicit decimal-point is inexact, but
it is also an integer. The functions
scm_is_integer report true for such a number, but the functions
allow exact integers and thus report false. Likewise, the conversion
scm_to_signed_integer only accept exact
The motivation for this behavior is that the inexactness of a number
should not be lost silently. If you want to allow inexact integers,
you can explicitly insert a call to
inexact->exact or to its C
scm_inexact_to_exact. (Only inexact integers will
be converted by this call into exact integers; inexact non-integers
will become exact fractions.)
#t if x is an exact or inexact integer number, else
(integer? 487) ⇒ #t (integer? 3.0) ⇒ #t (integer? -3.4) ⇒ #f (integer? +inf.0) ⇒ #f
This is equivalent to
scm_is_true (scm_integer_p (x)).
#t if x is an exact integer number, else
(exact-integer? 37) ⇒ #t (exact-integer? 3.0) ⇒ #f
This is equivalent to
scm_is_true (scm_exact_integer_p (x)).
The C types are equivalent to the corresponding ISO C types but are
defined on all platforms, with the exception of
scm_t_uint64, which are only defined when a 64-bit type is
available. For example,
scm_t_int8 is equivalent to
You can regard these definitions as a stop-gap measure until all platforms provide these types. If you know that all the platforms that you are interested in already provide these types, it is better to use them directly instead of the types provided by Guile.
1 when x represents an exact integer that is
between min and max, inclusive.
These functions can be used to check whether a
SCM value will
fit into a given range, such as the range of a given C integer type.
If you just want to convert a
SCM value to a given C integer
type, use one of the conversion functions directly.
When x represents an exact integer that is between min and max inclusive, return that integer. Else signal an error, either a ‘wrong-type’ error when x is not an exact integer, or an ‘out-of-range’ error when it doesn’t fit the given range.
SCM value that represents the integer x. This
function will always succeed and will always return an exact number.
When x represents an exact integer that fits into the indicated C type, return that integer. Else signal an error, either a ‘wrong-type’ error when x is not an exact integer, or an ‘out-of-range’ error when it doesn’t fit the given range.
scm_to_uint64 are only available when
the corresponding types are.
SCM value that represents the integer x.
These functions will always succeed and will always return an exact
Assign val to the multiple precision integer rop.
val must be an exact integer, otherwise an error will be
signalled. rop must have been initialized with
before this function is called. When rop is no longer needed
the occupied space must be freed with
See Initializing Integers in GNU MP Manual, for details.
SCM value that represents val.