Loops/Wrong ranges: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: disambiguate multiplication from */whatever)
(added langur language example)
Line 5: Line 5:
Some languages have syntax or function(s) to generate a range of numeric values from a start value, a stop value, and an increment.
Some languages have syntax or function(s) to generate a range of numeric values from a start value, a stop value, and an increment.


The purpose of this task is to select the range syntax/function that would generate at least two increasing numbers when given a stop value more than the start value and a positive increment of less than half the difference. You are than to use that ''same'' syntax/function but with different parameters; and show, here, what would happen.
The purpose of this task is to select the range syntax/function that would generate at least two increasing numbers when given a stop value more than the start value and a positive increment of less than half the difference. You are then to use that ''same'' syntax/function but with different parameters; and show, here, what would happen.


Use these values if possible:
Use these values if possible:
Line 690: Line 690:
Start equal stop equal zero: zero increment
Start equal stop equal zero: zero increment
Range(0, 0, 0) -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Range(0, 0, 0) -> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
</pre>

=={{header|langur}}==
{{trans|Python}}
{{workswith|langur|0.6.15}}

<lang langur>val .data = q:block END
start stop increment comment
-2 2 1 Normal
-2 2 0 Zero increment
-2 2 -1 Increments away from stop value
-2 2 10 First increment is beyond stop value
2 -2 1 Start more than stop: positive increment
2 2 1 Start equal stop: positive increment
2 2 -1 Start equal stop: negative increment
2 2 0 Start equal stop: zero increment
0 0 0 Start equal stop equal zero: zero increment
END

# Process .data string into .table.
var .table = map(f replace(.s, RE:m/^\s+/), split("\n", .data))
.table = map(f split(RE/\s\s+/, .s), .table)

for .i in 2..len(.table) {
.table[.i] = map(f { if(.i2==4: .s2; toNumber(.s2)) }, 1..4, .table[.i])
}

for .test in rest(.table) {
var (.start, .stop, .inc, .comment) = .test
{
val .series = series(.start to .stop, .inc)
catch {
writeln $"\.comment;\nERROR: \.err["msg"]:L200(...);\n"
} else {
writeln $"\.comment;\nresult: \.series;\n"
}
}
}</lang>

{{out}}
<pre>Normal
result: [-2, -1, 0, 1, 2]

Zero increment
result: []

Increments away from stop value
ERROR: Expected positive range with positive increment, or negative range with negative increment

First increment is beyond stop value
result: [-2]

Start more than stop: positive increment
ERROR: Expected positive range with positive increment, or negative range with negative increment

Start equal stop: positive increment
result: [2]

Start equal stop: negative increment
result: [2]

Start equal stop: zero increment
result: []

Start equal stop equal zero: zero increment
result: []
</pre>
</pre>