Why is the hyphen conventional in symbol names in LISP?

后端 未结 3 1792
深忆病人
深忆病人 2021-02-14 22:54

What\'s the reason of this recommendation? Why not keeping consistent with other programming languages which use underscore instead?

相关标签:
3条回答
  • 2021-02-14 22:55

    Just a guess: it may be because it resembles English language's compound words (like "well-known", "merry-go-round", etc). Like Paul said in comment, it's one of the oldest languages and for the creators of LISP hyphen might have seemed more natural than, for example, an underscore.

    Side note: I, personally, do like it, because it separates words, but at the same time makes the long identifier look as a whole (compare fooBarBaz, foo-bar-baz and foo_bar_baz).

    0 讨论(0)
  • 2021-02-14 23:02

    I think that LISP uses the hyphen for two reasons: "history" and "because you can".

    History

    LISP is an old language, and in the early days typing an underscore could be challenging. For example, the first terminal I used for LISP was an ASR-33 teletype. On some hosts and teletype models, the key sequence for the underscore character would be interpreted as a left-pointing arrow (the assignment operator in Smalltalk). Hyphens could be typed more reliably.

    Because You Can

    In LISP, there are no infix operators (well, few). So there is no ambiguity concerning whether x-1 means "x minus 1" or "x hyphen 1". The early pioneers liked the look of the hypen for multiword symbols (or were stuck on ASR-33s as well :).

    0 讨论(0)
  • 2021-02-14 23:04

    In written natural languages the - sign is often used as a way to make compound words. In German for example we compose german nouns just by appending them:

    Hofbräuhaus
    

    Above consist of three parts: Hof bräu haus.

    But when we write concepts in German which have foreign names as their part we write them like this:

    Mubarak-Regime
    

    In natural languages it is not common to compose words by CamelCase or Under_Score.

    The design of most Lisps was more oriented towards the linguistic tradition. The convention in some languages to use the underscore came up, because in these languages the - sign was already taken for the minus operation and the identifiers of theses were not allowed to include the - sign. The - sign is a identifier terminating character in these languages. Not in Lisp.

    Note though that one can use the underscore in Lisp identifiers, though this is rarely use in code for aesthetic reasons.

    One can also use every character in an identifier. The vertical bar encloses an arbitrary symbol:

    |this *@^! symbol is valid - why is that po_ss_ib_le?|
    
    > (defun |this *@^! symbol is valid - why is that po_ss_ib_le?| (|what? really?|)
                  (+ |what? really?| 42))
    |this *@^! symbol is valid - why is that po_ss_ib_le?|
    
    > (|this *@^! symbol is valid - why is that po_ss_ib_le?| 42)
    84
    

    Note that the backslash is an escape character in symbol names.

    0 讨论(0)
提交回复
热议问题