recursion - Recursive numeric equality in Scheme -
it seems scheme considers integer , floating point versions of number different when using equal?, same when using = compare them:
(equal? 2 2.0) ; => #f (= 2 2.0) ; => #t however, if have recursive structure numeric parts (or simple list of numbers), there method compare them uses = numeric comparisons?
(equal? '(2 3) '(2.0 3.0)) ; => #f (= '(2 3) '(2.0 3.0)) ; error: contract violation i can write own equality checker, this:
(define myequal? (lambda (x y) (cond ((and (null? x) (null? y)) #t) ((or (null? x) (null? y)) #f) ((and (pair? x) (pair? y)) (and (myequal? (car x) (car y)) (myequal? (cdr x) (cdr y)))) ((or (pair? x) (pair? y)) #f) ((and (number? x) (number? y)) (= x y)) ((or (number? x) (number? y)) #f) (else (equal? x y))))) but seems common enough task scheme might have builtin method this.
in racket can build notion of equality want of equal?/recur built-in procedure:
;; equalish? : any -> boolean ;; equal?, use = numbers (including within compound data) (define (equalish? b) (if (and (number? a) (number? b)) (= b) (equal?/recur b equalish?))) (equalish? '(2 3) '(2.0 3.0)) ;; => #t the equal?/recur procedure handles recurring through pairs, structures, etc.
Comments
Post a Comment