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?
"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."
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.
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 ...) .
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).
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With