Warning:This is the manual of the legacy Guile2.0series. You may want to read the manual of the current stable series instead.

Next: Faster Integers, Up: Data Representation [Contents][Index]

The simplest way to represent Scheme values in C would be to represent
each value as a pointer to a structure containing a type indicator,
followed by a union carrying the real value. Assuming that `SCM`

is
the name of our universal type, we can write:

enum type { integer, pair, string, vector, ... }; typedef struct value *SCM; struct value { enum type type; union { int integer; struct { SCM car, cdr; } pair; struct { int length; char *elts; } string; struct { int length; SCM *elts; } vector; ... } value; };

with the ellipses replaced with code for the remaining Scheme types.

This representation is sufficient to implement all of Scheme’s
semantics. If `x` is an `SCM`

value:

- To test if
`x`is an integer, we can write

.`x`->type == integer - To find its value, we can write

.`x`->value.integer - To test if
`x`is a vector, we can write

.`x`->type == vector - If we know
`x`is a vector, we can write

to refer to its first element.`x`->value.vector.elts[0] - If we know
`x`is a pair, we can write

to extract its car.`x`->value.pair.car