Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Scheme have both list and quote?

Tags:

scheme

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?

like image 849
drb Avatar asked Jan 25 '12 14:01

drb


People also ask

Is a Pair a list in scheme?

"A list is a combination of pairs that creates a linked list. More precisely, a list is either the empty list null, or it is a pair whose first element is a list element and whose second element is a list."

What is a pair in scheme?

Pairs are used to combine two Scheme objects into one compound object. Hence the name: A pair stores a pair of objects. The data type pair is extremely important in Scheme, just like in any other Lisp dialect.

How do you unquote in a scheme?

Just as you can use a single quote character and write '(foo bar baz) instead of (quote (foo bar baz) , you can use a backquote character ( ` ) to replace (quote ...) and a comma character ( , ) to replace (unquote ...) .


2 Answers

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

like image 98
sepp2k Avatar answered Sep 28 '22 23:09

sepp2k


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

like image 33
user448810 Avatar answered Sep 28 '22 23:09

user448810