This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: another nit.


Dirk Herrmann <dirk@ida.ing.tu-bs.de> writes:

> On Thu, 6 Jul 2000, Han-Wen Nienhuys wrote:
> 
> > why do assq-ref and friends return #f when the key is not found, in
> > stead of #<unspecified>? This makes it kind of hard to distinguish
> > between an alist not containing KEY and  (KEY . #f)
> 
> Actually, it would be best to throw an error if the key is not
> found.

no.  it would introduce a Guile-specific aspect into an otherwise
portable specification.

also, even if we assume that exceptions are standartized, it's not
reasonable to expect them to have reasonable performance.

so, if you don't know the domain of possible values in an alist, use
`assq' and friends, or maybe use some syntactic sugar like the
following:

(assq-bind (my-alist key)
  (value <do-stuff-with-value>)
  (else  <do-stuff-when-key-is-not-found>))

here is a quick-and-dirty definition of assq-bind (bad name, anything
better?):

(defmacro assq-bind (assq-args found-case not-found-case)
  (let ((pair (gensym)))
    `(let ((,pair (assq ,(cadr assq-args) ,(car assq-args))))
       (if ,pair
         (let ((,(car found-case) (cdr ,pair)))
           ,@(cdr found-case))
         (begin ,@(cdr not-found-case))))))

no error checking, should be rewritten hygienically, etc.

-- 
There are few personal problems which can't be solved by the suitable
application of high explosives.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]