1

I tried to reproduce the same data layout by using different combinations of list and cons. In MIT Scheme, the same data layout seems to be printed differently depending on how it was built.

In example,

> (cons 1 (cons 2 3))
(1 2 . 3)
> (list 1 (cons 2 3))
(1 (2 . 3))
> (cons 1 (list 2 3))
(1 2 3)
> (list 1 (list 2 3))
(1 (2 3))

The underlying data should always be the same. Why does MIT Scheme represents it differently? Is the underlying arrangements of pairs really the same every time?

frm
  • 3,346
  • 1
  • 21
  • 21
  • `(list 1 2 3)` is equivalent of `(cons 1 (cons 2 (cons 3 '())))`. – jkiiski Jul 10 '17 at 18:51
  • 2
    The objects are different, please try `equal` to see for yourself. You might find the page [Dotted-Pair-Notation](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dotted-Pair-Notation.html) useful. – sds Jul 10 '17 at 18:52
  • 1
    If `(cons 1 (cons 2 3))` returned the same thing as `(list 1 (list 2 3))`, that would make `cons` and `list` synonyms (in the two-argument cases, at least); they are not. – Kaz Jul 10 '17 at 20:40

1 Answers1

3

Each of the inputs is building a different output, so they're printed differently. Notice that the following expressions are equivalent:

(cons 1 (cons 2 3))
; (1 2 . 3)

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

(cons 1 (list 2 3))
(cons 1 (cons 2 (cons 3 '())))
; (1 2 3)

(list 1 (list 2 3))
(cons 1 (cons (cons 2 (cons 3 '())) '()))
; (1 (2 3))

Remember: a list is defined if and only if the cdr part of each cons is also a cons cell, or an empty list.

Óscar López
  • 232,561
  • 37
  • 312
  • 386