chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re : [Chicken-users] idiomatic/usual way of file-based configuration in


From: minh thu
Subject: Re : [Chicken-users] idiomatic/usual way of file-based configuration in Scheme
Date: Wed, 7 Feb 2007 08:41:28 +0100

2007/2/6, Tony Sidaway <address@hidden>:
On 2/6/07, minh thu <address@hidden> wrote:
>
> It would just amount to read a list of key/value pairs in Scheme
> syntax. Or maybe a triple if the type is given.
>
> What do you use ?
>
As I think you realise, this is a capability that is as old as Lisp
itself.  A property list is just a specialized form of s-expression.
I'm not aware of any formal standards for this, but it's pretty much a
matter of basic Scheme.

Yes of course, but it was the 'standard' part of your response that
interested me.


Key-value tuples as given by other respondents ((locale ca)(lang
fr)(user-data "paul" "users")), use read to read the list of key-value
pairs, (with-output-to-file "properties" write to write it.

(define property-list (with-input-from-file "properties" read))

...

(with-output-to-file "properties" (write property-list))

And to perform lookup, (let ((value-list (assq 'locale property-list)))
...)

Your first value would be (cadr value-list), the third (caddr
value-list) and so on.  (car value-list) holds the matching lookup
key.

An alternative would be to use lists of pairs ((locale . en)(lang .
fr)).  The only difference is that you use cdr to address the value,
not car.  Remember that '(a b c) is exactly the same as '(a . (b c)),
so you can mix both notations in the same list as long as your
software (and anyone who edits the list by hand) knows which form to
use for a given property.

To update, use something like this:

(define my-age 64)
...
(let ((value-list (assq 'age property-list)))
  (set-cdr! value-list (list my-age)))

To delete an entry, use SRFI-1 delete or delete! (normally you'd use
eq? as a comparison operator for this, to compare by keyword).

(delete! 'lang property-list (lambda (key value-list) (eq? key (car
value-list))))

If your keys can't be represented as symbols, surround them in string
quotes and use assoc rather than assq.

An association list can contain any serializable Scheme data,
including other association lists.

For very large collections of keyed data that you will need to lookup
frequently, consider using SRFI-69 hash tables for the internal
representation.  Procedures alist->hash-table and hash-table->alist
are provided for this purpose.  This kind of heavy duty tool shouldn't
be needed for normal property lookups, however.

Thank you very much Tony.
thu




reply via email to

[Prev in Thread] Current Thread [Next in Thread]