Lisp, cons and (number . number) difference
Asked Answered
T

5

4

What is the difference between

(cons 2  3)

and

'(2 . 3)

in Lisp?

Tortuga answered 28/7, 2011 at 20:18 Comment(2)
Is that a period or a comma in the second expression?Homeric
@David: wondered if it's possible to change your answer pick. I like danlei's better than mine.Venavenable
V
1

'(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.

Venavenable answered 28/7, 2011 at 20:22 Comment(1)
Actually there is a difference in that cons would produce a setf-able place and quote would produce a constant value which you should not modify.Pontiff
D
10

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:

TEST> (defun literal-cons ()
        (let ((cons '(1 . 2)))
          (incf (cdr cons))
          cons))
LITERAL-CONS
TEST> (literal-cons)
(1 . 3)
TEST> (literal-cons)
(1 . 4)
TEST> (literal-cons)
(1 . 5)

Compared to this:

TEST> (defun non-literal-cons ()
        (let ((cons (cons 1 2)))
          (incf (cdr cons))
          cons))
NON-LITERAL-CONS
TEST> (non-literal-cons)
(1 . 3)
TEST> (non-literal-cons)
(1 . 3)

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.

TEST> (function-lambda-expression #'literal-cons)
(LAMBDA NIL
  (DECLARE (CCL::GLOBAL-FUNCTION-NAME LITERAL-CONS))
  (BLOCK LITERAL-CONS (LET ((CONS '(1 . 5))) (INCF (CDR CONS)) CONS))) ;; notice the '(1 . 5)
NIL
LITERAL-CONS

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:

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

Desist answered 28/7, 2011 at 21:46 Comment(2)
Your literal-cons function's behavior must be specific to your implementation. On SBCL, I get the results as your non-literal-cons. This is curious enough that I opened a new question about it: #20343293Furunculosis
@OpenLearner Right; the consequences of modifying literal data are undefined by the standard. Anything could happen. I think the example shown in this answer is common, but SBCL does seem to be doing something interesting here…Prepossessing
G
2

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:

(defun a () 
  (let ((v (cons 2 3))) 
   (incf (car v))
   v)

(defun b () 
  (let ((v '(2 . 3))) 
   (incf (car v))
   v)

Quite a few implementations will return '(3 . 3) '(4 . 3) and so on as you keep calling b.

Ginger answered 29/7, 2011 at 11:56 Comment(0)
V
1

'(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.

Venavenable answered 28/7, 2011 at 20:22 Comment(1)
Actually there is a difference in that cons would produce a setf-able place and quote would produce a constant value which you should not modify.Pontiff
C
0

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

Culpepper answered 29/7, 2011 at 1:8 Comment(0)
T
0

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.

Thirzia answered 31/7, 2011 at 4:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.