Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not use the Unicode characters √ and ∀ in assignments?

Julia has an interesting support for Unicode characters. The syntax allows me to run the following in the iJulia notebook.

μ = 10
σ = 20
∑ = sum
a = [1,2,3,4] 
∑(a) # prints 10

However, it doesn't always seem like you can use Unicode characters for function/variable names.

sqrt √ example

√ = sqrt

Gives the following error:

syntax: unexpected "="

forall ∀ example

forall(x,f) = [f(i) for i in x]
∀ = forall

Gives the following error:

syntax: invalid character "∀"

Why?

Am I misunderstand the rules? I assumed that a Unicode character would be interpreted just like any other alphabetical character, but I seem to get two different errors on seemingly simple statements.

like image 892
cantdutchthis Avatar asked Feb 11 '15 17:02

cantdutchthis


People also ask

How do I enable Unicode?

Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X.

How do I insert a Unicode character in Word?

Inserting Unicode CharactersType the character code where you want to insert the Unicode symbol. Press ALT+X to convert the code to the symbol. If you're placing your Unicode character immediately after another character, select just the code before pressing ALT+X.

What is a Unicode character code?

The Unicode character encoding standard is a fixed-length, character encoding scheme that includes characters from almost all of the living languages of the world. Information about Unicode can be found in The Unicode Standard , and from the Unicode Consortium website at www.unicode.org.

How do you use Unicode characters in Python?

In Python, the built-in functions chr() and ord() are used to convert between Unicode code points and characters. A character can also be represented by writing a hexadecimal Unicode code point with \x , \u , or \U in a string literal.


2 Answers

is the name of an existing function; try running the following:

julia> methods(√)
# 12 methods for generic function "sqrt":
sqrt(a::Complex{Float16}) at float16.jl:141
sqrt{T<:FloatingPoint}(z::Complex{T<:FloatingPoint}) at complex.jl:237
sqrt(z::Complex{T<:Real}) at complex.jl:261
...

As for , it is not a valid character in Julia source code (yet?). See this discussion; what transpires is that the developers are still considering whether to make a valid character, and if they do, what meaning it would have.

like image 84
jub0bs Avatar answered Oct 21 '22 01:10

jub0bs


I think the problem is that these functions are already defined.

See the link some of the characters that have meanings built into Julia: https://github.com/JuliaLang/julia/blob/d234b4ff56df9ce85198dcdd8d9ef1073b2436fc/base/operators.jl#L403-L432 (note, not all of these are unicode and there might be others in a different file in base -- but this at least covers your sqrt example)

like image 20
spencerlyon2 Avatar answered Oct 21 '22 01:10

spencerlyon2