Icon+Unicon/Intro: Difference between revisions

Content added Content deleted
m (→‎Binary (and Augmented): fix hanging tag)
(→‎Contractions: + extreme)
Line 599: Line 599:
}</lang>
}</lang>


=== Extreme Contractions ===
Deciding on the level of contraction is an art and, like any art form, easy to abuse.

For example, the following procedure (from the Van de Corput Sequence task solution):
Deciding on the level of contraction is an art and, like any art form, easy to abuse. For example, the following procedure (from the Van de Corput Sequence task solution):
<lang Unicon>procedure vdc(n, base)
<lang Unicon>procedure vdc(n, base)
e := 1.0
e := 1.0
Line 613: Line 614:
end</lang>
end</lang>
This is probably only lovable by J programmers.
This is probably only lovable by J programmers.

Here is another example taken from [[Sierpinski_carpet|Sierpinski Carpet]] which we will explain:

The procedure IsFilled is used to determine if the carpet is to be filled and is a fairly straightforward translation of Java which in turn was translated from Python. About the only natural contraction for the language is the use of the fact that operators return values and comparisons can be chained.<lang Icon>procedure IsFilled(x,y)
while x ~= 0 & y ~= 0 do {
if x % 3 = y %3 = 1 then fail
x /:= 3
y /:=3
}
return
end</lang>

Taken to extremes we can write the following:<lang Icon>procedure IsFilled2(x,y)
every (x *:= 3, y *:= 3, |((1 = (x /:= 3) % 3 = (y /:= 3) %3,fail)|next)) | return
end</lang>

How did we get to this point:
* every proceeds evaluating expressions until a result can generate more values via alternation (x|y) or (|expr), generation (!x), or generators. Everything before the (|x) is evaluated only once. So this example initializes by multiplying x and y by 3 before iterating and reducing them by 3 each loop. Reduction by 3 and modulus checking are all within a single expression that if successful fails the procedure.
* "every expr | return" removes a line break


= Run Time Considerations =
= Run Time Considerations =