Implement ldict-contains? predicate.

This commit is contained in:
Dominik Pantůček 2023-04-09 19:29:46 +02:00
parent 920fab2b83
commit a5ed5bca53
2 changed files with 23 additions and 9 deletions

View file

@ -49,11 +49,18 @@ populating it with initial data.
If only one argument is given, the procedure checks whether it is a
list of equality procedure and acts accordingly.
(ldict-empty? d)
(ldict-empty? ld)
* ```d``` - a ldict instance
* ```ld``` - a ldict instance
Returns true if given dictionary contains no keys.
Returns ```#t``` if given dictionary contains no keys.
(ldict-contains? ld k)
* ```ld``` - a ldict instance
* ```k``` - a key compatible with given ldict
Returns ```#t``` if given ```ld``` contains given key ```k```.
### IO

View file

@ -34,8 +34,9 @@
ldict-empty?
ldict-has-key?
ldict-contains?
ldict-ref
ldict-remove
ldict-set
ldict-keys
@ -76,15 +77,21 @@
;; Convenience accessors
(define ldict-meta cadr)
(define ldict-equality? caadr)
(define ldict-list cddr)
(define ldict-pairs cddr)
;; Returns true if given dictionary contains no keys
(define (ldict-empty? d)
(null? (ldict-list d)))
(define (ldict-empty? ld)
(null? (ldict-pairs ld)))
;; Checks whether given dictionary d contains the key k.
(define (dict-has-key? d k)
(if (assq k d) #t #f))
(define (ldict-contains? ld k)
(let ((equality? (ldict-equality? ld)))
(let loop ((pairs (ldict-pairs ld)))
(if (null? pairs)
#f
(if (equality? (caar pairs) k)
#t
(loop (cdr pairs)))))))
;; 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