drb
drb

Reputation: 738

Why does Scheme have both list and quote?

Since (list 1 2 3) yields (1 2 3) and (quote (1 2 3)) yields (1 2 3), what is the rationale for having both?

Since Scheme is otherwise so spare, these must have some meaningful difference. What is that?

Upvotes: 12

Views: 1917

Answers (2)

sepp2k
sepp2k

Reputation: 370082

In the example you mentioned quote and list have the same result because numeric constants evaluate to themselves. If you use expressions that are not self-evaluating in the list (say variables or function calls), you'll see the difference:

(quote (a b c)) will give you a list that contains the symbols a, b and c while (list a b c) will give you a list containing the values of the variables a, b and c (or an error if the variables do not exist).

Upvotes: 27

user448810
user448810

Reputation: 17851

List creates a list, so (list 1 2 3) creates a three-element list.

Quote prevents evaluation. Without quote, the expression (1 2 3) would be evaluated as the function 1 called with arguments 2 and 3, which obviously makes no sense. Quote prevents evaluation and just returns the list, which is specified literally in its external printable form as (1 2 3).

Upvotes: 5

Related Questions