Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there so many ways to compare for equality?

If I want to compare two values for equality there are a number of options, such as:

  • eq for symbols
  • = for numbers
  • char-equal for characters
  • string-equal for strings
  • eql for symbols, numbers and strings
  • equal 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.

like image 798
Golo Roden Avatar asked Oct 12 '25 06:10

Golo Roden


1 Answers

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

  • Use eq to compare identify the same object (pointer equal)
  • Use equal for everything that looks the same

Now 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:

  • Use equalp for everything that means the same

So 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.

like image 106
Sylwester Avatar answered Oct 15 '25 09:10

Sylwester