phanthero
2020-12-9 12:12:26

Sorry I’m kinda dumb…


phanthero
2020-12-9 12:12:34

Is . equal to cons?


phanthero
2020-12-9 12:12:47

Doing (7 . 3) gives an error


phanthero
2020-12-9 12:13:08

Not sure what is meant by “quoted syntax”


jgeddes
2020-12-9 12:35:13

It means that '(1 . 2) produces the same value as (cons 1 2) .



laurent.orseau
2020-12-9 12:38:56

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))


plragde
2020-12-10 03:36:00

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.)


plragde
2020-12-10 03:38:02

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.


plragde
2020-12-10 03:40:37

And I think there is a definition of “proper list”; it’s given in the documentation for list?.


sorawee
2020-12-10 03:47:23

I believe the dot in function header is read in the exact same way.


sorawee
2020-12-10 03:47:38

This even works:

(define (a . b . c) (+ a c)) (b 1 2) ; 3


sorawee
2020-12-10 03:48:05

Similar to:

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


samth
2020-12-10 04:35:24

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.


samth
2020-12-10 04:35:46

Note that the syntax of lambda is an argument for “all values are either proper or improper lists”


sorawee
2020-12-10 05:18:12

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