caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@janestcapital.com>
To: Sam Steingold <sds@gnu.org>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Re: modifying hash tables
Date: Wed, 18 Jul 2007 12:36:09 -0400	[thread overview]
Message-ID: <469E4179.90506@janestcapital.com> (raw)
In-Reply-To: <469E396D.7010508@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1347 bytes --]

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


[-- Attachment #2: Type: text/html, Size: 1922 bytes --]

  reply	other threads:[~2007-07-18 16:36 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-18 15:27 Sam Steingold
2007-07-18 16:00 ` [Caml-list] " Christopher L Conway
2007-07-18 16:01 ` Sam Steingold
2007-07-18 16:36   ` Brian Hurt [this message]
2007-07-18 16:08 ` [Caml-list] " David Allsopp
2007-07-18 16:38 ` Zheng Li
2007-07-18 20:38 ` [Caml-list] " Jon Harrop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=469E4179.90506@janestcapital.com \
    --to=bhurt@janestcapital.com \
    --cc=caml-list@inria.fr \
    --cc=sds@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).