Sorry for the spam here :sweat_smile:, but I am doing the exercism track and finding out some things.
This time I was using the List.range
function with the optional step parameter. (A satisfyingly clean way to get a list of multiples of a number under a given limit for the sum-of-multiples exercise). However when the step is 0 I found out it behaves as if the step is actually 1. E.g. List.range { start: At 0, end: At 5, step: 0 } == [0, 1, 2, 3, 4, 5]
. I don't know what the result should be (empty list, singleton list with only the start, ...), but I was wondering if this is the intended behavior.
I'm not sure why but in the implementation we default to 0 (if it is not specified) for the step size:
range = \{ start, end, step ? 0 } ->
{ calcNext, stepIsPositive } =
if step == 0 then
I think it would be slightly more consistent if it returned empty list when the step is 0. But I’m not sure how much that matters. Are there scenarios when someone would actually want it to return empty list?
Anton said:
I'm not sure why but in the implementation we default to 0 (if it is not specified) for the step size:
range = \{ start, end, step ? 0 } -> { calcNext, stepIsPositive } = if step == 0 then
A default step size of 1 would make more sense to me
Are there scenarios when someone would actually want it to return empty list?
If someone actually provided a step of 0, something is probably wrong with their code and they should know about it...
Now that ?
is so convenient to use it doesn't seem crazy to return a result here :thinking:
Anton said:
Are there scenarios when someone would actually want it to return empty list?
If someone actually provided a step of 0, something is probably wrong with their code and they should know about it...
For the specific problem I had, the empty list worked, but in general yeah, it probably is a bug
Maybe returning empty list when the step is zero would make it clear enough that something is off without requiring the return type to be a Result
Hmm, I don't know, it could lead to the user getting an empty list of items on a website for example, without any errors being logged
That is true
It just pains me a little bit to add a result when most of the time step isn’t even passed in to the function
But perhaps it’s still the best thing to do
Zero is default step in that code
It is expected a user will never provide zero, so we use it to infer the step
That way, we dont have to make step some sort of tag and the entire function more verbose
So this was an intentional design decision
This is important for reverse ranges just working. {start: At 5, end: At 0}
That is at least the background context for why it is that way. I think it would be reasonable to propose an alternative in #ideas, but it isn't a bug
this is just because we can't currently do the following, right?
\{ start, end, step ? defaultStep start end } ->
...and then defaultStep
can look at start
and end
and return a step based on their values
that seems like what we actually want, but if I remember right the compiler doesn't support that right now
Do we plan to support that? Calling a function to determine a default value?
But yeah, that would be fine too and more clear
ideally I'd like it to be the case that after the ?
you can give any expression, and that expression is also allowed to use anything from earlier in the destructure
(but not later in the destructure, becuase that could create circular defaults)
That would be nice
I thought we had an issue for this somewhere but maybe not? :sweat_smile:
I remember it being mentioned in ideas a long time ago
Last updated: Jul 06 2025 at 12:14 UTC