19

Here is some doco I'm creating but...

I'm not sure what the dot '.' between the extension and the mode is for though in the following:


File Associations

Example: Associate *.mmd with markdown-mode:

      (setq auto-mode-alist (cons '("\\.mmd$" . markdown-mode) auto-mode-alist))

basically there is an alist (associative list / hashtable) called auto-mode-alist. That points extension -> to mode. Extension looks like it's a regular expression.

cons is a function that pre-pends an element to a list

setq means set quoted (which quotes auto-mode-list for you), otherwise instead of assigning to the symbol auto-mode-alist, you will assign to the results of evaluating that symbol...not what you want ;)

Matt Curtis
  • 23,168
  • 8
  • 60
  • 63
ftravers
  • 3,809
  • 3
  • 37
  • 38

4 Answers4

22

Lists are built up from smaller pieces in Lisp. The dot notation indicates those smaller pieces.

(a b c d e)                       ; a normal list
(a . (b . (c . (d . (e . nil))))) ; the same list in primitive form

An item of the form (a . b) syntax is called a cons cell; a is the car and b is the cdr. (Those last terms come from the register names used to store them on the minicomputer Lisp was originally developed on, and are not otherwise meaningful. cons is short for "construct".) cons cell s are created with the cons function. Notice that the behavior for prepending to a list falls naturally out of the internal format of a list, as shown above, and that appending to a list with cons will not do what one might naïvely expect.

Alist s are by historical convention plain cons cell s instead of full lists, originally for speed.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • 1
    **Alist**:s are desinged to map one kind of element to another. They are not an historic artifact, and they have nothing to do with speed. – Lindydancer Mar 16 '11 at 10:24
  • 4
    @Lindydancer: Yes, but alists would work just as well with proper lists instead of dotted pairs, because the car of a list would still be the key of the association. Therefore, dotted pairs for alists can arguably be called an optimisation to save the space of an additional nil to end the list and the time accessing the value with cadr instead of cdr would take. – Rörd Mar 16 '11 at 13:35
14

In lisp, on the low level, you can either have an simple value (a number or an atom) or a dotted pair, or cons cells. (Let's ignore modern stuff like vectors...). On top of these, data structures like lists (a b c) are constructed.

An alist or associative list, is a plain list where each element itself (a, b et.c.) is a dotted pair. For example:

((foo . 10) (bar . 20))

You can search an alist using assq or assoc, a pair is returned. You can apply car and cdr to get the left or right part of the pair, respectively.

A plain list is cleverly constructed the above so that (a b c) is represented as (a . (b . (c . nil))) Typically, one does not have to know this to understand alist:s, but the internal printing mechanism can sometimes get thrown of. Concretely, ((foo . nil) (bar . 20)) is typically printed as ((foo) (bar . 20)).

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
13

Its the dotted pair notation. See this link for further reference.

quazgar
  • 4,304
  • 2
  • 29
  • 41
Sujoy
  • 8,041
  • 3
  • 30
  • 36
2

This is also a nice description, the essence:

A dotted list is one whose last cons does not have nil for its cdr, rather some other data object (which is also not a cons, or the first-mentioned cons would not be the last cons of the list).

This allows for example to specify a list whose last cons is a symbol, and this symbol then can point to another list, change at runtime, etc.

jhegedus
  • 20,244
  • 16
  • 99
  • 167