Reputation: 6189
I know the dot for creating a procedure, which can take an arbitrary amount of arguments, but I saw several other examples, which use the dot in other places. One example is from the docs of csv-reading
:
(define next-row (make-csv-reader (open-input-file "fruits.csv") '((separator-chars #\|) (strip-leading-whitespace? . #t) (strip-trailing-whitespace? . #t))))
To me this looks like a list of configuration parameters is "set", but what does the dot do there?
Another example is from a web application tutorial from the docs (in the formlet section) as well:
; new-post-formlet : formlet (values string? string?) ; A formlet for requesting a title and body of a post (define new-post-formlet (formlet (#%# ,{input-string . => . title} ,{input-string . => . body}) (values title body)))
My guess for this example is, that the dots somehow allow to write =>
as an infix operator. I was able to put the =>
at the front of the lists and leave the dots away and it still worked at some point. I didn't try with the final version of the tutorial's code though.
However, this "infixing" wouldn't fit the first example, I think.
Upvotes: 4
Views: 4097
Reputation: 48775
The dot
is not uniquely a racket thing, but a lisp thing. A list is made out of pairs and one pair has the literal form (car . cdr)
and a list of the elements 2, 3
is made up like (2 . (3 . ()))
and the reader can read this, however a list that has a pair as it's cdr
can be shown without the dot and the parens such that (2 . (3 . ()))
is the same as (2 3 . ())
. Also a dot with the special empty list in the end can be omitted so that (2 3)
is how it can be read and the only way display
and the REPL would print it.
What happens if you don't have a pair or null as the cdr
? Then the only way to represent it is as a dotted. With (2 . (3 . 5))
you can only simplify the first dot so it's (2 3 . 5)
.
The literal data structure looks like an assoc
with pairs as elements where the car
is the key and cdr
is the value. Using a new pair is just wast of space.
As an parameter list an interpreter looks at the elements and if it's a pair it binds the symbol in car
, but if it's not a pair it is either finished with null or you have a rest argument symbol. It's convenient way to express it. Clojure that don't have dotted pairs use a &
element while Common Lisp does dotted in structures, but not as a parameter specification since it supports more features than Scheme and Clojure and thus use special keywords to express it instead.
The second is #lang racket
specific and is indeed used to support infix. If you write (5 . + . 6)
a reader extension will change it to (+ 5 6)
before it gets evaluated. It works the same way even if it's quoted or actual code and curlies are of course equal to normal and square parentheses so the second one actually becomes:
(define new-post-formlet
(formlet
(#%# ,(=> input-string title)
,(=> input-string body))
(values title body)))
If you were to use #!r5rs
or #!r6rs
in racket (5 . + . 6)
will give you a read error.
Upvotes: 6