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

Re: Tables returning nil if no value



>     One thing that bothers me is that table-entries that have never been
>     defined return nil.  I often want to discriminate between values that
>     have never been set and ones that have been set to nil.  Shouldn't
>     uninitialized table-entries return some undefined value instead of nil?

> The problem is that *any* value could be stored in the table, including
> "some undefined value".

> The correct solution is to return multiple values such as the following:

>         (table-entry table key) => found?, value

I hesitate to offer an alternative to "the correct solution", but in our
hash table (HT) package here at UCLA, we have nil as the undefined value and
an additional function, HT:KEY? that returns true if the key is defined in
the table (even with a nil value) and false otherwise.  This has the advantage
that you can ignore the defined/undefined issue if you'd like.  In general,
our solution has been to have the application (built on top of HT) decide
about the defined/undefined question.

As far as what functions I'd like in a table package, here are the ones
that we have in HT:

;  Functions intended for use by mortal beings
;
;  HT:CREATE              HT?               HT:REMOVE       HT:PAIRS
;  HT:ENTRY               HT:PAIR           FREE            HT:KEY?
;  HT:WALK                DUMP              HT:KEYS         COPY
;  HT:MAP                 HT:ADD            HT:RECORDS      HT:COMBINE
;  HT:FIND-ENTRY          HT:UPDATE         HT:COUNT

The unobvious ones:

        HT:FIND-ENTRY -- returns the first pair that satisfies a predicate.
        HT:ADD/UPDATE -- add assumes the key is new, update the key is old.

DUMP, FREE and COPY are generic operations defined on many of the packages
we have here.   DUMP is a debug print-out, FREE releases an object to
storage management, and COPY returns a copy of the object.  COPY in
particular is ill-defined and is slowly going away.

Ashwin has a copy of this code if you'd care to look at it.  It is largely
based on the original HT code.  The default hashing function is OBJECT-HASH,
but the user can define another (as well as the equality predicate) in the
call to HT:CREATE.

                                        -- Scott Turner