Next: one-dimension make, Up: one-dimension [Index]
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:
Any object.
An object that satisfies the item? predicate of the selected type.
A valid range according to %range?
.
A valid domain according to %domain?
.
The included left–limit of a range.
The excluded lower–limit of a range.
The included upper–limit of a range.
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)))
An n–ary predicate returning #t
if the all the arguments are
equal. char=?
is an example of such a function.
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.
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.
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))
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))
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))))
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))))
A binary function:
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))))
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)
.
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: one-dimension make, Up: one-dimension [Index]