Search code examples
formatcommon-lispsingle-quotes

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"

Solution

  • 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"