Version française
Home     About     Download     Resources     Contact us    
Browse thread
modifying hash tables
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Hurt <bhurt@j...>
Subject: Re: [Caml-list] Re: modifying hash tables
Sam Steingold wrote:

>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>Sam Steingold wrote:
>  
>
>>I want to modify the _current_ datum in Hashtable.iter, e.g., increment
>>a count. Is this code valid?
>>Hashtable.iter ht ~f:(fun ~key ~data ->
>>  Hashtable.replace ht ~key ~data:(data+1))
>>(it appears to work, but I was told that "it is not guaranteed to work",
>>i.e., the documentation does not promise that).
>>If the above code is wrong, what is TRT?
>>    
>>
>
>in case you wondered why I think my code is reasonable:
>http://www.lisp.org/HyperSpec/Body/sec_3-6.html
>
>Hash-table traversal
>
>    For hash table traversal operations, new elements may not be added
>or deleted except that the element corresponding to the current hash key
>may be changed or removed.
>
>  
>
See also C++'s STL iterators.

The fundamental problem here is that more gaurantees you give as to how 
things work as you mutate data structures as you iterate over them, the 
less flexibility you have in how those data structures can be 
implemented.  At the extreme, you have the "one true implementation", 
which is the only legal implementation.  At the other extreme, you have 
no gaurentees what so ever as to what will happen when you modify a data 
structure while iterating over it.

Lisp chose one trade-off, C++ another, and Ocaml a third.

Brian