
Why doesn't the following code produce a syntax error? I know why, given the absence of a syntax error, it's an infinite loop, but I don't understand why there isn't an error on the missing left hand side of the assignment operator
Ken Walker provided the answer: Assignment in Icon has the formwhile next := read() do write(next)
whereexpr1 := expr2
expr1 and expr2 can be any expressions.
The control structure next is an expression, so the program
fragment above is syntactically correct, with expr1 being
next. Of course, when this expression is evaluated, it transfers
control back to the beginning of the while loop.next
is a perfectly reasonable mnemonic for the intended use. And it's easy to
forget that Icon is an expression-based language -- no statements, just
expressions. That's useful in many situations, but it allows some ridiculous
expressions to be syntactically correct.
procedure main(arg)
limit := integer(arg[1]) | 30
every write(genprime(limit)) # typical usage
end
procedure genprime(limit)
# This is a list of 14 fractions that encode a
# FRACTRAN program for computing prime numbers.
f := list(14)
f[ 1] := fract(17,91)
f[ 2] := fract(78,85)
f[ 3] := fract(19,51)
f[ 4] := fract(23,38)
f[ 5] := fract(29,33)
f[ 6] := fract(77,29)
f[ 7] := fract(95,23)
f[ 8] := fract(77,19)
f[ 9] := fract( 1,17)
f[10] := fract(11,13)
f[11] := fract(13,11)
f[12] := fract(15, 2)
f[13] := fract( 1, 7)
f[14] := fract(55, 1)
# Construct a table of the powers of two.
pot := table()
power := 1
every n := 0 to limit - 1 do {
pot[power] := n
power *:= 2
}
# Generate the primes.
s := 2
repeat {
# Multiply s by the first fraction that produces an
# integer result.
x := !f & (s * x.numer) % x.denom = 0
s := (s * x.numer) / x.denom
# The exponent is prime if s is a power of two;
# if so, produce it.
suspend \pot[s]
}
end