Why is the hyphen conventional in symbol names in LISP?

*爱你&永不变心* 提交于 2019-12-09 10:20:09

问题


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


回答1:


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 :).




回答2:


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).




回答3:


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.



来源:https://stackoverflow.com/questions/4900342/why-is-the-hyphen-conventional-in-symbol-names-in-lisp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!