# Why are there all these strange stumbling blocks in Julia?

**URL:** <https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644>\
**Category:** New to Julia\
**Tags:** syntax, python\
**Created:** [January 7, 2023, 6:19pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644 "2023-01-07T18:19:30Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 8, 2023, 10:28pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/42 "2023-01-08T22:28:27Z")

</div>

The most famous example of course is a logarithm

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [January 8, 2023, 10:30pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/43 "2023-01-08T22:30:54Z")

</div>

I hadn’t expected `length(::String)` to behave logarithmically but I guess maybe I should.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [January 8, 2023, 10:41pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/44 "2023-01-08T22:41:14Z")

</div>

free group (as mentioned above) is literally an example… though because a free group has “inverse letters” the length of a\*b may be less than length(a) + length(b)

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 8, 2023, 11:19pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/45 "2023-01-08T23:19:15Z")

</div>

> [@fph](#):
>
> Too bad these two are only suggestions and do not work in practice.

We can make alternation work in practice by specializing the `+` operator:

> **Boring code**
>
> ```julia
> using Base: PCRE, wrap_string
> 
> # copypasta from regex * method, regex.jl line 804
> 
> function Base.:+(r1::Union{Regex,AbstractString,AbstractChar}, rs::Union{Regex,AbstractString,AbstractChar}...)
> mask = PCRE.CASELESS | PCRE.MULTILINE | PCRE.DOTALL | PCRE.EXTENDED # imsx
> match_opts = nothing # all args must agree on this
> compile_opts = nothing # all args must agree on this
> shared = mask
> for r in (r1, rs...)
> r isa Regex || continue
> if match_opts === nothing
> match_opts = r.match_options
> compile_opts = r.compile_options & ~mask
> else
> r.match_options == match_opts &&
> r.compile_options & ~mask == compile_opts ||
> throw(ArgumentError("cannot multiply regexes: incompatible options"))
> end
> shared &= r.compile_options
> end
> unshared = mask & ~shared
> Regex(join((wrap_string(r1, unshared), wrap_string.(rs, Ref(unshared))...), "|"), compile_opts | shared, match_opts)
> end
> 
> Base.:+(r::Regex) = r 
> 
> ```

```julia
julia> x, y = r"abc", r"def"
(r"abc", r"def")

julia> match(x+y, "abc"), match(x+y, "def")
(RegexMatch("abc"), RegexMatch("def"))

julia> x*(x+y) # r"abc(?:abc|def)", equivalent to r"abcabc|abcdef"
r"(?:abc)(?:(?:abc)|(?:def))"

```

Could be nice to get it into `Base`. As you can see, concatenation is distributive over alternation.

The unfortunate thing about these implementations for concatenation and alternation is, it’s not very efficient: it’s basically just converting the regex into a string, doing string manipulation, and then recompiling a new regex from the string. Furthermore this can’t be folded by the compiler. Probably in theory it could be much more efficient, but that might require deeper hooks into the PCRE library than we have (I have no clue). Maybe somebody will one day write a pure Julia PCRE library, in which case the sky would be the limit.

Also, I’m not the happiest about this:

```julia
julia> x+y, y+x
(r"(?:abc)|(?:def)", r"(?:def)|(?:abc)")

julia> ans[1] == ans[2] # lies!
false

```

the two regular expressions above are equivalent, in that they describe the same regular language and the same set of finite automata, but the equality tests for the PCRE library’s regular expressions are not very sophisticated.

Furthermore, we don’t currently have a way to express the Kleene star. We could possibly overload our only unary postfix operator `Base.adjoint`, but that doesn’t feel right. Perhaps one day the parser will be modified so that `(x*)` will call `Base.star(x)`—then Kleene algebra will be fully supported IIUC. (oh how I wish our keyboards had `⋅` though, to disambiguate multiplication from convolution and Kleene stars.)

> [@Sandjan](#):
>
> it is difficult for me to understand this \* … This is very unintuitive for me.

It’s definitely unintuitive, but to a compsci nerd it should be straightforward because it aligns with first-semester CS computability theory stuff.

I think the best way to grok it, is to think of the operators’ use in finite automata and regular expressions. [This is a really good tutorial](https://brilliant.org/wiki/regular-languages/). And then, instead of thinking of `*` as numerical multiplication, think of it more like logical multiplication “and,” `&` or `∧`, and instead of thinking of `+` as numerical addition, think of it like logical addition “or,” `|` or `∨`.

In other words, a finite state machine which accepts the regular language described by the regular expression `r"abc" * r"def"` will only accept strings that have `"abc"` _and_ `"def"` (in that order). And (if we have alternation implemented like I’ve done above) the FSM described by the regular expression `r"abc" + r"def"` will accept strings that have `"abc"` _or_ `"def"`.

Then take the same operator rules for regular expressions and apply them to the strings they process. Alternation doesn’t have an analogous meaning for strings as for regular expressions, but concatenation does.

It’s arguable we should’ve used `&` for concatenation and `|` for alternation, but that ship has probably sailed. And Stephen Kleene isn’t around, so we can’t prod him to request the Kleene algebra to use these operators instead.

At the end of the day though, just like anything else, you get used to it.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [January 9, 2023, 12:17am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/46 "2023-01-09T00:17:09Z")

</div>

`+` and `*` already have meaning in regex too, which makes it more confusing.

@uniment , if alternation is `+` shouldn’t it have an additive inverse and identity?

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 9, 2023, 1:25am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/47 "2023-01-09T01:25:51Z")

</div>

Yes, this is a point pretty strongly in favor of `&` and `|` for concatenation and alternation.

Or we could build a time machine and stop whoever decided to put asterisks on keyboards but not the multiplication dot.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 9, 2023, 3:14am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/48 "2023-01-09T03:14:47Z")

</div>

The concatenation identity (empty string) is written with a 1, and the alternation identity (null string) is written with a 0. See algebraic properties [here](https://en.m.wikipedia.org/wiki/Kleene_algebra).

Because this algebra is a semiring, an additive inverse isn’t necessary. I don’t know who makes these rules. 😅

---

<div class="post-metadata">

**Author:** ![simsurace](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simsurace/32/30216_2.png) [@simsurace](https://discourse.julialang.org/u/simsurace)\
**Post date:** [January 9, 2023, 6:32am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/49 "2023-01-09T06:32:26Z")

</div>

> [@uniment](#):
>
> Furthermore this can’t be folded by the compiler.

Perhaps you are aware of this, but there is a nice package that may be of interest to you: [GitHub - BioJulia/Automa.jl: A julia code generator for regular expressions](https://github.com/BioJulia/Automa.jl)

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 10, 2023, 10:50am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/50 "2023-01-10T10:50:46Z")

</div>

I wasn’t aware. Interesting that they introduce `&` as an intersection operator. They dropped exponentiation though, and don’t have a Kleene star operation.

Pretty neat library. I’ll play around with it, thanks for bringing it up!

---

<div class="post-metadata">

**Author:** ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)\
**Post date:** [January 10, 2023, 1:58pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/51 "2023-01-10T13:58:23Z")

</div>

A small anecdote as to “what is natural”.

A frend of mine was a team leader in software company making accounting software or like that. She had difficulty to explain to her programmer that their customers DO NOT want to have paragraphs numbering starting from zero.

---

<div class="post-metadata">

**Author:** ![gtgt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gtgt/32/46932_2.png) [@gtgt](https://discourse.julialang.org/u/gtgt)\
**Post date:** [January 10, 2023, 3:36pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/52 "2023-01-10T15:36:04Z")

</div>

Maybe those customers should just learn about pointer offset. 🙂

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [January 10, 2023, 8:46pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/53 "2023-01-10T20:46:24Z")

</div>

I do agree with the point, but…  
What is intuition if not familiarity?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 10, 2023, 10:04pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/54 "2023-01-10T22:04:05Z")

</div>

A bit of a philosophical question, I guess. But I think it includes predicting something new from something familiar. Perhaps applying familiar knowledge in news ways, or combining several familiar elements that are not normally used together. I definitely don’t think it’s seeing the plain old familiar thing, there has to be something new that you recognize as ‘making sense’.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [January 10, 2023, 10:56pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/55 "2023-01-10T22:56:12Z")

</div>

Talking about personal tastes, I have no problem adapting to 0-based indexing in C and 1-based indexing in Julia, but Python’s semi-open `range` always appears weird and unintuitive to me. And a Julia programmer learning Python will be shocked to find out that perfectly working scripts cannot run when pasted into the command-line REPL, due to indentation and blank lines…

---

<div class="post-metadata">

**Author:** ![dgagnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgagnon/32/45466_2.png) [@dgagnon](https://discourse.julialang.org/u/dgagnon)\
**Post date:** [January 10, 2023, 11:09pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/56 "2023-01-10T23:09:52Z")

</div>

First language I learned was APL (traumatic 😬). I put parentesis EVERYWHERE I just can’t type  
` return x`  
it has to be  
`return(x)`

I will religiously put  
`return(nothing)`  
if there is nothing to return. I get comments from my colleagues all the time for my weird syntax. Too old to change.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 11, 2023, 8:35am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/57 "2023-01-11T08:35:49Z")

</div>

> [@dgagnon](#):
>
> `return(x)`

Hehe. Not a fan of this one. Does it help if you consider the point that `return` is a keyword, but you are using it with function call syntax, which can be confusing for the reader? I feel it obfuscates the meaning of the statement, giving the impression that it calls into some other function.

And do you do this with all keywords? Even `function`, `struct`, `using`, `module`, `else`, `export`?

> [@dgagnon](#):
>
> I will religiously put  
> `return(nothing)`  
> if there is nothing to return.

Personally, I think explicit

```julia
return nothing

```

is a good thing (without the parens, though.)

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 11, 2023, 8:47am UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/58 "2023-01-11T08:47:03Z")

</div>

> [@DNF](#):
>
> `else`

oh dear lord, what fury hath thou wrought

```julia
julia> if(false) return(1) else(return(2)) end
2

```

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [January 11, 2023, 2:50pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/59 "2023-01-11T14:50:06Z")

</div>

Or even better

```julia
julia> if(false) return(1) else(return(2)) end()
2

```

---

<div class="post-metadata">

**Author:** ![palday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palday/32/12640_2.png) [@palday](https://discourse.julialang.org/u/palday)\
**Post date:** [January 11, 2023, 4:02pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/60 "2023-01-11T16:02:21Z")

</div>

> [@adienes](#):
>
> And of course many popular languages use `+`, including `C++`, `Python`, `Java`, `Go`, `Ruby`, `Typescript`, `Rust`, `Solidity`, `R`, `Swift` etc.

I don’t think this is correct for R:

```R
> "a" + "b"
Error in "a" + "b" : non-numeric argument to binary operator
> paste0("a","b")
[1] "ab"

```

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [January 11, 2023, 5:35pm UTC](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644/61 "2023-01-11T17:35:00Z")

</div>

good catch, sorry. I verified some of the weird ones but I had assumed all the `+` were correct.

[Previous page](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644.md?page=2)

[Next page](https://discourse.julialang.org/t/why-are-there-all-these-strange-stumbling-blocks-in-julia/92644.md?page=4)
