# Help understand the role of semicolon in turning a UnitRange to a Vector

**URL:** <https://discourse.julialang.org/t/help-understand-the-role-of-semicolon-in-turning-a-unitrange-to-a-vector/64856>\
**Category:** New to Julia\
**Tags:** vector, range, semicolon\
**Created:** [July 18, 2021, 12:15pm UTC](https://discourse.julialang.org/t/help-understand-the-role-of-semicolon-in-turning-a-unitrange-to-a-vector/64856 "2021-07-18T12:15:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 18, 2021, 12:15pm UTC](https://discourse.julialang.org/t/help-understand-the-role-of-semicolon-in-turning-a-unitrange-to-a-vector/64856/1 "2021-07-18T12:15:44Z")

</div>

I understand the difference between the [UnitRange](https://docs.julialang.org/en/v1/base/collections/#Base.UnitRange) and Array/[Vector](https://docs.julialang.org/en/v1/base/arrays/#Base.Vector) types. I sort of get the syntax for stacking two ranges and converting them into a vector using `;` and `[]` as in

```julia
julia> a = [1:3;4:5]
5-element Vector{Int64}:
 1
 2
 3
 4
 5

```

But I think I need some hint on how to remember/understand/interpret the usage of semicolon for converting just a single unit range into a vector as in:

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

```

The [documentation on puncuation](https://docs.julialang.org/en/v1/base/punctuation/) gives this description for semicolon:

> semicolons separate statements, begin a list of keyword arguments in function declarations or calls, or are used to separate array literals for vertical concatenation

and I am just not able to fit this usage to the above described conversion of a range into a vector.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 18, 2021, 12:20pm UTC](https://discourse.julialang.org/t/help-understand-the-role-of-semicolon-in-turning-a-unitrange-to-a-vector/64856/2 "2021-07-18T12:20:55Z")

</div>

> [@zdenek\_hurak](#):
>
> used to separate array literals for vertical concatenation

it’s this.

```julia
julia> vcat(1:3)
3-element Vector{Int64}:
 1
 2
 3

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

julia> vcat(1:3, 4:6)
6-element Vector{Int64}:
 1
 2
 3
 4
 5
 6

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

```

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 18, 2021, 12:24pm UTC](https://discourse.julialang.org/t/help-understand-the-role-of-semicolon-in-turning-a-unitrange-to-a-vector/64856/3 "2021-07-18T12:24:11Z")

</div>

I suspected that it should be interpreted like this. Simply a (vertical) concatenation of _something_ with _nothing_, right?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 18, 2021, 12:26pm UTC](https://discourse.julialang.org/t/help-understand-the-role-of-semicolon-in-turning-a-unitrange-to-a-vector/64856/4 "2021-07-18T12:26:12Z")

</div>

I don’t see how “with nothing” is a useful concept here. You can concatenate any number of abstract vectors into a vector, any number includes 1 and 0.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 18, 2021, 12:30pm UTC](https://discourse.julialang.org/t/help-understand-the-role-of-semicolon-in-turning-a-unitrange-to-a-vector/64856/5 "2021-07-18T12:30:48Z")

</div>

Perhaps I am just too biased to see the semicolon as a _separator_ of two things. That is why I somehow easily accepted the syntax `[a;b]` and struggled mentally with `[a;]`. But most probably there is not much more to dig here. I will just remember it. Thanks.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 18, 2021, 1:41pm UTC](https://discourse.julialang.org/t/help-understand-the-role-of-semicolon-in-turning-a-unitrange-to-a-vector/64856/6 "2021-07-18T13:41:31Z")

</div>

Yes instead of separators I think it’s best to think of `[a, b, ...]`, `[a; b; ...]`, `[a b ...]` and `[a b; c d; ...]` as fancy syntax for four different _functions_.

Four lines above in your linked documentation page, there’s a description specifically for the case of array literals:

> `[;]`[vertical concatenation](https://docs.julialang.org/en/v1/manual/arrays/#man-array-concatenation) (calling [vcat](https://docs.julialang.org/en/v1/base/arrays/#Base.vcat) or [hvcat](https://docs.julialang.org/en/v1/base/arrays/#Base.hvcat))  
> `[]` with space-separated expressions, [horizontal concatenation](https://docs.julialang.org/en/v1/manual/strings/#man-concatenation) (calling [`hcat`](https://docs.julialang.org/en/v1/base/arrays/#Base.hcat) or [`hvcat`](https://docs.julialang.org/en/v1/base/arrays/#Base.hvcat))

In you example:

- `[1:3]` is a special case of `[a, b, ...]` with only one argument `a=1:3`. This is fancy syntax for the `Base.vect` function:

- `[1:3;]` is a special case of `[a; b; ...]` with one argument `1:3`. That’s fancy syntax for `vcat`:

(As for the remaining cases: `[a b ...]` is fancy syntax for `hcat` and `[a b ...; c d ...; ...]` is fancy syntax for `hvcat`. Both of these also treat their arguments as iterators.)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 18, 2021, 1:41pm UTC](https://discourse.julialang.org/t/help-understand-the-role-of-semicolon-in-turning-a-unitrange-to-a-vector/64856/7 "2021-07-18T13:41:53Z")

</div>

No idea if this is a correct mental model but, in case it helps, may see it as concatenation with empty `[]`:

```julia
a=1:3
julia> [a;] == [a;eltype(a)[]]
true

```
