Sentinel
Builtin Function: range
The built-in function range
returns a list of numbers in a range.
There are three ways to call this function:
range(end)
range(start, end)
range(start, end, step)
The start
is inclusive, the end
is exclusive.
If start
is not provided, it defaults to 0
. If step
is not provided, it
defaults to 1
.
Negative steps are permitted and count down from start
towards end
, instead
of up.
The range ends when the next value given by the sum of the last value and step
would exceed end
, regardless of if it has been reached.
range(5) // [0,1,2,3,4]
range(1, 5) // [1,2,3,4] - starts at 1 instead of 0
range(1, 5, 2) // [1,3] - 3+2 does not satisfy r[-1] < end
range(0, -3, -1) // [0,-1,-2] - example of negative range
Impossible ranges, or ranges where start
will never reach end
given step
,
yield an empty list.
range(1, 1) // [] - already starting at 1
range(0, 1, -1) // [] - negative step will never reach 1
Supplying an undefined value to any argument of range
yields an undefined
value back.
A range with a zero step is a runtime error.