From: Richard J. <ri...@an...> - 2006-03-08 15:33:26
|
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 |