Sorry I’m kinda dumb…
Is .
equal to cons
?
Doing (7 . 3)
gives an error
Not sure what is meant by “quoted syntax”
It means that '(1 . 2)
produces the same value as (cons 1 2)
.
It’s mostly used for association lists (lists used as dictionaries) '((key1 . val1) (key2 . val2) ...
and rest-arguments in function headers (define (foo arg1 arg2 . rest-args)
(list arg1 arg2 rest-args))
(foo 'a 'b 'c 'd) --> '(a b (c d))
I use association lists a lot (students can easily understand them) but mine contain 2-element lists. The search functions only care about a pair and its first element, so it is possible to use a dotted pair. But it is also fine to put a proper 2-element list in, and I think this is preferable, because dotted pairs are a historical artifact to save one word of memory. (They might have some subtle legitimate uses, but I never run across them.)
Also I think the dot in function headers works differently; it’s overloaded syntax. There, it’s just a way to refer to some initial segment of the list of arguments by individual names, and then lump the rest into a list.
And I think there is a definition of “proper list”; it’s given in the documentation for list?
.
I believe the dot in function header is read in the exact same way.
This even works:
(define (a . b . c)
(+ a c))
(b 1 2) ; 3
Similar to:
'(1 . 2 . 3) ; '(2 1 3)
The . in a function header is indeed an improper list; it’s just that lambda
interprets an improper list of formals to create a function that accepts arbitrarily many arguments.
Note that the syntax of lambda
is an argument for “all values are either proper or improper lists”
Also, .
doesn’t always create an improper list. '(a . (b c))
= '(a b c)
.
(define (f a . (b c)) (+ a b c))
(f 1 2 3) ; 6