Why does (sqrt ∘ +)(3,6) work but (√ ∘ +)(3,6) throws an error?

I was just working my way through the Manual and got to the section about the composition operator. I wanted to try the example (sqrt ∘ +)(3,6) in my own jupyter notebook but used (√ ∘ +)(3,6) instead which throws the error:
error "syntax: “∘” is not a unary operator

To me it is not clear why the first one one ((sqrt ∘ +)(3,6)) works but not the other one ((√ ∘ +)(3,6)). I thought sqrt and √ were meant to be equivalent.

I started learning Julia just a few days ago but as far as I can tell at this point, this is easily the most elegant language I’ve ever seen!! :heart_eyes: (I’m coming over from Matlab)

2 Likes

In this case you have to use parentheses to disambiguate the meaning

julia> ((√) ∘ +)(3,6)
3.0
4 Likes

I see! But then why does the + not have to be in brackets?

is one of a small number of unary operators + - ! ~ ¬ √ ∛ ∜ ⋆ ± ∓ that require the parentheses in that position. You’re trying to just refer to the function , not use that as an operator.

I think what’s happening is that the parser is trying to interpret it as √(∘(+(3,6))) which doesn’t make sense so it errors.

There’s no ambiguity in the case on the right, so it doesn’t need parentheses. eg you can do

julia> ((√) ∘ √)(3)
1.3160740129524924
7 Likes