(1 . 2) is the s-expression syntax for a cons cell. The cons cell has 1 as the car and 2 as the cdr.
'(1 . 2) with the quote character in front is a short notation for (quote (1 . 2)). The quote instructs the evaluator to not (!) evaluate the form it encloses and to just return it as it is. The value of this quoted form is just the form itself. quote says: do not evaluate. In Common Lisp this can be considered to be constant literal data.
(cons 1 2) is a Lisp form which has the function cons in front and two arguments: 1 and 2. If we evaluate this form, the function cons gets called with the argument 1 and 2. cons returns by definition a new cons cell with its arguments as the car and cdr. So (cons 1 2) returns a fresh new cons cell whenever we evaluate it.
the simbol quote (') is a function that says: "create this list whithout evaluating this."
example: '(* 2 2 ) ; It's going to create the list ( * 2 2 )
(list (* 2 2) ; It's going to create (4) because the list is evaluated.
the function cons create associated pairs and just this.
Example: (cons (a b c) (1 2 3) ) ;create (a b c . 1 2 3)
(cons a (1 2 3 4) ) ;create (a. 1 2 3 4)
The two functions you used do the same thing on this case, but using diferents ways to do it.
because quote (') just create the list in front of the simbol quote ('), and the cons function just create the associated pair with the "itens" thar you gave to the function.
They are not exactly the same, even though they evaluate to the same values in the REPL. Consider these examples, in which cons cells are modified destructively:
In the first version, you are changing the literal cons cell in the code itself (thus, this is self-modifying code). In the second version, the cons cell is not literal. It is produced every time the code is called, and only this fresh cons cell will be changed.
Since this can lead to subtle bugs when using destructive operations, one should be careful with such literal objects in code. This also affects list literals ('(1 2 3) vs. (list 1 2 3)), which are built from cons cells.
literal adj. (of an object) referenced directly in a program rather
than being computed by the program; that is, appearing as data in a
quote form, or, if the object is a self-evaluating object, appearing
as unquoted data. ``In the form (cons "one" '("two")), the expressions
"one", ("two"), and "two" are literal objects.''
Both are objects with similar structures. That is, they're both cons cells, with 2 in the CAR position and 3 in the CDR position.
The main difference between them is that '(2 . 3) is a constant and (cons 2 3) generates a new cons cell. The distinction between these should become evident if you take two (similar-looking) functions as per below:
'(2 . 3)
is a dotted pair.(cons 2 3)
creates a dotted pair too. So these should evaluate to the same thing.So one is a literal for a dotted pair, the other one creates a dotted pair.
(1 . 2)
is the s-expression syntax for acons
cell. The cons cell has 1 as thecar
and 2 as thecdr
.'(1 . 2)
with the quote character in front is a short notation for(quote (1 . 2))
. Thequote
instructs the evaluator to not (!) evaluate the form it encloses and to just return it as it is. The value of this quoted form is just the form itself.quote
says: do not evaluate. In Common Lisp this can be considered to be constant literal data.(cons 1 2)
is a Lisp form which has the functioncons
in front and two arguments: 1 and 2. If we evaluate this form, the functioncons
gets called with the argument 1 and 2.cons
returns by definition a new cons cell with its arguments as thecar
andcdr
. So(cons 1 2)
returns a fresh new cons cell whenever we evaluate it.they are not the same and are the same.
the simbol quote (') is a function that says: "create this list whithout evaluating this." example: '(* 2 2 ) ; It's going to create the list ( * 2 2 ) (list (* 2 2) ; It's going to create (4) because the list is evaluated.
the function cons create associated pairs and just this. Example: (cons (a b c) (1 2 3) ) ;create (a b c . 1 2 3) (cons a (1 2 3 4) ) ;create (a. 1 2 3 4)
The two functions you used do the same thing on this case, but using diferents ways to do it. because quote (') just create the list in front of the simbol quote ('), and the cons function just create the associated pair with the "itens" thar you gave to the function.
They are not exactly the same, even though they evaluate to the same values in the REPL. Consider these examples, in which cons cells are modified destructively:
Compared to this:
In the first version, you are changing the literal cons cell in the code itself (thus, this is self-modifying code). In the second version, the cons cell is not literal. It is produced every time the code is called, and only this fresh cons cell will be changed.
Since this can lead to subtle bugs when using destructive operations, one should be careful with such literal objects in code. This also affects list literals (
'(1 2 3)
vs.(list 1 2 3)
), which are built from cons cells.From the HyperSpec:
Both are objects with similar structures. That is, they're both cons cells, with 2 in the CAR position and 3 in the CDR position.
The main difference between them is that '(2 . 3) is a constant and (cons 2 3) generates a new cons cell. The distinction between these should become evident if you take two (similar-looking) functions as per below:
Quite a few implementations will return '(3 . 3) '(4 . 3) and so on as you keep calling b.