Question about two variables?

Dear all,

What is the difference between foo and bar below?

julia> foo = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> bar = [1; 2; 3]
3-element Vector{Int64}:
 1
 2
 3

julia> foo === bar
false

julia> varinfo()
  name                    size summary
  –––––––––––––––– ––––––––––– –––––––––––––––––––––––
  Base                         Module
  Core                         Module
  InteractiveUtils 252.883 KiB Module
  Main                         Module
  ans                   1 byte Bool
  bar                 64 bytes 3-element Vector{Int64}
  foo                 64 bytes 3-element Vector{Int64}

=== returns whether two objects are indistinguishable from each other. For mutable structs, this means they need to be the same object.
If you write foo[1] = 3, bar[1] will still be 1 so they are not the same object.

1 Like

I see. Thanks.

But I want to know what’s the difference between [1, 2, 3] and [1; 2; 3]?
In Julia, they are both 3-element Vector{Int64}.

julia> foo = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> bar = [1; 2; 3]
3-element Vector{Int64}:
 1
 2
 3

julia> foo == bar
true

julia> typeof(foo)
Vector{Int64} (alias for Array{Int64, 1})

julia> typeof(bar)
Vector{Int64} (alias for Array{Int64, 1})

There isn’t a difference here. The difference between the two syntaxes is that ; concatenates. So

julia> [[1,2], [3,4]]
2-element Vector{Vector{Int64}}:
 [1, 2]
 [3, 4]

julia> [[1,2]; [3,4]]
4-element Vector{Int64}:
 1
 2
 3
 4
3 Likes

Oh, get it. Thanks so much.

1 Like

FYI

https://docs.julialang.org/en/v1/manual/arrays/#man-array-concatenation

1 Like