Changeset 28031 in project
 Timestamp:
 01/04/13 05:26:27 (8 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

wiki/eggref/4/genequal
r28022 r28031 1 == Generalized {{equal?}} predicate1 == genequal 2 2 3 {{(generalizedequal? }}''obj1 obj2''{{ . }}''comparatorlist''{{)}} 3 [[toc:]] 4 4 5 Compares ''obj1'' and ''obj2'' for equality using userspecified and builtin 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 {{generalizedequal?}}. 5 === Description 6 7 Compares ''obj1'' and ''obj2'' for equality using userspecified and builtin comparators. 8 9 10 === Author 11 12 [[/users/johncowanJohn Cowan]] 13 14 15 === Requirements 16 17 None 18 19 === Generalized {{equal?}} predicate 20 21 <procedure>(generalizedequal? obj1 obj2 . comparatorlist)</procedure> 22 23 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 {{generalizedequal?}}. 6 24 7 25 First, each element of ''comparatorlist'' is invoked on ''obj1'' and ''obj2'', passing ''comparatorlist'' as its third argument. If the comparator returns {{#t}} or {{#f}}, that is the result. … … 11 29 Otherwise, if the containers have different numbers of elements, the result is {{#f}}. Otherwise, {{generalizedequal?}} 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. 12 30 13 {{(predicates>comparator }}''typepredicate comparepredicate''{{)}} 31 <procedure>(predicates>comparator typepredicate comparepredicate)</procedure> 14 32 15 33 Returns a comparator that invokes ''typepredicate'' on its first and its second arguments. If they both return {{#t}}, then they are assumed to be of the same type, and ''comparepredicate'' 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. 16 34 17 == Comparators35 === Comparators 18 36 19 37 Specifying all three of these comparators causes {{generalizedequal?}} to act like Common Lisp's {{EQUALP}}. 20 38 21 {{(numericcomparator }}''obj1 obj2 comparatorslist''{{)}} 39 <procedure>(numericcomparator obj1 obj2 comparatorslist)</procedure> 22 40 23 41 A 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 ''comparatorslist'' argument is ignored. 24 42 25 {{(charcicomparator }}''obj1'' ''obj2 comparatorslist''{{)}} 43 <procedure>(charcicomparator obj1 obj2 comparatorslist)</procedure> 26 44 27 45 A comparator that returns {{#t}} if ''obj1'' and ''obj2'' are both characters that are equal by {{charci=?}}, {{#f}} if they are not equal by {{charci=?}}, and a third value otherwise. The ''comparatorslist'' argument is ignored. 28 46 29 {{(stringcicomparator }}''obj1 obj2 comparatorslist''{{)}} 47 <procedure>(stringcicomparator obj1 obj2 comparatorslist)</procedure> 30 48 31 49 A comparator that returns {{#t}} if ''obj1'' and ''obj2'' are both strings that are equal by {{stringci=?}}, {{#f}} if they are not equal by {{stringci=?}}, and a third value otherwise. The ''comparatorslist'' argument is ignored. 50 51 === Examples 52 53 <enscript highlight=scheme> 54 (use genequal) 55 56 (use srfi99) 57 (define foo (makefoo x) foo? (x foox foosetx!)) 58 (define bar (makebar x) bar? (x barx)) 59 (define a (makefoo 10)) 60 (define b (makefoo 10)) 61 (define c (makebar 10)) 62 63 (generalizedequal? a b) => #t 64 (generalizedequal? a c) => #f 65 (foosetx! a 20) 66 (generalizedequal? a b) => #f 67 68 (generalizedequal? ("A" "B") '("a" "b")) => #f 69 70 (generalizedequal? '("A" "B") '("a" "b") stringcicomparator) => #t 71 72 </enscript> 73 === License 74 75 BSD 76 77 78 === Version history 79 80 ==== Version 0.1 81 82 Initial release
Note: See TracChangeset
for help on using the changeset viewer.