Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Getting an element of a hashtable: simple ... or is it?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-08-05 (12:33)
From: Brighten Godfrey <pbg@c...>
Subject: Re: [Caml-list] Getting an element of a hashtable: simple ... or is it?
On Aug 5, 2008, at 5:16 AM, Richard Jones wrote:

> On Tue, Aug 05, 2008 at 05:05:46AM -0700, Brighten Godfrey wrote:
>> Suppose you are given a data structure, and you want to retrive one
>> element -- any one element.  Sounds simple... and it is, if you have
>> a list (List.hd list) or an array (arr.(0)).  But how about a
>> hashtable, if we don't know a priori any of the keys in the  
>> hashtable?
> It's very unclear what you're trying to do.

I think you've interpreted me correctly.  I want a function that,  
given a hashtable, returns any element of the hashtable (assuming  
it's not empty).  This is the same as the function "choose" in the  
Set module.

> For List and Array those methods won't work if the data structure is
> empty, but I guess that's expected.  Hashtbl isn't designed with the
> "get an/any element" usage in mind -- your loop/exception is probably
> the best way given that you've made a poor choice of data structure in
> the first place.  But this still comes back to the question, what are
> you trying to do?

A hashtable is not a poor choice of a data structure, because this  
"choose" functionality is not the only requirement for my data  
structure: I also want constant time search.  OCaml's Set data  
structure has O(log n)-time search.