Loops/Wrong ranges: Difference between revisions

Add Factor example
m (→‎{{header|Perl}}: now produces output)
(Add Factor example)
Line 122:
Start equal stop equal zero: zero increment
Range(0, 0, 0) -> [0 0 0 0 0 0 0 0 0 0]
</pre>
 
=={{header|Factor}}==
<code><range></code> divides by the step value, so a step of 0 causes a divide by zero exception. For the purpose of getting through all the examples, the exceptions are dropped and execution continues, which in general should be avoided.
<lang factor>USING: continuations formatting grouping io kernel math.ranges
prettyprint sequences ;
 
: try-range ( from length step -- )
[ <range> { } like . ]
[ 4drop "Exception: divide by zero." print ] recover ;
 
{
{ -2 2 1 } { 2 2 0 } { -2 2 -1 } { -2 2 10 } { 2 -2 1 }
{ 2 2 1 } { 2 2 -1 } { 2 2 0 } { 0 0 0 }
}
[
first3
[ "%2d %2d %2d <range> => " printf ]
[ try-range ] 3bi
] each</lang>
{{out}}
<pre>
-2 2 1 <range> => { -2 -1 0 1 2 }
2 2 0 <range> => Exception: divide by zero.
-2 2 -1 <range> => { }
-2 2 10 <range> => { -2 }
2 -2 1 <range> => { }
2 2 1 <range> => { 2 }
2 2 -1 <range> => { 2 }
2 2 0 <range> => Exception: divide by zero.
0 0 0 <range> => Exception: divide by zero.
</pre>
 
1,827

edits