[plt-scheme] help on how to write a frequency-counting function in a more functional way

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Apr 19 13:59:36 EDT 2009

I forgot about functional hash:

  vector: cpu time: 3580 real time: 3626 gc time: 0
  alist:  cpu time: 7784 real time: 8068 gc time: 1380
  bst:    cpu time: 15791 real time: 16052 gc time: 368
  hash:   cpu time: 7439 real time: 7646 gc time: 456

Not bad. -- Matthias




On Apr 19, 2009, at 11:56 AM, Matthew Flatt wrote:

> At Sat, 18 Apr 2009 22:57:59 -0400, Matthias Felleisen wrote:
>>
>> Sigrid, just to get a first idea, I coded up the four most natural
>> solutions to this problem with a simple stress test and timed them:
>>
>>   -- vector
>>   -- sort and count (PR's suggestion)
>>   -- association list
>>   -- binary search tree
>
> Here's one more, using a functional "hash table":
>
>  (define (cnt-ht f)
>     (cnt-fp f
>             (lambda () #hash())
>             (lambda (ht freq)
>               (hash-update ht freq add1 0))))
>
> The performance is close to the binary-tree performance, probably
> because that's how functional hash tables are implemented internally
> (but as red-black trees, so they're always balanced).
>



Posted on the users mailing list.