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:
- comparator-maxprocedure
- Applies the comparator procedure to given objects, returns the maximal object. Order of comparison is unspecified.
- comparator-minprocedure
- As above, except returns the minimal object.
- comparator-max-in-listprocedure
- comparator-min-in-listprocedure
- As above except accepting a single list argument, returning max or min object in the given list.
- default-comparatorrecord
- functions identically to a comparator generated by make-default-comparator from srfi-128.
- boolean-comparatorrecord
- #f compares before #t
- real-comparatorrecord
- for real numbers, smaller numbers compare before larger numbers
- char-comparatorrecord
- compares characters in Unicode codepoint order
- char-ci-comparatorrecord
- case-insensitive comparison of characters
- string-comparatorrecord
- compares strings using implementation's definition of string<?
- string-ci-comparatorrecord
- case-insensitive comparison of strings using string-ci<?
- pair-comparatorrecord
- compares pairs by applying make-pair-comparator to pairs with default-comparator for comparing car and cdr of the pairs
- list-comparatorrecord
- comparator for lists using make-list-comparator with default-comparator to compare list elements
- vector-comparatorrecord
- comparator for vectors using make-vector-comparator with default-comparator to compare vector elements
- eq/eqv/equal-comparatorrecord
- These comparators behave in the same manner as respective calls to make-eq-comparator, make-eqv-comparator, make-equal-comparator
srfi-128 API
- comparator?procedure
- returns #t if obj is a comparator or #f if not
- comparator-ordered?procedure
- returns #t if comparator has an ordering predicate, otherwise #f
- comparator-hashable?procedure
- returns #t if comparator has a hash function, otherwise #f
- make-comparatorprocedure
- 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.
- make-pair-comparatorprocedure
- 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.
- make-list-comparatorprocedure
- 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.
- make-vector-comparatorprocedure
- 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.
- make-default-comparatorprocedure
- 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.
- default-hashprocedure
- 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
- comparator-type-test-predicateprocedure
- comparator-equality-predicateprocedure
- comparator-ordering-predicateprocedure
- comparator-hash-functionprocedure
- These procedures return the 4 procedures of the comparator.
- comparator-test-typeprocedure
- comparator-check-typeprocedure
- 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.
- comparator-hashprocedure
- Invokes comparator's hash function on obj.
Comparison predicates
- =?procedure
- <?procedure
- >?procedure
- <=?procedure
- >=?procedure
- These procedures apply the equality and ordering predicates of comparator to objects. If all object comparisons return #t, result is #t, otherwise #f.
- comparator-if<=> [ <comparator> ] <object1> <object2> <less-than> <equal-to> <greater-than>syntax
- 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