Two questions about Julia

Hello,
I have two questions:
1- Why can’t I assign '' to a variable?

julia> ch = ''
ERROR: ParseError:
# Error @ REPL[17]:1:7
ch = ''
#     └ ── empty character literal
Stacktrace:
 [1] top-level scope
   @ none:1

2- Why doesn’t Julia have a reverse for loop?

for h = 10 : 1

I know that this problem can be solved as follows:

for h = last : -1 : first

Thank you.

This isn’t about assigning to a variable. The problem is that '' itself isn’t valid syntax. When you write 'a' that is syntax specifically for the single character a, not a string of characters. If you want to write an empty string you can do

julia> ch = ""
""

which creates an empty string. There is no definitive empty character, so '' is made to be a syntax error instead.

If you already know how to construct a reversed loop, then I’m not sure what to say. Yes, the second thing you wrote is the correct way to express a loop from last down to first.

The first thing you wrote is a loop over an empty range:

julia> length(10:1)
0

julia> length(10:-1:1)
10

You could also write reverse(1:10), which is considered identical:

julia> reverse(1:10) == 10:-1:1
true

See The colon punctuation: Why doesn't 5:1 return [5, 4, 3, 2, 1]?

It would obviously be way too breaking, but I wonder if 10:1 should be 10:-1:1 and there should be a different way to ask for the empty range starting at 10. Our reasoning is principled but I don’t think anyone new to the language understands it.

Wouldn’t this be inherently type unstable? And that would be unfortunate for something so commonly used as ranges.

Also, what languages actually returns a reverse unit range for 10:1? Not Matlab, and not numpy.

Right, that was discussed in the thread I linked above.

(There’s also the issue of representing empty ranges like 5:4, which are useful for searches etc.)

Hello,
Thank you so much for your reply.
What is the use of empty range?

Hello,
Please take a look at this code in C++ programming language:

#include <iostream>

using namespace std;

int main() {
        for (int i = 10; i >= 1; i--) {
        cout << i << " ";
    }
    return 0;
}

The same as the use of empty strings, empty tuples, or empty vectors. They come up often when programmatically building containers in generic code.

That’s a semicolon loop, essentially syntactic sugar for a while loop. The syntax does not exist in many languages, which often opt instead for foreach loops. That applies to Julia. A macro can replicate the C-style loop, but I’d probably just stick to let and while.

@DNF: R reverses ranges in this fashion and I have been bitten by that several times.
@hack3rcon: Another nice thing about empty ranges is that they are valid for indexing, i.e., the following works as expected (at least that’s what I would expected, see reversed ranges above):

dropthree(x) = x[4:end]
dropthree.([[1,2], [1,2,3], [1,2,3,4]])

The loop for (int i = 10; i >= 1; i--) in C is always explicit about the step, via i++, i-- or i+=..., except that its hidden under a bit of boilerplate. I.e., the closest translations to Julia would be for i in 1:1:10 for counting up or for i in 10:-1:1 for counting down.