Think of append!(X, Y) as equivalent to X = vcat(X, Y). You called append!
twice, so X gets Y appended twice.

julia> X = [1,2]; Y = [3,4];

julia> X = vcat(X,Y)
[1, 2, 3, 4]

In your example you went ahead and did this again:

julia> X = (X = vcat(X, Y))
[1, 2, 3, 4, 3, 4]

But if you reset X, Y via the first statement and *then* call X =
append!(X, Y), it works as you would expect.

julia> X = [1,2]; Y = [3,4];

julia> X = append!(X, Y) # same as X = (X = vcat(X, Y))
[1, 2, 3, 4]

On 11 December 2014 at 07:51, Alex Ames <[email protected]> wrote:

> Functions that end with an exclamation point modify their arguments, but
> they can return values just like any other function. For example:
>
> julia> x = [1,2]; y = [3, 4]
> 2-element Array{Int64,1}:
>  3
>  4
>
> julia> append!(x,y)
> 4-element Array{Int64,1}:
>  1
>  2
>  3
>  4
>
> julia> z = append!(x,y)
> 6-element Array{Int64,1}:
>  1
>  2
>  3
>  4
>  3
>  4
>
> julia> z
> 6-element Array{Int64,1}:
>  1
>  2
>  3
>  4
>  3
>  4
>
> julia> x
> 6-element Array{Int64,1}:
>  1
>  2
>  3
>  4
>  3
>  4
>
> The append! function takes two arrays, appends the second to the first,
> then returns the values now contained by the first array. No recursion
> craziness required.
>
> On Thursday, December 11, 2014 1:11:50 AM UTC-6, Sean McBane wrote:
>>
>> Ivar is correct; I was running in the Windows command prompt and couldn't
>> copy and paste so I copied it by hand and made an error.
>>
>> Ok, so I understand that append!(X,Y) is modifying X in place. But I
>> still do not get where the output for the second case, where the result of
>> append!(X,Y) is assigned back into X is what it is. It would make sense to
>> me if this resulted in a recursion with Y forever getting appended to X,
>> but as it is I don't understand.
>>
>> Thanks.
>>
>> -- Sean
>>
>> On Thursday, December 11, 2014 12:42:45 AM UTC-6, Ivar Nesje wrote:
>>>
>>> I assume the first line should be
>>>
>>> > X = [1,2]; Y = [3,4];
>>>
>>> Then the results you get makes sense. The thing is that julia has
>>> mutable arrays, and the ! at the end of append! indicates that it is a
>>> function that mutates it's argument.
>>
>>

Reply via email to