Lisp, minus and (number number) difference

What's the difference between

(cons 2 3) 

and

 '(2 . 3) 

in lisp?

+4
lisp quote cons
source share
5 answers

'(2 . 3) is a dotted pair.

(cons 2 3) also creates a dotted pair. Therefore, they must evaluate the same thing.

So, one of them is a literal for a dotted pair, the other creates a dotted pair.

+1
source share

They are not exactly the same, even if they evaluate the same values ​​in REPL. Consider these examples in which cons cells are destructively modified:

 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 change literal cons in the code itself (thus, this is self-modifying code). In the second version, the cons cell is not literal. It is created every time the code is called, and only this new 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 errors when using destructive operations, one must be careful with such literal objects in the code. It also affects list literals ( '(1 2 3) vs. (list 1 2 3) ), which are built from cons cells.

From HyperSpec :

literal adj. (of an object) directly referenced in the program than calculated by the program; that is, as data in the form of a quote or, if the object is a self-evaluating object, appears as unquoted data. `` In the form of (cons "one" '("two")), the expressions "one", ("two") and "two" are literal objects. ''

+10
source share

Both are objects with similar structures. That is, they are 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 difference between them should become obvious if you take two (similar) functions, as shown 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), etc. when you keep calling b.

+2
source share

(1 . 2) is the syntax of the s-expression for the cons cell. The cons cell has 1 as car and 2 as cdr .

'(1 . 2) with a quotation mark in front is a short note for (quote (1 . 2)) . quote instructs the appraiser not (!) to evaluate the form in which it enters, and simply return it as is. The meaning of this cited form is only the form itself. quote says: do not evaluate. In Common Lisp, this can be considered persistent literal data.

(cons 1 2) is a Lisp form that has a cons function in front and two arguments: 1 and 2. If we evaluate this form, the cons function is called with arguments 1 and 2. cons returns by definition a new cons cell with its arguments as car and cdr . Therefore (cons 1 2) returns a new fresh cons cell when we evaluate it.

0
source share

they do not match and are the same.

A character quote (') is a function that states: "Create this list without evaluating it." example: '(* 2 2); He is about to create a list (* 2 2) (list (* 2 2); he is about to create (4) because the list is being evaluated.

the cons function creates related pairs and only that. Example: (cons (abc) (1 2 3)); create (ab 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 in this case, but use different ways to do this. because quote (') just creates the list before the simbol (') quote, and the cons function simply creates a coupled with the "itens" that you passed to the function.

0
source share

All Articles