Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the hyphen conventional in symbol names in LISP?

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

like image 410
Thomson Avatar asked Feb 04 '11 16:02

Thomson


Video Answer


2 Answers

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.

like image 99
Rainer Joswig Avatar answered Dec 02 '22 19:12

Rainer Joswig


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

like image 43
WReach Avatar answered Dec 02 '22 19:12

WReach