# -*- mode: icon -*-
# $Id: wordfreq.icon,v 1.1 2000/12/25 06:58:04 doug Exp $
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
    local wcount, buckets, w, c, pair, wl

    wcount := table(0)
    every wcount[words()] +:= 1

    buckets := table()
    every w := key(wcount) do {
    c := wcount[w]
    / buckets[c] := list()
    push(buckets[c], w)
    }
    buckets := sort(buckets,1)
    while pair := pull(buckets) do {
     c := pair[1]
     wl := sort(pair[2],1)
    while (write(right(c,7), "\t", pull(wl)))
    }
end

procedure words()
    local line, word
    while line := read() do {
    line := map(line)
    line ? while tab(upto(&letters)) do {
        word := tab(many(&letters))
        suspend(word)
    }
    }
end