diff --git a/pkgs/racket-doc/scribblings/reference/dicts.scrbl b/pkgs/racket-doc/scribblings/reference/dicts.scrbl index 703c38aecb..20ae750fc5 100644 --- a/pkgs/racket-doc/scribblings/reference/dicts.scrbl +++ b/pkgs/racket-doc/scribblings/reference/dicts.scrbl @@ -15,14 +15,20 @@ values. The following datatypes are all dictionaries: @item{@techlink{vectors} (using only exact integers as keys);} - @item{@techlink{lists} of @techlink{pairs} (an @deftech{association - list} using @racket[equal?] to compare keys); and} + @item{@techlink{lists} of @techlink{pairs} as an @deftech{association + list} using @racket[equal?] to compare keys, which must be distinct; and} @item{@techlink{structures} whose types implement the @racket[gen:dict] @tech{generic interface}.} ] +When list of pairs is used as @tech{association list} but does not +have distinct keys (so it's not an association list), operations like +@racket[dict-ref] and @racket[dict-remove] operate on the first +instance of the key, while operations like @racket[dict-map] and +@racket[dict-keys] produce an element for every instance of the key. + @note-lib[racket/dict] @section{Dictionary Predicates and Contracts} diff --git a/pkgs/racket-test-core/tests/racket/dict.rktl b/pkgs/racket-test-core/tests/racket/dict.rktl index a213b66b25..1a978838f6 100644 --- a/pkgs/racket-test-core/tests/racket/dict.rktl +++ b/pkgs/racket-test-core/tests/racket/dict.rktl @@ -163,6 +163,16 @@ ;; preserve from GC: (list s1 s2))) +;; Check behavior on a list of pairs that isn't +;; a dictionary due to duplicate keys: +(test 1 dict-ref '((a . 1) (b . 2) (a . 3)) 'a) +(test '((b . 2) (a . 3)) dict-remove '((a . 1) (b . 2) (a . 3)) 'a) +(test '((a . 4) (b . 2) (a . 3)) dict-set '((a . 1) (b . 2) (a . 3)) 'a 4) +(test 3 dict-count '((a . 1) (b . 2) (a . 3))) +(test '((a 1) (b 2) (a 3)) dict-map '((a . 1) (b . 2) (a . 3)) list) +(test '(a b a) dict-keys '((a . 1) (b . 2) (a . 3))) +(test '(1 2 3) dict-values '((a . 1) (b . 2) (a . 3))) + ;; ---------------------------------------- (report-errs)