Records
LARD's second structured data type is the record. LARD's records are
like records in Pascal and structs in C - they are hetrogenous
collections of named components. Here is a simple example of how to
declare and use a record:
use(io).
bounding_box:type=record(left::int,
top::int,
right::int,
bottom::int).
coordinate:type=record(x::int,y::int).
within(c:val(coordinate),bb:val(bounding_box)):expr(bool)=(
c->x >= bb->left && c->x < bb->right
&& c->y >= bb->bottom && c->y < bb->top
).
print(within(cast(coordinate,{10,15}),
cast(bounding_box,{5,20,20,0})));
print('\n')
- A record declaration lists each of the fields. The field
declaration takes the form name :: type.
- The field selection operator is ->.
- Records can be constructed using the syntax {
value , value , ... }. Since
these records are anonymous, i.e. they don't indicate what particular
record declaration they corespond to, it is necessary to cast them to
the desired type in most cases.