tlauer
tlauer

Reputation: 588

Comparing names with equals

How do I compare names such as this case:

(if (= name '(bill)) (write-line '(over)))

?

Upvotes: 1

Views: 159

Answers (2)

Óscar López
Óscar López

Reputation: 236004

The procedure used for testing equality depends on the type of the operands to be compared. In particular, the = procedure is used for comparing between numbers:

(= 1 1)

But that doesn't seem to be the case. If name is a symbol:

(symbol=? name 'bill)

If name is a string:

(string=? name "bill")

If name is a single character:

(char=? name #\b)

If name is in a list:

(member name '(bill))

Finally, if you're not sure of the type of name, you can always use equal?:

(equal? name "bill")

Upvotes: 2

jacobm
jacobm

Reputation: 14025

First of all, it's unlikely that you'd want the name to be '(bill), which is a list consisting of the single symbol 'bill. You probably just want 'bill directly. Second, you can't use = for a comparison of symbols: = is for numeric comparisons. symbol=? is probably what you want:

(if (symbol=? name 'bill) ...)

(Note that there are some other equality functions that will work here too, and you may well see other people use eq? or equal? in place of symbol=? here.)

Upvotes: 1

Related Questions