Icon code

T T L Davidson (lnp6ttld@gps.leeds.ac.uk)
Thu, 20 Jul 1995 16:34:14 +0100

>
>
> I've heard some good reports about Icon here, but I still
> don't have a good idea about what it *is*. Can someone
> tell me, specifically, why I should use Icon over, say,
> Perl? As it is, I count word frequencies in Perl with
> a simple program like:
>
> ---------------------------------------------------------
> while (<>) {
> @words = split(/\s+/, $_);
> foreach $word (@words) {
> $wordcount{$word}++; # Increment the entry.
> }
> }
> # Now print out all the entries in the %wordcount array.
> foreach $word (sort keys(%wordcount)) {
> print "$wordcount{$word}\t$word\n";
> }
> ---------------------------------------------------------
>
> Is this easier in Icon? As easy? Can someone submit some Icon
> code to demonstrate?
>
> I don't mean to pick on word counting, but I suppose it's
> a simple exercise that illustrates some features of a
> language that's good at lexical processing.
>
> Thanks.
>
> --
> Jeff Adams, Language Modeling Scientist jeffa@kurz-ai.com
> Kurzweil Applied Intelligence
> Waltham, MA (617) 893-5151 x339
>
Here is something similar in Icon.

procedure main()
vocab := table(0)
while line := read() do {
line ? while tab(upto(&letters)) do
vocab[tab(many(&letters))] +:= 1
}
t := sort(vocab,3)
while write(get(t),"\t",get(t))
end
* * * * * * * * * *

T T L Davidson

Tel: 0113 233 3565 e-mail: T.T.L.Davidson@leeds.ac.uk
Fax: 0113 233 3566

Dept of Linguistics & Phonetics
University of Leeds
Leeds
LS2 9JT
UK

* * * * * * * * * *