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.


Hello,

DH>Actually, it would be best to throw an error if the key is not found.

CM>Since assq-ref is suppose to "rhyme" with vector-ref, and vector-ref
CM>throws an error, it seems like assq-ref should throw and error as well.  
CM>Different schemes handle vector-ref out of bounds errors differently so
CM>I don't see the problem with assq-ref throwing an error in a GUILE
CM>specific way (vector-ref does too).

Just to contribute to this discussion from the point of view of someone
who has programmed with dictionaries and hash tables in multiple
languages: an unfound hash key is not an exception. I think alists are
generally seen as a poor man's hash table for Scheme. With that in mind it
is clear that assq-ref should *not* throw an exception on a key not being
found:

  - this is not normal. Perl, Tcl, even Postscript don't throw
    exceptions for unfound hash elements. Python does, but Python,
    like Java, depends heavily on exception handling and has
    great built-in language support.

  - exception handling is way beyond the expertise of most beginners,
    and hash-like structures are *really* useful to beginners
    migrating from other languages.

The downside as someone mentioned is that a hash value of #f is disguised
by this technique.

The only good, backwards-compatible solution that springs to mind is to
have another predicate (like the exists operator in Perl) that returns
true or false depending only on whether the key exists in the table or
not, and not its value.

For example:

    (define cat-alist
      (list (cons 'zeus 'orange)
            (cons 'moy  'black) ))

    (assq-ref cat-alist 'moy)   ;; returns 'black
    (assq-ref cat-alist 'anna)  ;; returns #f as currently defined

    (assq-ref-exists cat-alist 'anna)  ;; returns #f to indicate nonextant
    (assq-ref-exists cat-alist 'zeus)  ;; returns #t, not 'zeus

Humbly,

Andrew

----------------------------------------------------------------------
Andrew Ho               http://www.tellme.com/       andrew@tellme.com
Engineer                   info@tellme.com          Voice 650-930-9062
Tellme Networks, Inc.                                 Fax 650-930-9101
----------------------------------------------------------------------


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