10.2.2 List Operations
These procedures are similar to procedures on normal lists.
(empty-node-list)
Returns an empty node-list.
(node-list-reduce nl proc obj)
If nl has no members, returns obj, and otherwise returns the result of applying node-list-reduce to

•  a node-list containing all but the first member of nl,

•  proc, and

•  the result of applying proc to obj and the first member of nl.
node-list-reduce could be defined as follows:
(define (node-list-reduce nl combine init)
  (if (node-list-empty? nl)
      init
      (node-list-reduce (node-list-rest nl)
                        combine
                        (combine init (node-list-first nl)))))
(node-list-contains? nl snl)
Returns #t if nl contains a node equal to the member of snl, and otherwise returns #f.  This could be defined as follows:
(define (node-list-contains? nl snl)
  (node-list-reduce nl
                    (lambda (result i)
                      (or result
                          (node-list=? snl i)))
                    #f))
(node-list-remove-duplicates nl)
Returns a node-list which is the same as nl except that any member of nl which is equal to a preceding member of nl is removed. This could be defined as follows:
(define (node-list-remove-duplicates nl)
  (node-list-reduce nl
                    (lambda (result snl)
                      (if (node-list-contains? result snl)
                          result
                          (node-list result snl)))
                    (empty-node-list)))
(node-list-union #!rest args)
Returns a node-list containing the union of all the arguments, which shall be node-lists.  The result shall contain no duplicates.  With no arguments, an empty node-list shall be returned.  This could be defined as follows:
(define (node-list-union #!rest args)
  (reduce args
          (lambda (nl1 nl2)
            (node-list-reduce nl2
                              (lambda (result snl)
                                (if (node-list-contains? result
                                                         snl)
                                    result
                                    (node-list result snl)))
                              nl1))
          (empty-node-list)))
where reduce is defined as follows:
(define (reduce list combine init)
  (let loop ((result init)
             (list list))
    (if (null? list)
        result
        (loop (combine result (car list))
              (cdr list)))))
(node-list-intersection #!rest args)
Returns a node-list containing the intersection of all the arguments, which shall be node-lists.  The result shall contain no duplicates.  With no arguments, an empty node-list shall be returned.  This could be defined as follows:
(define (node-list-intersection #!rest args)
  (if (null? args)
      (empty-node-list)
      (reduce (cdr args)
              (lambda (nl1 nl2)
                (node-list-reduce nl1
                                  (lambda (result snl)
                                    (if (node-list-contains? nl2 snl)
                                        (node-list result snl)
                                        result))
                                  (empty-node-list)))
              (node-list-remove-duplicates (car args)))))
(node-list-difference #!rest args)
Returns a node-list containing the set difference of all the arguments, which shall be node-lists.  The set difference is defined to be those members of the first argument that are not members of any of the other arguments.  The result shall contain no duplicates.  With no arguments, an empty node-list shall be returned.  This could be defined as follows:
(define (node-list-difference #!rest args)
  (if (null? args)
      (empty-node-list)
      (reduce (cdr args)
              (lambda (nl1 nl2)
                (node-list-reduce nl1
                                  (lambda (result snl)
                                    (if (node-list-contains? nl2 snl)
                                        result
                                        (node-list result snl)))
                                  (empty-node-list)))
              (node-list-remove-duplicates (car args)))))
(node-list-symmetric-difference #!rest args)
Returns a node-list containing the symmetric set difference of all the arguments, which shall be node-lists.  The symmetric set difference is defined to be those nodes that occur in exactly one of the arguments. The result shall contain no duplicates.  With no arguments, an empty node-list shall be returned.  This could be defined as follows:
(define (node-list-symmetric-difference #!rest args)
  (if (null? args)
      (empty-node-list)
      (reduce (cdr args)
              (lambda (nl1 nl2)
                (node-list-difference (node-list-union nl1 nl2)
                                      (node-list-intersection nl1 nl2)))
              (node-list-remove-duplicates (car args)))))
(node-list-map proc nl)
For each member of nl, applies proc to a singleton node-list containing just that member and appends the resulting node-lists. It shall be an error if proc does not return a node-list when applied to any member of nl. This could be defined as follows:
(define (node-list-map proc nl)
  (node-list-reduce nl
                    (lambda (result snl)
                      (node-list (proc snl)
                                 result))
                    (empty-node-list)))
(node-list-union-map proc nl)
For each member of nl, applies proc to a singleton node-list containing just that member and returns the union of the resulting node-lists.  It shall be an error if proc does not return a node-list when applied to any member of nl.  This could be defined as follows:
(define (node-list-union-map proc nl)
  (node-list-reduce nl
                    (lambda (result snl)
                      (node-list-union (proc snl)
                                       result))
                    (empty-node-list)))
(node-list-some? proc nl)
Returns #t if, for some member of nl, proc does not return #f when applied to a singleton node-list containing just that member, and otherwise returns #f.  An implementation is allowed, but not required, to signal an error if, for some member of nl, proc would signal an error when applied to a singleton node-list containing just that member.  This could be defined as follows:
(define (node-list-some? proc nl)
  (node-list-reduce nl
                    (lambda (result snl)
                      (if (or result (proc snl))
                          #t
                          #f))
                    #f))
(node-list-every? proc nl)
Returns #t if, for every member of nl, proc does not return #f when applied to a singleton node-list containing just that member, and otherwise returns #f.  An implementation is allowed to signal an error if, for some member of nl, proc would signal an error when applied to a singleton node-list containing just that member.  This could be defined as follows:
(define (node-list-every? proc nl)
  (node-list-reduce nl
                    (lambda (result snl)
                      (if (and result (proc snl))
                          #t
                          #f))
                    #t))
(node-list-filter proc nl)
Returns a node-list containing just those members of nl for which proc applied to a singleton node-list containing just that member does not return #f. This could be defined as follows:
(define (node-list-filter proc nl)
  (node-list-reduce nl
                    (lambda (result snl)
                      (if (proc snl)
                          (node-list snl result)
                          result))
                    (empty-node-list)))
(node-list->list nl)
Returns a list containing, for each member of nl, a singleton node-list containing just that member.  This could be defined as follows:
(define (node-list->list nl)
  (reverse (node-list-reduce nl
                             (lambda (result snl)
                               (cons snl result))
                             '())))
(node-list-length nl)
Returns the length of nl. This could be defined as follows:
(define (node-list-length nl)
  (node-list-reduce nl
                    (lambda (result snl)
                      (+ result 1))
                    0))
(node-list-reverse nl)
Returns a node-list containing the members of nl in reverse order. This could be defined as follows:
(define (node-list-reverse nl)
  (node-list-reduce nl
                    (lambda (result snl)
                      (node-list snl result))
                    (empty-node-list)))
(node-list-ref nl k)
Returns a node-list containing the kth member of nl (zero-based), if there is such a member, and otherwise returns the empty node-list.  This could be defined as follows:
(define (node-list-ref nl i)
  (cond ((< i 0)
         (empty-node-list))
        ((zero? i)
         (node-list-first nl))
        (else
         (node-list-ref (node-list-rest nl) (- i 1)))))
(node-list-tail nl k)
Returns the node-list comprising all but the first k members of nl. If nl has k or fewer members, returns the empty node-list. This could be defined as follows:
(define (node-list-tail nl i)
  (cond ((< i 0) (empty-node-list))
        ((zero? i) nl)
        (else
         (node-list-tail (node-list-rest nl) (- i 1)))))
(node-list-head nl k)
Returns a node-list comprising the first k members of nl. If nl has k or fewer members, returns nl. This could be defined as follows.
(define (node-list-head nl i)
  (if (zero? i)
      (empty-node-list)
      (node-list (node-list-first nl)
                 (node-list-head nl (- i 1)))))
(node-list-sublist nl k1 k2)
Returns a node-list containing those members of nl that are preceded in nl by at least k1 members but fewer than k2 members.  This is equivalent to selecting those members whose zero-based index in nl is greater than or equal to k1 but less than k2.  This could be defined as follows:
(define (node-list-sublist nl i j)
  (node-list-head (node-list-tail nl i)
                  (- j i)))
(node-list-count nl)
Returns the number of distinct members of nl. This could be defined as follows:
(define (node-list-count nl)
  (node-list-length (node-list-remove-duplicates nl)))
(node-list-last nl)
Returns a node-list containing the last member of nl, if nl is not empty, and otherwise returns the empty node-list. This could be defined as follows:
(define (node-list-last nl)
  (node-list-ref nl
                 (- (node-list-length nl) 1)))
When using node-list-some?, node-list-every?, node-list-filter, and node-list-union-map, the first argument is often a lambda expression with a variable.  A syntax that avoids the need to use an explicit lambda expression in this case is provided in this International Standard.
[146] special-query-expression = there-exists?-expression | for-all?-expression | select-each-expression | union-for-each-expression
[147] there-exists?-expression = (there-exists? variable expression expression)
An expression
(there-exists? var nl-expr expr)
is equivalent to:
(node-list-some? (lambda (varexprnl-expr)
Read this as: there exists a var in nl-expr such that expr.
[148] for-all?-expression = (for-all? variable expression expression )
An expression
(for-all? var nl-expr expr)
is equivalent to:
(node-list-every? (lambda (varexprnl-expr)
Read this as: for all var in nl-expr, expr.
[149] select-each-expression = (select-each variable expression expression)
An expression
(select-each var nl-expr expr)
is equivalent to:
(node-list-filter (lambda (varexprnl-expr)
Read this as: select each var in nl-expr such that expr.
[150] union-for-each-expression = (union-for-each variable expression expression)
An expression
(union-for-each var nl-expr expr)
is equivalent to:
(node-list-union-map (lambda (varexprnl-expr)
Read this as: the union of, for each var in nl-expr, expr.