Racket has more than one function for testing the equality of two items. Why? So that specialized equality functions can be optimized to run faster. But if you’re not comparing data types that are appropriate, you’ll get inaccurate results. Choose with care.
equal? gives the right answer for all data types, though it’s the slowest of the equality functions. When in doubt, use this. It diligently inspects every value, including values enclosed in two lists:
1 2 3 4
eq? is mostly restricted to symbols and objects. eq? can be much faster than equal? because it doesn’t actually inspect the values of the two items. Rather, it simply checks if the items refer to the same chunk of memory. (Those familiar with memory operations might know this as .)
Its reliance on pointer comparison, however, is why eq? is limited to and a handful of other data types that are . Conversely, it’s also why you’ll see symbols chosen for situations where speedy comparisons are needed, because then you can use eq?.
Take extra care with eq?, which can lead newcomers astray with its ability to give seemingly accurate results in simple test cases (like numbers and strings), then seemingly fail when the input gets more complex. In those cases, eq? is right; your expectations are wrong.
1 2 3 4
eqv? is like eq?, but handles numbers and characters consistently.
= is like eqv?, but limited to numbers, and can be faster than equal?.