logo       
Google Custom Search
    AddThis Social Bookmark Button
-->

A good structure for counting things (?): msg#00005

Subject: A good structure for counting things (?)
This is a problem I often have - count "things" in an imperative way.
As an example, read a document and count the frequency of each word in
the document.

The way I normally solve it is something like this:

  let results = Hashtbl.create 31 in
  List.iter (
    fun word ->
      try
        let count = Hashtbl.find results word in
        Hashtbl.replace results word (count+1)
      with Not_found ->
        Hashtbl.add results word 1
  ) words;
  let results =
    Hashtbl.fold (fun word count xs -> (count, word) :: xs) results [] in
  (* ... *)

It's not particularly elegant ...

Is there a better structure that I should be using, or should we add
one to Extlib?

Rich.

PS. Note that "words" is only an example.  In real life I'm processing
gigabytes of "things", and they don't live in a convenient list in
memory either -- hence the imperative approach.

-- 
Richard Jones, CTO Merjis Ltd.
Merjis - web marketing and technology - http://merjis.com
Team Notepad - intranets and extranets for business - http://team-notepad.com


Archives up to August 22, 2005 are also downloadable at 
http://www.connettivo.net/cntprojects/ocaml_beginners/
The archives of the very official ocaml list (the seniors' one) can be found at 
http://caml.inria.fr
Attachments are banned and you're asked to be polite, avoid flames etc. 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/ocaml_beginners/

<*> To unsubscribe from this group, send an email to:
    ocaml_beginners-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 






<Prev in Thread] Current Thread [Next in Thread>