Why is multiplication A*B*C left-associative (`foldl`) not right-associative (`foldr`)?

BTW, * in Julia’s AST do not say it’s left- or right-associative:

julia> dump(:(1 * 2 * 3))
Expr
  head: Symbol call
  args: Array{Any}((4,))
    1: Symbol *
    2: Int64 1
    3: Int64 2
    4: Int64 3

(I found that it’s strange. Why is that?)

So it’s easy to get right-associative * a la future-import (provided you do it before any use of *):

julia> module RightAssociative
       *(a) = Base.:*(a)
       *(a, b) = Base.:*(a, b)
       *(args...) = *(args[1:end-2]..., *(args[end-1], args[end]))
       end
Main.RightAssociative

julia> using .RightAssociative: *

julia> Base.:*(x::Symbol, y) = Symbol("($x * $y)")

julia> :a * :b * :c
Symbol("(a * (b * c))")

where the result of the last output would be Symbol("((a * b) * c)") without using .RightAssociative: *.

5 Likes