Document dictionary.

This commit is contained in:
Dominik Pantůček 2023-03-11 08:40:00 +01:00
parent de8f54005d
commit dd2716c1c7

View file

@ -57,12 +57,18 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dictionary
;; Returns an empty dictionary represented as empty list.
(define (make-dict)
'())
;; Checks whether given dictionary d contains the key k.
(define (dict-has-key? d k)
(if (assq k d) #t #f))
;; Retrieves the value for key k from dictionary d. If third argument
;; is provided it is used as default value in case the key does not
;; exist. If only two arguments are given and the key does not exist,
;; raises an error.
(define (dict-ref d k . r)
(let ((p (assq k d)))
(if p
@ -71,6 +77,8 @@
(error 'dict-ref "Key does not exist" k)
(car r)))))
;; Returns a new dictionary based on d with key k removed. If it
;; doesn't contain the key, an error is raised.
(define (dict-remove d k)
(let loop ((s d)
(r '())
@ -83,6 +91,9 @@
(loop (cdr s) r #f)
(loop (cdr s) (cons (car s) r) e)))))
;; Adds a new value v under the key k to the dictionary d possibly
;; overwriting any value which has been stored under the key
;; before. Returns the updated dictionary.
(define (dict-set d k v)
(let ((dr (let loop ((s d)
(r '()))
@ -94,6 +105,7 @@
(cons (cons k v)
dr)))
;; Returns the list of keys stored in given dictionary.
(define (dict-keys d)
(map car d))