This is the mail archive of the guile@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: Scheme style auto-resizing hashtable (fwd)


Jay Glascoe writes:
...
> > Destructive conversion functions are a bad idea and cannot
> > generalize. Taking advantage of the fact that a hash table is
> > fundamentally a cons cell will make things lose big if you ever change
> > your underlying implementation, for instance.
> 
> hmm.  I could make them destructive as well as functional.  I mean, "list"
> will still be destroyed -- it's finally reduced to (()) -- but rather than
> directly converting "list" to a hash-table, the new hash-table will be the
> return value of the function.  Would this be preferable to in-place
> transformation?  I like the idea, now they'll work fine with empty lists.

The opposite of imperative isn't functional.  It's the "!" which isn't 
functional.  Mutation/destruction is the opposite of functional -- it
just so happens that imperative style is useless without mutation
(after all, if you don't use the resulting value of a side-effect-free 
procedure, why call the procedure at all?)

You shouldn't be destroying the source collections, IMHO.  That's what
the garbage collector is for.  Not only is it easier for the garbage
collector to destroy the old list, it is safer.  Who knows what else
might be pointing into the middle of the list?  What else might be
depending on the list?  If there isn't anything else depending on it,
the garbage collector will do its work.  If there is then you
shouldn't be destroying the list.  The garbage collector is smart,
programmers have repeatedly been found not to be smart enough (since
they have to analyze these things before runtime).  Isn't something
like >50% of bugs result from mistakes in memory-management (in
non-GCed languages)...?

Maybe it's not optimally efficient, but then Scheme isn't C.  It's not 
supposed to be optimally efficient, it's supposed to be a little
safe.  Garbage collection is a big part of this.

> In addition to the space-saving virtues of the destructive functions, they
> also ensure that the new hash-table or list is the sole owner of the pairs
> contained in the old list or hash-table. (assuming nobody else is holding
> onto the pairs).

That last little caveat isn't to be ignored!


<------------------------------------------------------------------->
< Ian Bicking                 |  bickiia@earlham.edu                >
< drawer #419 Earlham College |  http://www.cs.earlham.edu/~bickiia >
< Richmond, IN 47374          |  (765) 973-2824                     >
<------------------------------------------------------------------->