David542
David542

Reputation: 110083

Difference between two cons

What is the difference between how cons works on the following two items:

(cons 1 '(2))
; (1 2)
(cons 1 2)
; (1 . 2)

Both evaluate pair? to true, so I'm curious how the two are different.

Does one produce:

 --------
| 1 | 2 | 
 -------

And the other produces:

 --------        --------
| 1 | -> |       | 2 | X |  
 -------         -------

Or what's the difference?

Upvotes: 1

Views: 92

Answers (3)

Will Ness
Will Ness

Reputation: 71065

There is no difference in how cons works in both cases.

The cons is the same. Anything produced by cons causes pair? to return true. Simple as that.

The two things that you cons together are different between the two code snippets.

In the second snippet, (cons 1 2), it is two numbers. Eschewing any inclinations toward efficiency of representation and being interested only in conceptual correctness, we regard it as creating

        ---------
        | * | * |
        ---------
          |   |
          v   v
          1   2

The first snippet, (cons 1 '(2)), uses a number 1 and an object created by '(2):

        ---------
        | * | * |
        ---------
          |   |
          v   v
          1   ---------
              | * | * |
              ---------
                |   |
                v   v
                2   NIL

Upvotes: 2

masp
masp

Reputation: 11

A proper list is always terminated with NIL. I.e., the first code example produces two cons-cells, one within the other, and the innermost is nil-terminated. The second example however doesn't follow the rules for standard lists, as it is just a single cons-cell with two non-nil values.

(cons 1 '(2)) 
CLISP> (1 2) ; This list is actually (cons 1 (cons 2 NIL))

(cons 1 (cons 2 NIL))
CLISP> (1 2) ; Same as above

However, is you leave NIL out, the REPL will indicate that with a dot.

(cons 1 2) ; this cons-cell is not terminated by NIL
CLISP> (1 . 2)

(cons 1 (cons 2 (cons 3 4))) ; A longer example
CLISP> (1 2 3 . 4) ; Notice the dot to indicate last element is non-nil.

Upvotes: 1

Óscar López
Óscar López

Reputation: 235994

You need to understand how a proper list is defined in Scheme. A proper list is created by consing elements, where the cdr part is either another cons cell or the empty list '(). The first example in the question is a proper list:

(cons 1 '(2))
(cons 1 (cons 2 '())) ; this is the same as above
=> '(1 2)

Of course we can also create arbitrary cons cells that don't follow the rule. The second example in the question demonstrates this, it's a cons cell where the cdr part is not another cons cell or the empty list, hence it's not a proper list:

(cons 1 2)
=> '(1 . 2)

Upvotes: 1

Related Questions