简体   繁体   中英

Recursive numeric equality in Scheme

It seems that Scheme considers integer and floating point versions of a number to be different when using equal?, but the same when using = to compare them:

(equal? 2 2.0)  ; => #f
(= 2 2.0)       ; => #t

However, if I have a recursive structure with some numeric parts (or even a simple list of numbers), is there a method to compare them that uses = for numeric comparisons?

(equal? '(2 3) '(2.0 3.0))  ; => #f
(= '(2 3) '(2.0 3.0))       ; error: contract violation

I can write my own equality checker, something like 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 it seems like this would be a common enough task that Scheme might have a builtin method to do this.

In Racket you can build the notion of equality that you want with the help of the equal?/recur built-in procedure:

;; equalish? : Any Any -> Boolean
;; Like equal?, but use = for numbers (including within compound data)
(define (equalish? a b)
  (if (and (number? a) (number? b))
      (= a b)
      (equal?/recur a b equalish?)))

(equalish? '(2 3) '(2.0 3.0))
;; => #t

The equal?/recur procedure handles recurring through pairs, structures, etc.

Scheme is a minimalistic language and have very few primitives. 2 and 2.0 are not the same number because 2.0 can be lower and higher than 2 which is the exact amount 2 .

If you have a list with numbers and wish to check if all are the same with = you can do it using every from SRFI-1 List Library :

;; include the library. In R5RS this is impleentation specific
;; and worst case you need to load of external file to be portable.
(load "srfi1.scm") 

(every = '(2 3) '(2.0 3.0)) ; ==> #t

In R6RS it gets simpler:

#!r6rs

(import (rnrs base)
        (only (srfi :1) every))

(every = '(2 3) '(2.0 3.0)) ; ==> #t

And since you have tagged Racket there is a chance that you might not write Scheme but perhaps #lang racket which has both support for SRFI-1 and its own version of every that is called andmap :

#lang racket

(andmap = '(2 3) '(2.0 3.0)) ; ==> #t

(require srfi/1)
(every = '(2 3) '(2.0 3.0)) ; ==> #t

EDIT

A generic solution for all tree structures that use itself for tree structure and equal? when there are no more type specific options:

(define (make-equal . predicates-and-equal-procedures)
  (when (odd? (length predicates-and-equal-procedures))
    (error "make-equal needs an even number of predicate and equal function arguments"))

  (define (mequal? a b)
    (if (pair? a)
        (and (pair? b)
             (mequal? (car a) (car b))
             (mequal? (cdr a) (cdr b)))
        (let loop ((pe predicates-and-equal-procedures))
          (if (null? pe)
              (equal? a b)
              (let ((p? (car pe)))
                (if (p? a)
                    (and (p? b)
                         ((cadr pe) a b))
                    (loop (cddr pe))))))))
  mequal?)

(define list=?
  (make-equal number? =))

(list=? '(1 2 a b "test") '(1.0 2 a b "test")) ; ==> #t

(define equal-ci?
  (make-equal string? string-ci=? char? char-ci=?))

(equal-ci? '(1 2 a b "Test") '(1 2 a b "test")) ; ==> #t

(define inexact-eq-ci?
  (make-equal number? = string? string-ci=? char? char-ci=?))

(inexact-eq-ci? '(1 2 a b "test") '(1.0 2 a b "TEST")) ; ==> #t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM