If I want to compare two values for equality there are a number of options, such as:
eq
for symbols=
for numberschar-equal
for charactersstring-equal
for stringseql
for symbols, numbers and stringsequal
for everything but symbols(And I hope I got this right so far.)
Now, as a Lisp beginner, my question is: Why is that? Is this just for "historical reasons" or is there a real benefit of having all these possibilities?
I know that why-questions are always hard to answer and may be opinion-based, but I think (guess) that there are best practices that are commonly agreed on by advanced Lisp developers.
There are so many because there are specialized comparison functions that are optimized for certain types. Using them might give you better performance.
This simple rule I read in Land of Lisp
eq
to compare identify the same object (pointer equal)equal
for everything that looks the sameNow sometimes you want to test if a string is equal case insensitively and then equal
wouldn't do that since "test" and "TEST" don't look the same. string-equal
would be the right choice.
When comparing different types of number, like 3.0 and 3 they don't look the same. However if you do (= 3 3.0)
it's T
since they represent the same value if the fixnum was casted down to a float.
Also equalp
is like equal
except it does string case insensitive and numbers like =
. I'm not entirely sure but in my mind a third rule would be:
equalp
for everything that means the sameSo you'll come pretty far with just those 3 generic ones but some cases you might need to search CLHS for a specialized equality operator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With