Note: This is taken from the Chicken Wiki, where a more recent version could be available.
This egg provides keyword/value list (or key-value list, for short) operations. Key-value lists are a restricted subset of property lists wherein the key is always a self-evaluating keyword symbol.
In addition to key-value list manipulators, the egg also provides facilities for conversions between kvlists (key-value lists) and both plists (property lists) and alists (association lists).
Here's a run-down of the different ways to use Scheme lists for directly storing mappings/bindings:
(Self-evaluating keyword symbols are defined in SRFI-88: Keyword objects).
These examples assume Chicken is being run with -keyword-style suffix, which is the default mode. See the manual section Using the interpreter in case you wish to use prefix-style keywords as in Common Lisp.
#;1> (use kvlists) #;2> (define lst '(foo: 1 bar: 2 baz: 3)) #;3> (kvlist? lst) #t #;4> (kvlist-ref lst baz:) 3 #;5> (kvlist-ref lst abc:) #f #;6> (kvlist-ref lst abc: 123) 123 #;7> (kvlist->alist lst) ((foo . 1) (bar . 2) (baz . 3)) #;8> (kvlist->plist lst) (foo 1 bar 2 baz 3)
There are many opportunities to use key-value lists to cut down on the proliferation of parentheses in syntactic forms. While this may arguably be against the Lisp ethos, here's a straightforward let/kv macro which uses let to introduce lexically scoped variable bindings but without one layer of enclosing parentheses in the defining form:
(use kvlists) (define-macro (let/kv head . body) `(let ,(kvlist-map (lambda (k v) (list (keyword->symbol k) v)) head) ,@body)) (let/kv (x: 123 y: 456 z: 789) (printf "x + y + z = ~A\n" (+ x y z)))
[procedure] (kvlist-cons* KEY VALUE KVLIST)
[procedure] (kvlist? X)
[procedure] (alist? X)
[procedure] (plist? X)
[procedure] (kvlist-key? KVLIST KEY)
[procedure] (kvlist-ref KVLIST KEY)
[procedure] (kvlist-assoc KEY KVLIST)
[procedure] (kvlist-map PROC KVLIST)
[procedure] (kvlist-for-each PROC KVLIST)
[procedure] (kvlist-delete KEY KVLIST)
[procedure] (kvlist-set! KVLIST KEY VALUE)
[procedure] (kvlist->alist KVLIST)
[procedure] (kvlist->plist KVLIST)
[procedure] (alist->kvlist ALIST)
[procedure] (alist->plist ALIST)
[procedure] (plist->kvlist PLIST)
[procedure] (plist->alist PLIST)
[procedure] (keyword->symbol KEYWORD)
[procedure] (symbol->keyword SYMBOL)
Copyright (c) 2006-2007 Arto Bendiken. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.