3 ms·
coming from total unawareness of zig: in the for (1..5) construct, these integer ranges consistently not including the upper limit element when lists do include
by Terretta 4y ago
coming from total unawareness of zig: in the for (1..5) construct, these integer ranges consistently not including the upper limit element when lists do include the last element, seems surprising. i guess it's a range boundary (1 TO 5), not a list (1 THROUGH 5), but the other behavior feels like a list, so it feels like 5 should be in.
- throwawaymaths 4y agoIt's consistent with (some) other languages, for example iirc in ruby .. is exclusive of the last item and ... Includes the last item.
- cmoski 4y agoI completely agree with you on the madness of not including the upper limit. However, I don't see how the phrase "one to five" would not include five. "Rate this film on a scale of one to five" does not mean four is the highest rating. It translates to "increment from one, stopping before you get to five". Ridiculous.
- kzrdude 4y agoIt's not ridiculous, "1 to 5" translates into it starts with 1 and ends with 5, and both versions are ambiguous on the point of including the endpoint or not. In a programming context, it seems "clear" that it's ambiguous or down to convention.
- Wildgoose 4y agoDon't think of it as TO, think of it as increasing UNTIL. 1 increasing UNTIL 5. It is genuinely recommended (Djikstra onwards) to specify ranges as Start Value (inclusive) Until Final Value (exclusive).