[plt-scheme] Newbie question about Lisp
> Why the "." in evaluating (cons 1 2)
>
> returns (1 . 2)
Just to make sure: there isn't a literal "dot" element in the value we're
getting back. DrScheme's value printer puts that visual que there to say:
"You've got a pair whose first element is 1 and whose rest element is
2, and this is unusual enough that I'll print it with this funny dot."
The printer does this to help us see and distinguish the above from a
different value, the list containing 1 and 2:
(cons 1 (cons 2 '()))
which we know is a different thing from:
(cons 1 2)
The printer does something reasonable to print out those different values
differently.
But just to mention, though: building a cons pair whose "rest" isn't
itself a list is a somewhat advanced topic. In fact, the Beginner and
Intermediate HTDP language levels in DrScheme will actively disallow it
since the resulting value doesn't fit the data definition of a "list".
For more details about this, you may want to take a look at the HTDP link
that Geoffrey points out:
http://htdp.org/2003-09-26/Book/curriculum-Z-H-13.html#node_chap_9
> in evaluating (cons 1 2) returns (1 . 2)
>
> But (list 1 2) returns (1 2)
Another thing to note: what we see is somewhat separate from the value we
have. There are different ways of looking at the same values. DrScheme
makes this more clear if we run it in different language levels.
Concretely, if we're running DrScheme in "Beginning Student" language
without the list-abbreviation feature, then here's what we see:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Welcome to DrScheme, version 369.10-svn20apr2007 [3m].
Language: Beginning Student.
> (cons 1 2)
cons: second argument must be of type <list>, given 1 and 2
> (cons 1 (cons 2 empty))
(cons 1 (cons 2 empty))
> (list 1 2)
(cons 1 (cons 2 empty))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
If we switch to "Beginning Student with List Abbreviations" mode, what we
see is a little bit more concise. We're still dealing with the same exact
values, but we're seeing those values in a slightly more concise notation.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Welcome to DrScheme, version 369.10-svn20apr2007 [3m].
Language: Beginning Student with List Abbreviations.
> (cons 1 2)
cons: second argument must be of type <list>, given 1 and 2
> (cons 1 (cons 2 empty))
(list 1 2)
> (list 1 2)
(list 1 2)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
And if we switch to a full-on unrestricted, professional language like
"Pretty Big", then we start seeing the things you noted in your question,
using the very terse notation you've saw. (Plus CONS is unrestricted, as
you saw earlier.)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Welcome to DrScheme, version 369.10-svn20apr2007 [3m].
Language: Pretty Big (includes MrEd and Advanced Student).
> (cons 1 2)
(1 . 2)
> (cons 1 (cons 2 empty))
(1 2)
> (list 1 2)
(1 2)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Best of wishes!