Comparing Records
The following code can be used to check records for equality. Two records are considered equal if they have the same record type and if each of their fields are equal. It
should do the right thing if record fields contain other records.
This might or might not be what you want. Barely tested.
;;; Comparing records
;; Emilio Lopes <eclig@gmx.net>, 2006-04-11
;; THIS FILE IS IN THE PUBLIC DOMAIN. USE AT YOUR OWN RISK!
(define-structure record-compare
(export same-record?
records-equal?)
(open records
records-internal
srfi-1
scheme)
(begin
" For the sake of Emacs' indentation
("
(define (same-record? r1 r2 pred)
(let ((type (record-type r1)))
(and (eq? type (record-type r2))
(every (lambda (field)
(let ((accessor (record-accessor type field)))
(let ((f1 (accessor r1))
(f2 (accessor r2)))
(if (and (record? f1) (record? f2))
(same-record? f1 f2 pred)
(pred f1 f2)))))
(record-type-field-names type)))))
(define (records-equal? r1 r2)
(same-record? r1 r2 equal?))
))
ComparingRecords - raw wiki source |
code snippets archive
|