Next: , Previous: Miscellaneous features, Up: System features


4.1.2 Various utilities

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.

— procedure: unspecific –> unspecific

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.

— procedure: reduce kons knil list –> final-knil

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)
— procedure: fold combiner list accumulator –> final-accumulator

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)
— procedure: fold->2 combiner list accumulator1 accumulator2 –> [final-accumulator1 final-accumulator2]
— procedure: fold->3 combiner list accumulator1 accumulator2 accumulator3 –> [final-accumulator1 final-accumulator2 final-accumulator3]

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
                   '() '())
— procedure: filter predicate list –> filtered-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)
— procedure: posq object list –> integer or #f
— procedure: posv object list –> integer or #f
— procedure: position object list –> integer or #f

These find the position of the first element equal to object in list. Posq compares elements by eq?; posv compares by eqv?; position compares by equal?.

          (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
— procedure: any predicate list –> value or #f
— procedure: every predicate list –> boolean

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
— procedure: sublist list start end –> list

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)
— procedure: last list –> value

Returns the last element in list. Last's effect is undefined if list is empty.

          (last '(a b c))                         => c
— procedure: insert object list elt< –> list

Inserts object into the sorted list list, comparing the order of object and each element by elt<.

          (insert 3 '(0 1 2 4 5) <)               => (0 1 2 3 4 5)