Do programming languages ​​have sequential interpretations of {1, ..., n} when n = 0?

In math.SE, the question of mathematical notation discussed the question of how programming languages ​​interpret the set {1, ..., n} when n = 0

The question is given a mathematical notation for representing the R-code 1:n

According to the comments, the mathematical interpretation of {1, ..., n} for n = 0 is that it is an empty set. The following comment suggested that C is consistent with this interpretation because for (int i = 1; i < n; i++) returns an empty set because it iterates 0 times.

It is not clear to me that there is an equivalent instruction in R, but 1:0 returns the vector [1,0]

So for (i in 1:0) print(i) iterates over 1 and 0 (I interpret it like C code above)

Is this because {1, ..., n} is not the correct notation for 1:n ?

Does this mean that R violates the universal rule?

Is there a consistent interpretation of this set among programming languages?

+4
source share
4 answers

Each mathematical formalism has its own notation. Assuming that there is a "universal notation" is very "non-mathematical." Look at the notation associated with tensors or groups if you want examples of mathematical areas in which several notation systems exist.

In R, the code x <- 1:0 returns the ordered vector c (1,0). Just like the code x <- 2: -2 returns c (2,1,0, -1, -2). The code x <- seq(1, length=0) returns a sequence of length 0, which is printed in console sessions as integer(0) . R is not really intended to simulate a set of notation, but it does have some set functions, as well as packages that more fully implement standard notation.

+8
source

C has no idea about the set in which the for loop executes. Loop for for(a;b;c) d; is just syntactic sugar for:

 a; loop: if (!b) goto done; d; c; goto loop; done: ; 
+4
source

See also my answer at: The construction of a sequence that creates an empty sequence if the lower bound is greater than the upper bound - in R, seq_len(n) should be used in preference 1:n for this reason (the latter does not work incorrectly for n=0 )

+4
source

some languages ​​support the concept of ranges, in C this is what you do for the for loop, you can make it a value of 0, or you could consider it the opposite. In other languages, a range that has a second number less than the first often leads to a decrease in the number sequence. But its arbitrary, and there is no universal rule.

+3
source

Source: https://habr.com/ru/post/1416494/


All Articles