Next: , Up: one-dimension   [Index]

### 33.1 Data types and conventions

A range is a pair representing a half–open interval of items. The car of the pair if the included lower–limit, called start; the cdr is the excluded upper–limit, called past, or the included upper–limit, called last. Empty ranges are not valid.

A domain is a sorted list of ranges. Empty domains are empty lists. Ranges in a domain do not overlap and are not contiguous. A domain of half–open ranges has the following format:

```((start1 . past1) (start2 . past2) (start3 . past3) ...)
```

with the constraints:

```start1 < past1 < start2 < past2 < start3 < past3 < ...
```

a domain of double–closed ranges has the following format:

```((start1 . last1) (start2 . last2) (start3 . last3) ...)
```

with the constraints:

```start1 <= last1 < start2 <= last2 < start3 <= last3 < ...
```

The range and domain functions accept the following arguments:

obj

Any object.

item

An object that satisfies the item? predicate of the selected type.

range

A valid range according to `%range?`.

domain

A valid domain according to `%domain?`.

start

The included left–limit of a range.

past

The excluded lower–limit of a range.

last

The included upper–limit of a range.

item?

A unary predicate returning `#t` if the argument is a valid item. For integers it can be `integer?`, while for characters it can be `char?`.

If we need to exclude some range of values, we can do it by properly define this function. For example, to exclude the integers in the half–open range [10, 23) we can use:

```(lambda (n)
(and (integer? n)
(<= 10  n)
(<   n 23)))
```
item=?

An n–ary predicate returning `#t` if the all the arguments are equal. `char=?` is an example of such a function.

item<?

An n–ary predicate returning `#t` if the all the arguments, in the given order, are in strict increasing order. `char<?` is an example of such a function.

item<=?

An n–ary predicate returning `#t` if the all the arguments, in the given order, are in non–strict increasing order. `char<=?` is an example of such a function.

item-min

An n–ary function returning the minimum between its arguments. For numbers it can be `min`, for characters we can use:

```(lambda (a b) (if (char<? a b) a b))
```
item-max

An n–ary function returning the maximum between its arguments. For numbers it can be `max`, for characters we can use:

```(lambda (a b) (if (char<? a b) b a))
```
item-prev

A binary function accepting an item and a range, or false, as arguments. The second argument can be `#f`, meaning that no range is specified, or a pair whose car is the left–limit and whose cdr is the right–limit; when the range is true, it is guaranteed that the item is inside the range.

This function must return the value previous to item inside the range; if it is not possible to compute the previous value, because the lower bound has been reached: The function must return `#f`.

For integers it can be:

```(lambda (x range)       ; with both (one-dimension-cc)
(let ((x (- x 1)))    ; and (one-dimension-co)
(if range
(and (<= (car range) x)
x)
x)))
```

for characters it can be:

```(lambda (ch range)      ; with (one-dimension-cc)
(let* ((x  (- (char->integer ch) 1)))
(and (number-in-range? x)
(let ((ch (integer->char x)))
(if range
(and (<= x (char->integer (car range)))
ch)
ch)))))

(define (number-in-range? x)
(or (and (<= 0 x)
(<  x #xD800))
(and (<  #xDFFF x)
(<= x #x10FFFF))))
```
item-next

A binary function accepting an item and a range, or false, as arguments. The second argument can be `#f`, meaning that no range is specified, or a pair whose car is the left–limit and whose cdr is the right–limit; when the range is true, it is guaranteed that the item is inside the range.

This function must return the value next to item inside the range; if it is not possible to compute the next value, because the upper bound has been reached: The function must return `#f`.

For integers it can be:

```(lambda (x range)       ; with (one-dimension-cc)
(let ((x (+ 1 x)))
(if range
(and (<= x (cdr range))
x)
x)))

(lambda (x range)       ; with (one-dimension-co)
(let ((x (+ 1 x)))
(if range
(and (< x (cdr range))
x)
x)))
```

for characters it can be:

```(lambda (ch range)      ; with (one-dimension-cc)
(let* ((x  (+ 1 (char->integer c))))
(and (number-in-range? x)
(let ((ch (integer->char x)))
(if range
(and (<= x (char->integer (cdr range)))
ch)
ch)))))

(define (number-in-range? x)
(or (and (<= 0 x)
(<  x #xD800))
(and (<  #xDFFF x)
(<= x #x10FFFF))))
```
item-minus

A binary function:

• For half–open ranges: It must return the number of items between the first argument included and the second argument excluded.
• For double–closed ranges: It must return the number of items between the first argument included and the second argument included.

The arithmetic `-` applied to integers is an example of such a function for half–open ranges; for characters and double–closed ranges we can use:

```(lambda (past start)
(+ 1 (- (char->integer past)
(char->integer start))))
```
item-copy

A unary function returning a copy of an item. For ranges holding atomic values (like characters or integers) it is fine to use ```(lambda (x) x)```.

type

A record used as type descriptor for collected items. It is used as first arguments to all almost all the functions. Type descriptors are built by `%make-type-descriptor`.

Next: , Up: one-dimension   [Index]