Next: , Previous: , Up: srfi comparators constructors   [Index]


2.38.8.4 List comparators

Function: make-list-comparator element-comparator

Build and return a comparator object behaving like list-comparator but using element-comparator rather than default-comparator.

#!vicare
(import (vicare) (srfi :114))

(define-constant C
  (make-list-comparator exact-integer-comparator))

;; type test
(comparator-test-type C '())            ⇒ #t
(comparator-test-type C '(1 2))         ⇒ #t
(comparator-test-type C '(1 2 . 3))     ⇒ #f
(comparator-test-type C '(1 2.0))       ⇒ #f
(comparator-test-type C "ciao")         ⇒ #f
(comparator-test-type C '(1+2i))        ⇒ #f

;; type check
(comparator-check-type C '(1 2))        ⇒ #t
(try
    (comparator-check-type C (void))
  (catch E
    ((&comparator-type-error)
     #t)
    (else E)))
⇒ #t

;; comparison
(comparator-compare C '(1 2) '(1 2))   ⇒ 0)
(comparator-compare C '(1 2) '(1 3))   ⇒ -1
(comparator-compare C '(1 3) '(1 2))   ⇒ +1

(comparator-compare C '()    '())      ⇒ 0
(comparator-compare C '()    '(1 2))   ⇒ -1
(comparator-compare C '(1 2) '())      ⇒ +1

;; hash
(non-negative-exact-integer?
  (comparator-hash C '()))    ⇒ #t
(non-negative-exact-integer?
  (comparator-hash C '(1 2))) ⇒ #t
Function: make-listwise-comparator type-test element-comparator empty? head tail

Return a comparator which compares two objects that satisfy type-test as if they were lists, using the empty? procedure to determine if an object is empty, and the head and tail procedures to access particular elements.

Function: make-improper-list-comparator element-comparator

Return a comparator that compares arbitrary objects as follows: the empty list precedes all pairs, which precede all other objects. Pairs are compared as if with:

(make-pair-comparator element-comparator element-comparator)

All other objects are compared using element-comparator.

Notice that element-comparator is used only to compare elements and compute the hash value elements; it is not used to validate the type of elements.

#!vicare
(import (vicare) (srfi :114))

(module (C)

  (define element-compare
    (let ((compare (comparator-comparison-procedure
                      exact-integer-comparator)))
      (lambda (A B)
        (if (pair? A)
            (begin
              (assert (pair? B))
              (let ((rv (compare (car A) (car B))))
                (if (zero? rv)
                    (comparator-compare C (cdr A) (cdr B))
                  rv)))
          (compare A B)))))

  (define-constant E
    (make-comparator #t #t
       element-compare
       (comparator-hash-function default-comparator)))

  (define-constant C
    (make-improper-list-comparator E))

  #| end of module |# )

;; type test
(comparator-test-type C '())            ⇒ #t
(comparator-test-type C '(1 2))         ⇒ #t
(comparator-test-type C '(1 2 . 3))     ⇒ #t
(comparator-test-type C '(1 2.0))       ⇒ #t
(comparator-test-type C "ciao")         ⇒ #t
(comparator-test-type C '(1+2i))        ⇒ #t

;; type check
(comparator-check-type C '(1 2))        ⇒ #t
(comparator-check-type C (void))        ⇒ #t

;; comparison
(comparator-compare C '(1 2) '(1 2))    ⇒ 0
(comparator-compare C '(1 2) '(1 3))    ⇒ -1
(comparator-compare C '(1 3) '(1 2))    ⇒ +1

(comparator-compare C '()    '())       ⇒ 0
(comparator-compare C '()    '(1 2))    ⇒ -1
(comparator-compare C '(1 2) '())       ⇒ +1

(comparator-compare C '(1 2 . 3) '(1 2 . 3))    ⇒ 0
(comparator-compare C '(1 2 . 3) '(1 2 . 4))    ⇒ -1
(comparator-compare C '(1 2 . 4) '(1 2 . 3))    ⇒ +1

(comparator-compare C '(1 2 9 . 3) '(1 2 9 . 3)) ⇒ 0
(comparator-compare C '(1 2 9 . 3) '(1 2 9 . 4)) ⇒ -1
(comparator-compare C '(1 2 9 . 4) '(1 2 9 . 3)) ⇒ +1

;; hash
(non-negative-exact-integer? (comparator-hash C '()))        ⇒ #t
(non-negative-exact-integer? (comparator-hash C '(1 2)))     ⇒ #t
(non-negative-exact-integer? (comparator-hash C '(1 2 . 3))) ⇒ #t
(non-negative-exact-integer? (comparator-hash C "ciao"))     ⇒ #t

Next: , Previous: , Up: srfi comparators constructors   [Index]