srfi-162
Description
srfi-162 is a set of procedures and comparators supplementing srfi-128. This extension provides srfi-162 modified for Chicken 6. Since srfi-162 contains the complete implementation of srfi-128, srfi-162 effectively replaces srfi-128.
Author
- srfi-162 was authored by John Cowan
- Modified for Chicken 6 by Jules Altfas
Repository
- https://codeberg.org/jrapdx/srfi-162
Requirements
None.
srfi-162 API
srfi-162 provides these procedures and pre-built comparators:
[procedure]
comparator-max comparator obj1 obj2 ...- Applies the comparator procedure to given objects, returns the maximal object. Order of comparison is unspecified.
[procedure]
comparator-min comparator obj1 obj2 ...- As above, except returns the minimal object.
- As above except accepting a single list argument, returning max or min object in the given list.
[record]
default-comparator- functions identically to a comparator generated by
make-default-comparatorfrom srfi-128.
[record]
boolean-comparator- #f compares before #t
[record]
real-comparator- for real numbers, smaller numbers compare before larger numbers
[record]
char-comparator- compares characters in Unicode codepoint order
[record]
char-ci-comparator- case-insensitive comparison of characters
[record]
string-comparator- compares strings using implementation's definition of
string<?
[record]
string-ci-comparator- case-insensitive comparison of strings using
string-ci<?
[record]
pair-comparator- compares pairs by applying
make-pair-comparatorto pairs withdefault-comparatorfor comparingcarandcdrof the pairs
[record]
list-comparator- comparator for lists using
make-list-comparatorwithdefault-comparatorto compare list elements
[record]
vector-comparator- comparator for vectors using
make-vector-comparatorwithdefault-comparatorto compare vector elements
[record]
eq/eqv/equal-comparator- These comparators behave in the same manner as respective calls to
make-eq-comparator,make-eqv-comparator,make-equal-comparator
srfi-128 API
[procedure]
comparator? obj- returns #t if obj is a comparator or #f if not
[procedure]
comparator-ordered? comparator- returns #t if comparator has an ordering predicate, otherwise #f
[procedure]
comparator-hashable? comparator- returns #t if comparator has a hash function, otherwise #f
[procedure]
make-comparator type-test equality ordering hash- type-test: predicate that returns #t if argument is correct type, otherwise #f
- equality: predicate returns #t if objects are the same, otherwise #f
- ordering: predicate if first object precedes second object presented to the comparator.
- hash: a procedure taking an object and returning an exact integer.
[procedure]
make-pair-comparator car-comparator cdr-comparator- type-test returns #t if argument is a pair, #f otherwise.
- equality predicate returns #t if cars are equal per car-comparator and cdrs are equal per cdr-comparator.
- ordering: cars are compared by car-comparator, if not equal, cdrs are compared and if not equal, ordering predicate is applied.
- hash function computes hash of car and cdr, and the hashes are hashed together.
[procedure]
make-list-comparator element-comparator type-test empty? head tail- type-test returns #t when type-test is #t and list elements satisfy element-comparator.
- an empty sequence (per empty?) compares less than non-empty.
- if head elements are compared and if equal, tail elements are compared to determine order of input objects.
- hash function of element-comparator computes hash of elements which are hashed together.
[procedure]
make-vector-comparator element-comparator type-test length ref- type-test returns #t when type-test is #t and elements satisfy element-comparator.
- equality-predicate is #t when lengths of vectors are equal, and elements are equal according to element-comparator.
- ordering-predicate is #t when first vector length is less than second, or if equal, element-wise comparison is made.
- hash function of element-comparator computes hash of elements when are hashed together.
[procedure]
make-default-comparator- Comparing objects of disjoint types, all elements of one type compare less or greater than a second type.
- The empty list is ordered before any pairs.
- Comparing booleans, #f<#t.
- Comparing characters must use char=? and char<?, order is Unicode codepoint order.
- Comparing pairs must work the same as
make-pair-comparator. - Comparing symbols uses an implementation-dependent total order. (Possibly applying
symbol->stringandstring<?). - Comparing bytevectors, comparator should conform to
make-vector-comparator (make-comparator exact-integer? = < number-hash) bytevector? bytevector-length bytevector-u8-ref. - Comparing complex numbers, real parts are compared first, and if equal, order is determined by comparing imaginary parts.
- Real numbers are compared with = and <.
- Comparing strings uses string=? and string<?.
- Vector comparison should behave same as
make-vector-comparator (make-default-comparator) vector? vector-length vector-ref. - For comparing types registered with
comparator-register-default!, default comparator must work like the registered comparator. - Hashing is accomplished with
default-hash.
[procedure]
default-hash obj- Applied to a pair, this procedure must return result of hashing together results of hashing car and cdr of the pair.
- Applied to boolean, character, string, symbol and number values, result must be the same as produced by boolean-hash, char-hash, string-hash, symbol-hash, or number-hash.
- Applied to list or vector types, result is the result of hashing together the hash of each element.
Accessors
- These procedures return the 4 procedures of the comparator.
- These procedures invoke the comparator's type-test predicate on obj. The latter signals an error if not #t. Less efficient than
comparator-type-test-predicatebut more convenient.
[procedure]
comparator-hash comparator obj- Invokes comparator's hash function on obj.
Comparison predicates
- These procedures apply the equality and ordering predicates of comparator to objects. If all object comparisons return #t, result is #t, otherwise #f.
[syntax]
comparator-if<=> [ <comparator> ] <object1> <object2> <less-than> <equal-to> <greater-than>- If comparator ordering predicate returns #t, then
<less-than>is evaluated. If the equality predicate is #t, then<equal-to>is evaluated, otherwise<greater-than>is evaluated and result returned.
Documentation
For more information please confer the srfi documents:
Examples
(import srfi-162)
(=? list-comparator '(a b c) '(a b c)) ;; -> #t
(=? list-comparator '(a b c) '(a b c d)) ;; -> #f
(=? list-comparator '(11 22 33) '(11 22 33)) ;; -> #t
(=? list-comparator '(11 22 33) '(11 22 333)) ;; -> #f
(=? list-comparator '(11 22 33) '(11 22 zz)) ;; -> #f
(=? list-comparator '("abc" "cde") '("abc" "cde")) ;;-> #t
(=? list-comparator '("abc" "cde") '("abc" "cdE")) ;;-> #f
(>? list-comparator '("abc" "cde") '("abc" "cdE")) ;;-> #t
;; make comparator for case-insensitive comparisons of string lists
(define list-ci-comparator (make-list-comparator string-ci-comparator
list? null? car cdr))
(=? list-ci-comparator '("abc" "cde") '("abc" "cde")) ;; -> #t
(=? list-ci-comparator '("abc" "cde") '("abC" "cdE")) ;; -> #t
(<? list-ci-comparator '("abc" "cde") '("abC" "cdE")) ;; -> #f
(>? list-ci-comparator '("abc" "cde") '("abC" "cdE")) ;; -> #f
(=? list-ci-comparator '("abc" "cde") '("abC" "cdEf")) ;; -> #fVersion
- version 0.1 November 28, 2025
License
- BSD