I saw there are other questions about the dot "." I followed but it didn't work for my code.... it's a part of code, the implementation is not focused to this symbol. but output should be included this dot. when I give input of two lists '(1 2 3) '(4 5) my expected output => (1 . 4) (2 . 5)
I managed to get (1 4) (2 5) just need to add "." in the middle.
Part of mycode
(cons (list (car lst1) (car lst2))
....
for the "." symbol , if I try
**trial-1**
(cons '(list (car lst1) (car lst2)) ...)
then the output : ((list (car lst1) (car lst2))
**trail-2**
(cons (list (car lst1) '. (car lst2)) ...)
then.. it says : illegal use of `.'
what are the rules to use the dot? any documents I can look at? btw, I am using Racket(R5RS).
Although
.
can be a part of a symbol,.
is not in itself a valid symbol..
is used in list structure as a divider between thecar
and thecdr
. eg.(a . (b . (c . ()))) ; ==> (a b c)
.Practical application is the historic use as a rest argument in the prototype of a procedure and you can use it as template an transformation of macros. Also,
read
can read it in and you can use it as data like(define lst '((a) . (b)))
.So to recap:
(a . b)
is a pair ofa
andb
, while(a.b)
is the same as(a.b . ())
thus a pair of the symbola.b
and the empty list.As for how to create a pair you use
cons
.(cons 'a 'b) => (a . b)
while(list a b) => (cons 'a (cons b '()))
. Now you can make a pair with two lists as arguments,(cons '(1 4) '(2 5))
but if you print it you know that(a . (b))
is the same as(a b)
thus(cons '(1 4) '(2 5))
will display as((1 4) 2 5)
since it will prefer not to display the dot. If it would prefer to show dots it would have displayed it as((1 . (4 . ())) . ((2 . (5 . ()))))
instead since thats how many pairs there are in that data structure.If you have managed to get the output
((1 4) (2 5))
and really wanted((1 . 4) (2 . 5))
you need to replace alist
withcons
.From R7RS:
.
is not a symbol.The dot symbol is displayed when you build a
cons
-pair or a list which is not proper (meaning: it doesn't end with the empty list). For example:For instance, to display an output such as the one shown in the question try this:
It's called a dotted pair and is produced when you
cons
an item with a non-list, such as:See: http://download.plt-scheme.org/doc/html/guide/Pairs__Lists__and_Scheme_Syntax.html