# Tuples Broadcast and Expression Tree

**URL:** <https://discourse.julialang.org/t/tuples-broadcast-and-expression-tree/8056>\
**Category:** New to Julia\
**Created:** [December 29, 2017, 7:02am UTC](https://discourse.julialang.org/t/tuples-broadcast-and-expression-tree/8056 "2017-12-29T07:02:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eloi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloi/32/8990_2.png) [@eloi](https://discourse.julialang.org/u/eloi)\
**Post date:** [December 29, 2017, 7:02am UTC](https://discourse.julialang.org/t/tuples-broadcast-and-expression-tree/8056/1 "2017-12-29T07:02:57Z")

</div>

I am trying to understand Metaprogramming in Julia. I wrote the following code

```julia

function traversal!(x::Expr,mem)
    for xx in x.args
        if isa(xx,Expr)
            push!(mem,xx)
            traversal!(xx,mem)
        end
    end
end

expr=:(sum((x-cos(W2*sin(W1*x+b1)+b2))^2))
mem=[]
a=traversal!(expr,mem)

```

I expect mem to be an Array with 8 elements

```julia

:((x - cos(W2 * sin(W1 * x + b1) + b2)) ^ 2)
:(x - cos(W2 * sin(W1 * x + b1) + b2))
:(cos(W2 * sin(W1 * x + b1) + b2))
:(W2 * sin(W1 * x + b1) + b2)
:(W2 * sin(W1 * x + b1))
:(sin(W1 * x + b1))
:(W1 * x + b1)
:(W1 * x)

```

Result is as expected however when i replace the expression

```julia
expr=:(sum((x-cos(W2*sin(W1*x+b1)+b2))^2))

```

with

```julia
expr=:(sum((x-cos.(W2*sin.(W1*x+b1)+b2)).^2))

```

output becomes

```julia
:((x - cos.(W2 * sin.(W1 * x + b1) + b2)) .^ 2)
:(x - cos.(W2 * sin.(W1 * x + b1) + b2))
:(cos.(W2 * sin.(W1 * x + b1) + b2))
:((W2 * sin.(W1 * x + b1) + b2,))
:(W2 * sin.(W1 * x + b1) + b2)
:(W2 * sin.(W1 * x + b1))
:(sin.(W1 * x + b1))
:((W1 * x + b1,))
:(W1 * x + b1)
:(W1 * x)

```

Why do i get those extra Tuple Symbols?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 29, 2017, 7:06am UTC](https://discourse.julialang.org/t/tuples-broadcast-and-expression-tree/8056/2 "2017-12-29T07:06:09Z")

</div>

Crosspost: [Julia traversing the expression tree containing broadcast results in unintended tuples - Stack Overflow](https://stackoverflow.com/questions/48018213/julia-traversing-the-expression-tree-containing-broadcast-results-in-unintended)

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [December 29, 2017, 7:33am UTC](https://discourse.julialang.org/t/tuples-broadcast-and-expression-tree/8056/3 "2017-12-29T07:33:05Z")

</div>

This is how broadcast expressions are stored. The tuple will host all arguments to the function which happens to be 1 in your case.

```julia
julia> dump(:(f.(x)))
Expr
  head: Symbol .
  args: Array{Any}((2,))
    1: Symbol f
    2: Expr
      head: Symbol tuple
      args: Array{Any}((1,))
        1: Symbol x
      typ: Any
  typ: Any

julia> dump(:(f.(x, y)))
Expr
  head: Symbol .
  args: Array{Any}((2,))
    1: Symbol f
    2: Expr
      head: Symbol tuple
      args: Array{Any}((2,))
        1: Symbol x
        2: Symbol y
      typ: Any
  typ: Any

julia> dump(:(f.(x, y, z)))
Expr
  head: Symbol .
  args: Array{Any}((2,))
    1: Symbol f
    2: Expr
      head: Symbol tuple
      args: Array{Any}((3,))
        1: Symbol x
        2: Symbol y
        3: Symbol z
      typ: Any
  typ: Any

```
