Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't CASE be used on string values and only symbol values?

In book 'land of lisp' I read

Because the case command uses eq for comparisons, it is usually used only for branching on symbol values. It cannot be used to branch on string values, among other things.

Please explain why?

like image 419
skang404 Avatar asked Sep 18 '13 17:09

skang404


1 Answers

The other two excellent answers do answer the question asked. I will try to answer the natural next question - why does case use eql?

The reason is actually the same as in C (where the corresponding switch statement uses numeric comparison): the case forms in Lisp are usually compiled to something like goto, so (case x (1 ...) (2 ...) (3 ...)) is much more efficient than the corresponding cond. This is often accomplished by compiling case to a hash table lookup which maps the value being compared to the clause directly.

That said, the next question would be - why not have a case variant with equal hash table clause lookup instead of eql? Well, this is not in the ANSI standard, but implementations can provide such extensions, e.g., ext:fcase in CLISP.

See also why eql is the default comparison.

like image 180
sds Avatar answered Sep 28 '22 20:09

sds