Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "eq?" evaluate to false in the following context but true otherwise?

I am currently practicing the racket language and I ran into an interesting issue. I am trying to compare the elements of two lists. Normally if I compare two symbols, I'd get the following:

> (eq? 'leet 'leet)
#t
> (eq? 'let 'notleet)
#f

For some reason, when comparing the first element of two lists, I get false even though they're equal.

> (eq? (first '('leet 'a 'f)) (first '('leet 'coder 'a 'f 'f)))
#f

Why would that evaluate to false when I'm basically comparing the same two things?

like image 334
msafadieh Avatar asked Dec 02 '25 00:12

msafadieh


1 Answers

Sylwester's answer is correct and detailed, but I want to bring out the TL/DR; here:

Don't use eq?. Instead, use equal?.

Is this the whole story? No, of course not. But if you're looking for a one-liner to stick in your brain, it should be this one; equal? nearly always does what you want, and eq? often doesn't.

like image 117
John Clements Avatar answered Dec 04 '25 12:12

John Clements



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!