[The Original Shootout]   [NEWS]   [FAQ]   [Methodology]   [Platform Details]   [Acknowledgements]   [Scorecard]
All Source For icon
Ackermann's Function
```# -*- mode: icon -*-
# \$Id: ackermann.icon,v 1.2 2000/12/17 23:34:17 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
num := argv[1];
write("Ack(3,", num, "): ", ack(3,num));
end

procedure ack(m,n)
if (m == 0) then {
return(n + 1)
}
if (n == 0) then {
return(ack(m-1, 1))
}
return(ack(m-1, ack(m, n-1)))
end
```
Array Access
```# -*- mode: icon -*-
# \$Id: ary3.icon,v 1.1 2001/05/31 02:27:48 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
# local n, i, k, x, y, last
n := argv[1] | 1
x := list(n,0)
y := list(n,0)
every i := 1 to n do {
x[i] := i
}
every k := 0 to 999 do {
every i := n to 1 by -1 do {
y[i] +:= x[i]
}
}
write(y[1], " ", y[n])
end
```
Count Lines/Words/Chars
```# -*- mode: icon -*-
# \$Id: wc.icon,v 1.1 2001/05/15 06:21:02 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
local nl, nw, nc, nonspaces
nl := nw := nc := 0
nonspaces := ~' \t'
while line := read() do line ? {
nl +:= 1
nc +:= 1 + *line
while tab(upto(nonspaces)) do {
nw +:= 1
tab(many(nonspaces))
}
}
write(nl, " ", nw, " ", nc)
end
```
Fibonacci Numbers
```# -*- mode: icon -*-
# \$Id: fibo.icon,v 1.2 2000/12/24 19:10:50 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
n := argv[1] | 1;
write(fib(n));
end

procedure fib(n)
if (n < 2) then { return(1) }
return(fib(n-2) + fib(n-1))
end
```
Hash (Associative Array) Access
```# -*- mode: icon -*-
# \$Id: hash.icon,v 1.1 2000/12/18 03:10:54 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure hexstring(i)
local s
if i = 0 then s := "0"
else {
s := ""
while i ~= 0 do {
s := "0123456789abcdef"[iand(i,15) + 1] || s
i := ishift(i,-4)
}
}
return s
end

procedure main(argv)
local n
local X
local c
local i
n := argv[1] | 1
X := table(0)
c := 0
every i := 1 to n do {
X[hexstring(i)] := i
}
every i := n to 1 by -1 do {
if (member(X,string(i))) then { c +:= 1 }
}
write(c)
end
```
Hashes, Part II
```# -*- mode: icon -*-
# \$Id: hash2.icon,v 1.1 2000/12/30 06:30:12 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
n := argv[1] | 1
hash1 := table(0)
hash2 := table(0)
every i := 0 to 10000 do {
hash1["foo_"||string(i)] := i
}
every i := 1 to n do
every k := key(hash1) do
hash2[k] +:= hash1[k]
write(hash1["foo_1"], " ", hash1["foo_9999"], " ",
hash2["foo_1"], " ", hash2["foo_9999"])
end
```
Heapsort
```# -*- mode: icon -*-
# \$Id: heapsort.icon,v 1.1 2001/05/08 02:46:59 doug Exp \$
# http://www.bagley.org/~doug/shootout/

\$define IM 139968
\$define IA 3877
\$define IC 29573

procedure gen_random(max)
static LAST; initial LAST := 42;
repeat { suspend((max * (LAST := (LAST * IA + IC) % IM)) / IM) }
end

procedure heapsort(n, ra)
local l, j, ir, i, rra

l := ishift(n, -1) + 1
ir := n
repeat {
if (l > 1) then {
l := l - 1
rra := ra[l]
} else {
rra := ra[ir]
ra[ir] := ra[1]
ir := ir - 1
if (ir == 1) then {
ra[1] := rra
return
}
}
i := l
j := ishift(l, 1)
while (j <= ir) do {
if ((j < ir) & (ra[j] < ra[j+1])) then {
j := j + 1
}
if (rra < ra[j]) then {
ra[i] := ra[j]
i := j
j := j + i
} else {
j := ir + 1
}
}
ra[i] := rra
}
end

procedure main(argv)
n := argv[1] | 1
ary := list(n)
every i := 1 to n do ary[i] := gen_random(1.0)
heapsort(n, ary)
write(ary[n])
end

```
Hello World
```# -*- mode: icon -*-
# \$Id: hello.icon,v 1.2 2001/06/18 01:04:15 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
write("hello world")
end
```
Nested Loops
```# -*- mode: icon -*-
# \$Id: nestedloop.icon,v 1.1 2000/12/30 21:42:57 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
local n, a, b, c, d, e, f, x
n := argv[1] | 1;
x := 0
every a := 1 to n do
every b := 1 to n do
every c := 1 to n do
every d := 1 to n do
every e := 1 to n do
every f := 1 to n do
x +:= 1
write(x)
end
```
Random Number Generator
```# -*- mode: icon -*-
# \$Id: random.icon,v 1.2 2001/05/08 01:51:39 doug Exp \$
# http://www.bagley.org/~doug/shootout/

\$define IM 139968
\$define IA 3877
\$define IC 29573

procedure gen_random(max)
static LAST; initial LAST := 42;
repeat { suspend((max * (LAST := (LAST * IA + IC) % IM)) / IM) }
end

procedure main(argv)
n := argv[1] | 1;
every i := 2 to n do gen_random(100.0)
# Icon has fixed number of output decimal points to 9, bogus!
write(gen_random(100.0))
end

```
Reverse a File
```# -*- mode: icon -*-
# \$Id: reversefile.icon,v 1.1 2000/12/25 04:33:31 doug Exp \$
# http://www.bagley.org/~doug/shootout/

# Author:   Ralph E. Griswold
# (http://www.cs.arizona.edu/icon/library/src/progs/revfile.icn)

procedure main()
local lines
lines := []
every push(lines, !&input)
every write(!lines)
end
```
Sieve of Erathostenes
```# -*- mode: icon -*-
# \$Id: sieve.icon,v 1.3 2000/12/17 23:34:06 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
n := argv[1] | 1
every i := 1 to n do count := sieve()
write("Count: ", count)
end

# algorithm from a test program that is distributed with
# the icon source

procedure sieve()
local limit, s, i
limit := 8192
s := set()
every insert(s,1 to limit)
every member(s,i := 2 to limit) do
every delete(s,i + i to limit by i)
delete(s,1)
return(*s);
end
```
Spell Checker
```# -*- mode: icon -*-
# \$Id: spellcheck.icon,v 1.1 2001/01/28 03:20:33 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
local dict
n := argv[1] | 1
dict := table(0)
f := open("Usr.Dict.Words") | {
write(&errout, "Can't open \"Usr.Dict.Words\"")
fail
}
while line := read(f) do line ? {
dict[line] := 1
}
close(f)
while line := read() do line ? {
if (not member(dict,line)) then {
write(line)
}
}
end

```
String Concatenation
```# -*- mode: icon -*-
# \$Id: strcat.icon,v 1.1 2000/12/17 23:46:59 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
n := argv[1] | 1;
str := "";
every i := 1 to n do str ||:= "hello\n";
write(*str);
end
```
Sum a Column of Integers
```# -*- mode: icon -*-
# \$Id: sumcol.icon,v 1.2 2000/12/17 23:40:20 doug Exp \$
# http://www.bagley.org/~doug/shootout/

procedure main(argv)
sum := 0
write(sum)
end
```
Word Frequency Count
```# -*- 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
```