Next: Filenames, Previous: Miscellaneous features, Up: System features
The util
structure contains some miscellaneous utility routines
extensively used internally in the run-time system. While they are not
meant to compose a comprehensive library (such as, for example, [SRFI
1]), they were found useful in building the run-time system without
introducing massive libraries into the core of the system.
Returns Scheme48's unspecific token, which is used wherever R5RS uses the term `unspecific' or `unspecified.' In this manual, the term `unspecified' is used to mean that the values returned by a particular procedure are not specified and may be anything, including a varying number of values, whereas `unspecific' refers to Scheme48's specific `unspecific' value that the
unspecific
procedure returns.
Reduces list by repeatedly applying kons to elements of list and the current knil value. This is the fundamental list recursion operator.
(reduce kons knil (cons elt1 (cons elt2 (...(cons eltN '())...)))) == (kons elt1 (kons elt2 (...(kons eltN knil)...)))Example:
(reduce append '() '((1 2 3) (4 5 6) (7 8 9))) => (1 2 3 4 5 6 7 8 9) (append '(1 2 3) (append '(4 5 6) (append '(7 8 9) '()))) => (1 2 3 4 5 6 7 8 9)
Folds list into an accumulator by repeatedly combining each element into an accumulator with combiner. This is the fundamental list iteration operator.
(fold combiner (list elt1 elt2 ... eltN) accumulator) == (let* ((accum1 (combiner elt1 accumulator)) (accum2 (combiner elt2 accum1)) ... (accumN (combiner eltN accumN-1))) accumN)Example:
(fold cons '() '(a b c d)) => (d c b a) (cons 'd (cons 'c (cons 'b (cons 'a '())))) => (d c b a)
Variants of
fold
for two and three accumulators, respectively.;;; Partition list by elements that satisfy pred? and those ;;; that do not. (fold->2 (lambda (elt satisfied unsatisfied) (if (pred? elt) (values (cons elt satisfied) unsatisfied) (values satisfied (cons elt unsatisfied)))) list '() '())
Returns a list of all elements in list that satisfy predicate.
(filter odd? '(3 1 4 1 5 9 2 6 5 3 5)) => (3 1 1 5 9 5 3 5)
#f
#f
#f
These find the position of the first element equal to object in list.
Posq
compares elements byeq?
;posv
compares byeqv?
;position
compares byequal?
.(posq 'c '(a b c d e f)) => 2 (posv 1/2 '(1 1/2 2 3/2)) => 1 (position '(d . e) '((a . b) (b . c) (c . d) (d . e) (e . f))) => 3
#f
Any
returns the value that predicate returns for the first element in list for which predicate returns a true value; if no element of list satisfied predicate,any
returns#f
.Every
returns#t
if every element of list satisfies predicate, or#f
if there exist any that do not.(any (lambda (x) (and (even? x) (sqrt x))) '(0 1 4 9 16)) => 2 (every odd? '(1 3 5 7 9)) => #t
Returns a list of the elements in list including & after that at the index start and before the index end.
(sublist '(a b c d e f g h i) 3 6) => (d e f)