Associative array/Merging: Difference between revisions

Added a Scheme implementation.
No edit summary
(Added a Scheme implementation.)
Line 1,486:
"price": "15.25",
}</pre>
=={{header|Scheme}}==
{{works with|Chez Scheme}}
'''Quick and Dirty'''
<br />
The procedures which access association lists (alist) in Scheme prefer the first matched item
in an alist. Hence, a merge may be implemented by simply appending the update alist to the
front of the base alist. The downside is this leaves a bunch of useless junk in the alist.
<lang scheme>; Merge alists by appending the update list onto the front of the base list.
; (The extra '() is so that append doesn't co-opt the second list.)
(define append-alists
(lambda (base update)
(append update base '())))
 
; Test...
(printf "~%Merge using append procedure...~%")
; The original base and update alists.
(let ((base '(("name" . "Rocket Skates") ("price" . 12.75) ("color" . "yellow" )))
(update '(("price" . 15.25) ("color" . "red") ("year" . 1974))))
; Merge by appending the update list onto the front of the base list.
(let ((merged (append-alists base update)))
; Show that everything worked.
(printf "Merged alist:~%~s~%" merged)
(printf "Values from merged alist:~%")
(let loop ((keys '("name" "price" "color" "year")))
(unless (null? keys)
(printf "~s -> ~s~%" (car keys) (cdr (assoc (car keys) merged)))
(loop (cdr keys))))))</lang>
{{out}}
<pre>
Merge using append procedure...
Merged alist:
(("price" . 15.25) ("color" . "red") ("year" . 1974) ("name" . "Rocket Skates") ("price" . 12.75) ("color" . "yellow"))
Values from merged alist:
"name" -> "Rocket Skates"
"price" -> 15.25
"color" -> "red"
"year" -> 1974
</pre>
'''More True to Intent'''
<br />
This is more true to the intent of the Task. It generates a new alist with only the key/value
pairs needed.
<lang scheme>; Merge the given alists. Prefer the items from the "update" alist.
; Returns a new list; the argument lists are not modified.
(define merge-alists
(lambda (base update)
(let ((merged (list-copy update))
(remains (list-copy base)))
(let loop ((shadowing merged))
(if (null? shadowing)
(append merged remains)
(begin (set! remains (remp (lambda (pair) (equal? (car pair) (caar shadowing)))
remains))
(loop (cdr shadowing))))))))
 
; Test...
(printf "~%Merge using defined merge-alists procedure...~%")
; The original base and update alists.
(let ((base '(("name" . "Rocket Skates") ("price" . 12.75) ("color" . "yellow" )))
(update '(("price" . 15.25) ("color" . "red") ("year" . 1974))))
; Merge using the defined merge-alists procedure.
(let ((merged (merge-alists base update)))
; Show that everything worked.
(printf "Merged alist:~%~s~%" merged)
(printf "Values from merged alist:~%")
(let loop ((keys '("name" "price" "color" "year")))
(unless (null? keys)
(printf "~s -> ~s~%" (car keys) (cdr (assoc (car keys) merged)))
(loop (cdr keys))))))</lang>
{{out}}
<pre>
Merge using defined merge-alists procedure...
Merged alist:
(("price" . 15.25) ("color" . "red") ("year" . 1974) ("name" . "Rocket Skates"))
Values from merged alist:
"name" -> "Rocket Skates"
"price" -> 15.25
"color" -> "red"
"year" -> 1974
</pre>
 
=={{header|SenseTalk}}==