Search code examples
common-lispcase-sensitivecase-insensitive

Why is Common Lisp case insensitive?


Is there an advantage to defining a function like (defun hi () "Hi!") and be able to call it by using (hi) or (HI) or (Hi), or to (setf a-number 5) and be able to access that number using a-number, A-NUMBER, or A-Number?

If there is such an advantage, then why are most other languages case-sensitive?


Solution

  • Using case sensitive names in code within an interactive session is just more error-prone: one not only has to get the characters, but also their case right and there might be several identifiers whose names only differ in case.

    Common Lisp is case sensitive. It is just that the Common Lisp reader functionality by default converts all unescaped characters of symbols to uppercase. This is also defined in the Common Lisp standard. The predefined Common Lisp symbols are also all uppercase internally.

    a symbol is internally upcase:

    CL-USER 3 > 'defparameter
    DEFPARAMETER
    

    The input case by default does not matter:

    CL-USER 4 > 'deFParameTER
    DEFPARAMETER
    
    CL-USER 5 > 'DEFPARAMETER
    DEFPARAMETER
    

    a symbol has an upcase name:

    CL-USER 6 > (symbol-name 'defparameter)
    "DEFPARAMETER"
    

    how many symbols are in the CL package:

    CL-USER 7 > (length (apropos-list "" "CL"))
    978
    

    Is every symbol in the CL package upcased?

    CL-USER 8 > (every (lambda (symbol)
                         (every (lambda (c)
                                  (eql c (char-upcase c)))
                                (symbol-name symbol)))
                       (apropos-list "" "CL"))
    T
    

    Using uppercase was common on old machines. Remember, the design of Common Lisp started in the early eighties (1982) and a goal was compatibility with earlier Maclisp and when there were more types of computers to support (like the so-called Mini Computers and Mainframes). Other programming languages used on older computers also use uppercase identifiers, like COBOL or PL/1.

    Also note that Lisp often was used interactively, so that during an interactive programming session getting the case of names right is more difficult. It is slightly easier when the Lisp reader uses a default case (here uppercase) and converts all input to this case.

    Common Lisp supports other reader modes and you can also escape symbols: |This is a Symbol with mixed CASE and spaces|.

    Today a lot of software is either lowercase or even case sensitive with lowercase preferred. Some Lisp vendors provide a non-standard variant of Common Lisp, where all symbols by default are lowercase and the reader is case preserving. But this makes it incompatible with standard Common Lisp, where the expectation is that (symbol-name 'cl:defun) is "DEFUN" and not "defun".