Wizard
Wizard

Reputation: 22083

(1 2) reported an error of invalid function

Upon reading Cons-Cell-types

ELISP> '(1 2)
(1 2)

ELISP> (1 2)
*** Eval error ***  Invalid function: 1

The reported error confused me,
How could an integer of 1 be interred as a function which is invalid.

Upvotes: 1

Views: 75

Answers (1)

coredump
coredump

Reputation: 38809

Note that the paragraph is about reading and printing lisp forms:

The read syntax and printed representation for lists are identical, and consist of a left parenthesis, an arbitrary number of elements, and a right parenthesis.

When encoutering (1 2), the interpreter builds a list of two elements, which, when printed, is also printed as (1 2). In a Read-Eval-Print-Loop, however, the form being read is immediately evaluated, and the evaluation rules says that:

If the first element of a list being evaluated is a Lisp function object, byte-code object or primitive function object, then that list is a function call.

The result is different when you write the list quoted:

'(1 2)

Here above, the quote syntax translates the form into the following one (at read-time):

(quote (1 2))

And the quote operator is special in that when evaluated, it does not evaluate its subexpression, but returns it as-is. That's why '(1 2) in the REPL evaluates as the literal list enclosed under the quote, namely (1 2).

Upvotes: 2

Related Questions