Golo Roden
Golo Roden

Reputation: 150624

Quoting surrounds its output with pipes - why?

I have created a table daily-planet as follows:

(setf daily-planet '((olsen jimmy 123-76-4535 cub-reporter)
                     (kent  clark 089-52-6787 reporter)
                     (lane  lois  951-26-1438 reporter)
                     (white perry 355-16-7439 editor)))

Now I want to extract the social security numbers by running mapcar as follows:

(mapcar #'caddr daily-planet)

The result is:

(|123-76-4535| |089-52-6787| |951-26-1438| |355-16-7439|)

I would have expected:

(123-76-4535 089-52-6787 951-26-1438 355-16-7439)

When I tried to run the more simple

'(123-456)

I have seen that it also was evaluated to:

(|123-456|)

Why?

Upvotes: 3

Views: 72

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139251

It has nothing to do with evaluation.

CL-USER 84 > '123-456-789
|123-456-789|

CL-USER 85 > (type-of '123-456-789)
SYMBOL

123-456-789 is a symbol. The printer might print it as |123-456-789|. The reason: it has only digits and the - character in its name, which makes the printed representation a so-called potential number.

The pipe character is an escape character for symbols. It surrounds the symbol string and that will be the symbol name.

CL-USER 86 > '|This is a strange symbol with digits, spaces and other characters --- !!!|
|This is a strange symbol with digits, spaces and other characters --- !!!|

CL-USER 87 > (symbol-name *)
"This is a strange symbol with digits, spaces and other characters --- !!!"

For some reason the printer may think that 123-456-789 should be printed escaped. But the symbol name is the same:

CL-USER 88 > (eq '123-456 '|123-456|)
T

Escaping allows symbols to have arbitrary names.

A useless example:

CL-USER 89 > (defun |Gravitation nach Einstein| (|Masse in kg|) (list |Masse in kg|))
|Gravitation nach Einstein|

CL-USER 90 > (|Gravitation nach Einstein| 10)
(10)

The Backslash is the single escape (by default characters will be upcased):

CL-USER 93 > 'foo\foo\ bar
|FOOfOO BAR|

Side note:

Common Lisp has the idea of potential numbers. There is a number syntax for various number types: integer, float, ratio, complex, ... But there is room for extension defined by the standard. For example 123-456-789 might be in an extended Common Lisp a number! When the printer sees a symbol which looks like it could be a potential number, it escapes the symbol, so that it can safely read back as a symbol...

Upvotes: 5

Related Questions