What's the difference between:
(cons 'a (cons 'b 'c)) ;; (A B . C)
and
(cons 'a '(b.c)) ;; (A B.C)
I need to create the following list ((a.b).c) using cons so i'm trying to understand what that "." represents.
L.E.: I have the following (cons (cons 'a 'b) 'c) but it produces ((A . B) . C) and not ((A.B).C) (Note the extra spaces)
Spaces are used to separate list tokens.
A.Bis a single token.(A.B)is a list with a single element.(A . B)is a cons cell withAas car andBas cdr.A cons cell is a pair of "things" (objects). In your case, these things are symbols, and they are named
A,B, etc.. The printed representation of such a cell is(A . B), for example. This is called "dot notation". The first element is called "car", the second "cdr".The function
conscreates such a cell.(cons 'a 'b)thus produces the cell(A . B). Note that names are always upcased internally.This is most likely what your teacher wanted, so
((A . B) . C)is the correct output, and your code the right answer. This is a cell where the car points to another cell, and the cdr containsC. That other cell is a cell where the car containsAand the cdrB.By the way, a list is a linear chain of such cons cells, such that the car always holds a value and the cdr points to the rest of the list. The last cdr points nowhere (which is called NIL in Lisp). In dot notation, a list is e.g.
(A . (B . (C . NIL))). Since lists are important, they can be written shorter like this:(A B C). If the last CDR has a value instead of NIL, it is shown in dot notation, e.g.(A . (B . (C . D))))can be written as(A B C . D).