Icon+Unicon/Intro: Difference between revisions

m
moved contractions
m (moved contractions)
m (moved contractions)
Line 179:
 
Icon/Unicon performs implicit type conversions (called coercion) where it makes sense to do so. At the same time dangerous coercions are disallowed. Details of these coercions are covered under the topic of [[Icon+Unicon/Intro#DataTypes|Data Types]]. Where a coercion is not possible, a run-time error is generated.
 
=== Contractions ===
Icon/Unicon have a rich set of operators which combined with the fact that all successful expressions produce values makes possible contractions. These appeal of these is somewhat a question of style and if taken to extremes there is also the possibility of being overtaken by one-liners. Having said that Icon/Unicon contractions are hardly in the same league as those of [[APL]] or [[J]]. A number of examples are presented below.
 
 
These examples initializes sum and adding all the contents of arglist.
<lang Icon>sum := 0 # initial and
every sum +:= !arglist do something() # loop in two statements
 
sum := 0; every sum +:= !arglist do something() # ; splice
 
every (sum := 0) +:= !arglist do something() # a common contraction
 
while sum := 0) +:= !arglist do something() # an error</lang>
 
Examples of using program control structures in expressions:
<lang Icon>
(if i > j then i else j) := 0 # sets the larger of i and j to 0
d := if a > b then a-b else b-a # sets d to the positive difference of a and b
x := case expr of {
1: "Text 1"
2: "Text 2"
default: "Undefined text"
} # sets x to a string based on the value of expression</lang>
 
== Program Flow ==
Anonymous user