Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does single quote in Lisp always return upper case?

I'd like to be able to set case from a single quote, but that does not seem possible.

(format nil "The value is: ~a" 'foo)
"The value is: FOO"

(format nil "The value is: ~a" 'FOO)
"The value is: FOO"

(format nil "The value is: ~a" "Foo")
"The value is: Foo"
like image 210
Jiminion Avatar asked Aug 11 '15 13:08

Jiminion


2 Answers

Quoting

The quote has nothing to do with case. A quote prevents evaluation.

quoting a symbol:

CL-USER 1 > 'foo
FOO

quoting a list:

CL-USER 2 > '(1 2 3 foo)
(1 2 3 FOO)

You can put a quote in front of many things. For example in front of a string:

CL-USER 3 > '"a b c"
"a b c"

Since strings evaluate to themselves, quoting them or not makes no difference:

CL-USER 4 > "a b c"
"a b c"

Symbols are by default read as uppercase:

CL-USER 5 > 'FooBar
FOOBAR

CL-USER 6 > (symbol-name 'FooBar)
"FOOBAR"

But that has nothing to do with quoting and is a feature of the reader.

CL-USER 7 > (read-from-string "foo")
FOO
3

Downcase

If you want the string in lowercase, you need to convert the string to lowercase:

CL-USER 8 > (string-downcase (symbol-name 'FooBar))
"foobar"

Symbols with mixed case

But you can create symbols with lowercase names or mixed case. You need to escape them:

CL-USER 9 > '|This is a symbol With spaces and mixed case|
|This is a symbol With spaces and mixed case|

CL-USER 10 > 'F\o\oB\a\r
|FooBar|

Downcasing output using FORMAT

You can also tell FORMAT to print in lowercase:

CL-USER 11 > (format nil "The value is: ~(~a~)" 'foo) 
"The value is: foo"
like image 89
Rainer Joswig Avatar answered Sep 22 '22 17:09

Rainer Joswig


'foo means "suppress the evaluation of the symbol FOO, leaving only the symbol FOO". Common Lisp tends towards upcasing symbol names by default (so the symbols expressed as 'foo, 'Foo and 'FOO are all the same symbol, with the symbol name "FOO").

To see exactly what your implementation will do, you can check the readtable case of the current readtable see CLHS, ch 23.1.2, effect of readtable case by calling (readtabe-case *readtable*).

Some lisp implementations will start with the readtable-case as :preserve.

As for if you should use symbols or strings, it's one of those "it depends". If you're not worried about cse preservation, using interned symbols gives you less storage and quicker comparison, at the (possible) price of case-mangling. But if case is important, the balance is probably further towards the "use strings throughout" end of the scale.

like image 41
Vatine Avatar answered Sep 22 '22 17:09

Vatine