diff --git a/doc/utils.md b/doc/utils.md index f94424d..5ca0c95 100644 --- a/doc/utils.md +++ b/doc/utils.md @@ -81,6 +81,15 @@ of missing key. Returns a new dictionary with the record under given key removed. If the dictionary does not contain the key ```k```, an error is raised. + (ldict-set ld k v) + +* ```ld``` - a ldict instance +* ```k``` - a compatible key +* ```v``` - a value to insert + +Sets existing key ```k``` to the new value ```v``` or inserts it if it +is not already present in the dictionary ```ld```. + ### IO (import util-io) diff --git a/src/util-dict.scm b/src/util-dict.scm index e0766ce..aa59e2e 100644 --- a/src/util-dict.scm +++ b/src/util-dict.scm @@ -110,7 +110,7 @@ ;; 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 ld k) + (define (ldict-remove ld k) (let ((equality? (ldict-equality? ld))) (let loop ((pairs (ldict-pairs ld)) (res '()) @@ -129,16 +129,19 @@ ;; 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 '())) - (if (null? s) - r - (if (eq? (caar s) k) - (loop (cdr s) r) - (loop (cdr s) (cons (car s) r))))))) - (cons (cons k v) - dr))) + (define (ldict-set ld k v) + (let ((equality? (ldict-equalit? ld))) + (let loop ((pairs (ldict-pairs ld)) + (res '())) + (if (null? pairs) + (cons TAG-LDICT + (cons (ldict-meta ld) + (cons (cons k v) + res))) + (loop (cdr pairs) + (if (equality? (caar pairs) k) + res + (cons (car pairs) res))))))) ;; Returns the list of keys stored in given dictionary. (define (dict-keys d)