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.


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.  _Any_ object, even #<unspecified> could be the value of a 
(KEY . value) pair.  You simply shouldn't use assq-ref if you don't know
whether the key exists.  That's the reason assq is defined the way it
is:  It returns either a pair or #f, no ambiguity.

If you really want to have some short way to get the value of an alist
where you don't know if the key exists, I suggest to write some function
of your own with a dedicated return value which is not eq to anything
else.  For example:

  (define assq-ref-not-found (cons 'tag 'assq-ref-not-found))
  (define (assq-ref-sloppy alist key)
    (cond ((assq key alist) => cdr)
          (else assq-ref-not-found)))

What do others think?  Should assq-ref be changed to throw an error if the
key is not found?

Best regards
Dirk


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