Changeset 28031 in project


Ignore:
Timestamp:
01/04/13 05:26:27 (8 years ago)
Author:
svnwiki
Message:

Anonymous wiki edit for IP [74.68.121.27]: Chickenizing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/genequal

    r28022 r28031  
    1 == Generalized {{equal?}} predicate
     1== genequal
    22
    3 {{(generalized-equal? }}''obj1 obj2''{{ . }}''comparator-list''{{)}}
     3[[toc:]]
    44
    5 Compares ''obj1'' and ''obj2'' for equality using user-specified and built-in comparators.  A ''comparator'' is a procedure that is given two arguments to compare.  It returns {{#t}} if its arguments are to be considered equal, {{#f}} if they are to be considered unequal, and any other value if it cannot decide.  The third argument passed to a comparator is a list of comparators to be used in recursive calls to {{generalized-equal?}}.
     5=== Description
     6
     7Compares ''obj1'' and ''obj2'' for equality using user-specified and built-in comparators.
     8
     9
     10=== Author
     11
     12[[/users/john-cowan|John Cowan]]
     13
     14
     15=== Requirements
     16
     17None
     18
     19=== Generalized {{equal?}} predicate
     20
     21<procedure>(generalized-equal? obj1 obj2 . comparator-list)</procedure>
     22
     23A ''comparator'' is a procedure that is given two arguments to compare.  It returns {{#t}} if its arguments are to be considered equal, {{#f}} if they are to be considered unequal, and any other value if it cannot decide.  The third argument passed to a comparator is a list of comparators to be used in recursive calls to {{generalized-equal?}}.
    624
    725First, each element of ''comparator-list''  is invoked on ''obj1'' and ''obj2'', passing ''comparator-list'' as its third argument.  If the comparator returns {{#t}} or {{#f}}, that is the result.
     
    1129Otherwise, if the containers have different numbers of elements, the result is {{#f}}.  Otherwise, {{generalized-equal?}} invokes itself recursively on each corresponding element of the containers, passing itself the same comparators.  If a recursive call returns {{#f}}, that is the result; if all recursive calls return {{#t}}, that is the result.
    1230
    13 {{(predicates->comparator }}''type-predicate compare-predicate''{{)}}
     31<procedure>(predicates->comparator  type-predicate compare-predicate)</procedure>
    1432
    1533Returns a comparator that invokes ''type-predicate'' on its first and its second arguments.  If they both return {{#t}}, then they are assumed to be of the same type, and ''compare-predicate'' is invoked on the first and second arguments together.  If the result is {{#t}} or {{#f}}, then the comparator returns {{#t}} or {{#f}} respectively.  If they are not of the same type, a third value is returned.  The comparator always ignores its third argument.
    1634
    17 == Comparators
     35=== Comparators
    1836
    1937Specifying all three of these comparators causes {{generalized-equal?}} to act like Common Lisp's {{EQUALP}}.
    2038
    21 {{(numeric-comparator }}''obj1 obj2 comparators-list''{{)}}
     39<procedure>(numeric-comparator  obj1 obj2 comparators-list)</procedure>
    2240
    2341A comparator that returns {{#t}} if ''obj1'' and ''obj2'' are numbers that are equal by {{=}}, {{#f}} if they are not equal by {{=}}, and a third value otherwise.  The ''comparators-list'' argument is ignored.
    2442
    25 {{(char-ci-comparator }}''obj1'' ''obj2 comparators-list''{{)}}
     43<procedure>(char-ci-comparator  obj1 obj2 comparators-list)</procedure>
    2644
    2745A comparator that returns {{#t}} if ''obj1'' and ''obj2'' are both characters that are equal by {{char-ci=?}}, {{#f}} if they are not equal by {{char-ci=?}}, and a third value otherwise.  The ''comparators-list'' argument is ignored.
    2846
    29 {{(string-ci-comparator }}''obj1 obj2 comparators-list''{{)}}
     47<procedure>(string-ci-comparator  obj1 obj2 comparators-list)</procedure>
    3048
    3149A comparator that returns {{#t}} if ''obj1'' and ''obj2'' are both strings that are equal by {{string-ci=?}}, {{#f}} if they are not equal by {{string-ci=?}}, and a third value otherwise.  The ''comparators-list'' argument is ignored.
     50
     51=== Examples
     52
     53<enscript highlight=scheme>
     54(use genequal)
     55
     56(use srfi-99)
     57(define foo (make-foo x) foo? (x foo-x foo-set-x!))
     58(define bar (make-bar x) bar? (x bar-x))
     59(define a (make-foo 10))
     60(define b (make-foo 10))
     61(define c (make-bar 10))
     62
     63(generalized-equal? a b) => #t
     64(generalized-equal? a c) => #f
     65(foo-set-x! a 20)
     66(generalized-equal? a b) => #f
     67
     68(generalized-equal? ("A" "B") '("a" "b")) => #f
     69
     70(generalized-equal? '("A" "B") '("a" "b") string-ci-comparator) => #t
     71
     72</enscript>
     73=== License
     74
     75BSD
     76
     77
     78=== Version history
     79
     80==== Version 0.1
     81
     82Initial release
Note: See TracChangeset for help on using the changeset viewer.