Implement ldict-remove procedure.

This commit is contained in:
Dominik Pantůček 2023-04-09 19:40:10 +02:00
parent 1df2792838
commit a80b216bbc
2 changed files with 23 additions and 11 deletions

View file

@ -110,17 +110,21 @@
;; 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 '())
(e #t))
(if (null? s)
(if e
(error 'dict-remove "Key does not exist" k)
r)
(if (eq? (caar s) k)
(loop (cdr s) r #f)
(loop (cdr s) (cons (car s) r) e)))))
(define (dict-remove ld k)
(let ((equality? (ldict-equality? ld)))
(let loop ((pairs (ldict-pairs ld))
(res '())
(failure #t))
(if (null? pairs)
(if failure
(error 'dict-remove "Key does not exist" k)
(cons TAG-LDICT
(cons (ldict-meta ld)
res)))
(loop (cdr pairs)
(if (equality? (caar pairs) k)
res
(cons (car pairs) res)))))))
;; Adds a new value v under the key k to the dictionary d possibly
;; overwriting any value which has been stored under the key