You are looking at historical revision 45085 of this page. It may differ significantly from its current revision.
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.
- As above, except returns the minimal object.
[procedure] comparator-min-in-list comparator list
- As above except accepting a single list argument, returning max or min object in the given list.
- functions identically to a comparator generated by make-default-comparator from srfi-128.
- #f compares before #t
- for real numbers, smaller numbers compare before larger numbers
- compares characters in Unicode codepoint order
- case-insensitive comparison of characters
- compares strings using implementation's definition of string<?
- case-insensitive comparison of strings using string-ci<?
- compares pairs by applying make-pair-comparator to pairs with default-comparator for comparing car and cdr of the pairs
- comparator for lists using make-list-comparator with default-comparator to compare list elements
- comparator for vectors using make-vector-comparator with default-comparator to compare vector elements
- 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
- returns #t if comparator has an ordering predicate, otherwise #f
- returns #t if comparator has a hash function, otherwise #f
- 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.
- 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.
- 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.
- 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.
- 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->string and string<?).
- 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.
- 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
[procedure] comparator-type-test-predicate comparator[procedure] comparator-equality-predicate comparator
[procedure] comparator-ordering-predicate comparator
[procedure] comparator-hash-function comparator
- These procedures return the 4 procedures of the comparator.
[procedure] comparator-check-type comparator obj
- 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-predicate but more convenient.
- Invokes comparator's hash function on obj.
Comparison predicates
[procedure] =? comparator object1 object2 object3 ...[procedure] <? comparator object1 object2 object3 ...
[procedure] >? comparator object1 object2 object3 ...
[procedure] <=? comparator object1 object2 object3 ...
[procedure] >=? comparator object1 object2 object3 ...
- These procedures apply the equality and ordering predicates of comparator to objects. If all object comparisons return #t, result is #t, otherwise #f.
- 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")) ;; -> #f
Version
- version 0.1 November 28, 2025
License
- BSD