Stream: beginners

Topic: List.range behavior when step is 0


view this post on Zulip Kilian Vounckx (Sep 07 2024 at 11:50):

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.

view this post on Zulip Anton (Sep 07 2024 at 11:57):

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

view this post on Zulip Isaac Van Doren (Sep 07 2024 at 11:58):

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?

view this post on Zulip Kilian Vounckx (Sep 07 2024 at 12:00):

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

view this post on Zulip Anton (Sep 07 2024 at 12:04):

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...

view this post on Zulip Anton (Sep 07 2024 at 12:05):

Now that ? is so convenient to use it doesn't seem crazy to return a result here :thinking:

view this post on Zulip Kilian Vounckx (Sep 07 2024 at 12:07):

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

view this post on Zulip Isaac Van Doren (Sep 07 2024 at 12:10):

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

view this post on Zulip Anton (Sep 07 2024 at 12:24):

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

view this post on Zulip Isaac Van Doren (Sep 07 2024 at 14:16):

That is true

view this post on Zulip Isaac Van Doren (Sep 07 2024 at 14:17):

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

view this post on Zulip Isaac Van Doren (Sep 07 2024 at 14:17):

But perhaps it’s still the best thing to do

view this post on Zulip Brendan Hansknecht (Sep 07 2024 at 15:46):

Zero is default step in that code

view this post on Zulip Brendan Hansknecht (Sep 07 2024 at 15:46):

It is expected a user will never provide zero, so we use it to infer the step

view this post on Zulip Brendan Hansknecht (Sep 07 2024 at 15:46):

That way, we dont have to make step some sort of tag and the entire function more verbose

view this post on Zulip Brendan Hansknecht (Sep 07 2024 at 15:47):

So this was an intentional design decision

view this post on Zulip Brendan Hansknecht (Sep 07 2024 at 15:48):

This is important for reverse ranges just working. {start: At 5, end: At 0}

view this post on Zulip Brendan Hansknecht (Sep 07 2024 at 15:49):

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

view this post on Zulip Richard Feldman (Sep 07 2024 at 15:50):

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

view this post on Zulip Richard Feldman (Sep 07 2024 at 15:50):

that seems like what we actually want, but if I remember right the compiler doesn't support that right now

view this post on Zulip Brendan Hansknecht (Sep 07 2024 at 15:50):

Do we plan to support that? Calling a function to determine a default value?

view this post on Zulip Brendan Hansknecht (Sep 07 2024 at 15:51):

But yeah, that would be fine too and more clear

view this post on Zulip Richard Feldman (Sep 07 2024 at 15:51):

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

view this post on Zulip Richard Feldman (Sep 07 2024 at 15:51):

(but not later in the destructure, becuase that could create circular defaults)

view this post on Zulip Brendan Hansknecht (Sep 07 2024 at 15:53):

That would be nice

view this post on Zulip Richard Feldman (Sep 07 2024 at 15:55):

I thought we had an issue for this somewhere but maybe not? :sweat_smile:

view this post on Zulip Brendan Hansknecht (Sep 07 2024 at 15:57):

I remember it being mentioned in ideas a long time ago


Last updated: Jul 06 2025 at 12:14 UTC