recursionschemeracketequalityr5rs# 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.

Solution

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.

- python recursion i dont understand this output pls help me
- I'm trying to understand recursion in Tcl, but every time the recursion finishes it throws errors
- How to remove all numbers from a list in Lisp
- Calculate the total weight of segments
- Why does the expression !(cin>>word) cause infinite recursion?
- What's the best way to recursively traverse a BinaryTree in Java without void methods?
- can anybody explains the code and also recursion tree
- Is it possible to limit the depth of a recursive directory listing in S3 bucket?
- takeWhile implementation in JavaScript - Looking for better ideas
- I used a recursive function to modify a string, but if the string is too large the function returns nothing
- How to Compress Paths in SQL Server Using Recursive CTE with Node Visibility Conditions?
- Why this recursion example in ocaml doesn't work for negative number?
- Recursive loop on a list to print xml with indent
- Big O of algorithm that steps over array recursively
- Powerset of a list with equal elements in Java
- Recursion in FP-Growth Algorithm
- Passing list to function results in no case clause matching
- Convert tree-like structure formatted as a string using indentations to a list of paths
- Recursive function fails depending on lexical scoping
- Creating a list of interleaved elements: Prolog
- How to get the recursive difference formula between two columns in excel
- Get all values and associative keys as a flat array from a multidimensional array of variable depth/structure
- How can I make a recursive square root in Python?
- Issue with Memoization in Recursive Function for Finding Combinations Summing to Target
- Finding a continuous route through a list of lists
- How to get all possible combinations of elements in a given list?
- How can I get the nested keys of a map in clojure?
- Computing Checkmate Correctly
- Node.js recursively list full path of files
- C# Binary Search of a number in an array using recursion